1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #include <stdio.h> #include <utility> #include <queue> #include <cmath> #include <stack> using namespace std; queue<pair<pair<int, int>, int> > q; stack<int> change; stack<char> rst; int arr[550][550] = { 0 }; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf("%d", &arr[i][j]); } } pair<int, int> xy(1,1); q.push(make_pair(xy,n)); while (q.empty() == false) { int x = q.front().first.first; int y = q.front().first.second; int len = q.front().second; int flag = 0; q.pop(); for (int i = y; i < y + len; i++) { for (int j = x; j < x + len; j++) { if (arr[y][x] != arr[i][j]) { change.push(1); // 분할, 하나씩 xy = make_pair(x, y); q.push(make_pair(xy,len/2)); xy = make_pair(x+len/2, y); q.push(make_pair(xy, len / 2)); xy = make_pair(x, y+len/2); q.push(make_pair(xy, len / 2)); xy = make_pair(x+len/2, y+len/2); q.push(make_pair(xy, len / 2)); // 탈출조건 flag = 1; break; } } if (flag == 1) break; } // 모든 숫자가 일치할 경우, 분할 안함 if (flag == 0) { change.push(0); change.push(arr[y][x]); } } int sixteen = 0, tmp=0; while (change.empty() == false) { if (sixteen == 4) { if (tmp < 10) rst.push((char)('0' + tmp)); else rst.push((char)('A' + tmp - 10)); sixteen = 0; tmp = 0; } if(change.top() == 1) tmp += (int) pow(2, sixteen); change.pop(); sixteen++; } if (tmp != 0) { rst.push((char)('0' + tmp)); } // 결과값 rst에 넣어서 역순으로 출력하기 while (rst.empty() == false) { printf("%c", rst.top()); rst.pop(); } } | cs |
'알고리즘 > Divide and Conquer-일반' 카테고리의 다른 글
색종이 만들기 더블릿 (0) | 2019.02.13 |
---|---|
catoring along 더블릿 (0) | 2019.02.13 |
1992 쿼드트리 (0) | 2019.02.11 |