zoj 1788 Quad Trees

#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;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值