#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map>
using namespace std;
int g[513][513];
struct node {
int r, c, l;
};
string arr[16] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
node root;
string bfs() {
queue<node> q;
q.push(root);
int sz, i, j, k, len;
bool flag;
string ans = "";
while (!q.empty()) {
sz = q.size();
while (sz--) {
node nd = q.front();
q.pop();
len = nd.l / 2;
flag = true;
for (i = 0; i < len; i++) {
for (j = 0; j < len; j++) {
if (g[nd.r + i][nd.c + j] == g[nd.r][nd.c + len + j] && g[nd.r + len + i][nd.c + j] == g[nd.r + len + i][nd.c + len + j]
&& g[nd.r + i][nd.c + j] == g[nd.r + len + i][nd.c + j]) {
continue;
} else {
flag = false;
}
}
}
if (flag) {
ans += '0';
ans += char('0' + g[nd.r][nd.c]);
} else {
ans += "1";
node n1 = {nd.r, nd.c, len};
node n2 = {nd.r, nd.c + len, len};
node n3 = {nd.r + len, nd.c, len};
node n4 = {nd.r + len, nd.c + len, len};
q.push(n1);
q.push(n2);
q.push(n3);
q.push(n4);
}
}
}
return ans;
}
string gethex(string str) {
int x = str.find_first_of("1", 0);
if (x == -1)return "0";
str = str.substr(x, str.size() - x);
int len = str.size();
int i, sum, k, t;
string ans = "";
t = 0;
k = 1;
sum = 0;
for (i = len - 1; i >= 0; i--) {
t++;
sum += k * int(str[i] - '0');
k = k << 1 ;
if (t % 4 == 0 || i == 0) {
ans = arr[sum] + ans;
sum = 0;
k = 1;
}
}
return ans;
}
int main() {
int t, n, i, j;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &g[i][j]);
}
}
root = {1, 1, n};
string ans = bfs();
ans = gethex(ans);
cout << ans << endl;
}
}
zoj 1788 Quad Trees
最新推荐文章于 2024-08-21 18:53:45 发布