#include <iostream>
using namespace std;
int a[100][100];
bool same(int x, int y, int n) {
       for (int i = x; i < x + n; ++i) {
               for (int j = y; j < y + n; ++j) {
                      if (a[x][y] != a[i][j])
                              return false;
               }
       }
       return true;
}
void solve(int x, int y, int n) {
       if (same(x, y, n)) {
               cout << a[x][y];
       }
       else {
               cout << "(";
               int m = n / 2;
               for (int i = 0; i < 2; ++i) {
                      for (int j = 0; j < 2; ++j) {
                              solve(x + m * i, y + m * j, m);
                      }
               }
               cout << ")";
       }
}
int main(void) {
       int n;
       cin >> n;
       for (int i = 0; i < n; ++i) {
               for (int j = 0; j < n; ++j) {
                      scanf("%1d", &a[i][j]);
               }
       }
       solve(0, 0, n);
       cout << endl;
       return 0;
}


#include <iostream>
using namespace std;
int power2(int k) {
       return (1 << k); //2^k by shift operator
}
int go(int n, int x, int y) {
       if (n == 1) { // repeat devide, and n==1 => finish
               return 2 * x + y;
       }
       else {
               if (x < power2(n - 1)) {
                      if (y < power2(n - 1)) {
                              return go(n - 1, x, y);
                      }
                      else {
                              return go(n - 1, x, y - power2(n - 1)) + power2(2 * n - 2);
                      }
               }
               else {
                      if (y < power2(n - 1)) {
                              return go(n - 1, x - power2(n - 1), y) + power2(2 * n - 2) * 2;
                      }
                      else {
                              return go(n - 1, x - power2(n - 1), y - power2(n - 1)) + power2(2 * n - 2) * 3;
                      }
               }
       }
}
int main(void)
{
       int n, r, c;
       while (cin >> n >> r >> c) {
               cout << go(n, r, c) << '\n';
       }
       return 0;
}a




#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
struct Point {
       int x, y;
       Point() {}
       Point(int x, int y) : x(x), y(y) {
       }
       bool operator < (const Point &v) const {
               if (x == v.x) {
                      return y < v.y;
               }
               else {
                      return x < v.x;
               }
       }
};
bool cmp(const Point &u, const Point &v) {
       return u.y < v.y;
}
int dist(Point p1, Point p2) { //require output is square, so no need root
       return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}
int bf(vector<Point> &a, int x, int y) {
       int ans = -1;
       for (int i = x; i <= y - 1; ++i) {
               for (int j = i + 1; j <= y; j++) {
                      int d = dist(a[i], a[j]);
                      if (ans == -1 || ans > d) {
                              ans = d;
                      }
               }
       }
       return ans;
}
int cs(vector<Point> &a, int x, int y) {
       int n = y - x + 1;
       if (n <= 3) {
               return bf(a, x, y);
       }
       int mid = (x + y) / 2;
       int left = cs(a, x, mid);
       int right = cs(a, mid + 1, y);
       int ans = min(left, right);
       vector<Point> b;
       for (int i = x; i <= y; ++i) {
               int d = a[i].x - a[mid].x;
               if (d*d < ans) {
                      b.push_back(a[i]);
               }
       }
       sort(b.begin(), b.end(), cmp);
       int m = b.size();
       for (int i = 0; i<m - 1; ++i) {
               for (int j = i + 1; j<m; ++j) {
                      int y = b[j].y - b[i].y;
                      if (y*y < ans) {
                              int d = dist(b[i], b[j]);
                              if (d < ans) {
                                     ans = d;
                              }
                      }
                      else {
                              break;
                      }
               }
       }
       return ans;
}
int main(void)
{
       int n;
       cin >> n;
       vector<Point> a(n);
       for (int i = 0; i<n; ++i) {
               cin >> a[i].x >> a[i].y;
       }
       sort(a.begin(), a.end());
       cout << cs(a, 0, n - 1) << endl;
       return 0;
}



'알고리즘' 카테고리의 다른 글

[DC] 백준 1992 쿼드트리  (0) 2018.04.29
[DC] 백준 1074 Z  (0) 2018.04.29
[DC] 백준 11729 하노이의 탑 이동순서  (0) 2018.04.29
[DC] 백준 6236 용돈관리  (0) 2018.04.29
[DC] 백준 11728 배열 합치기  (1) 2018.04.29

+ Recent posts