ZOJ1805 超级四分树 压缩四分树

思路:
在这里插入图片描述
实现代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n, m;
int Index, count1, count2;
char str[150][150];
int MAP[150][150];
class quadtree
{
public:
	int q[4];
	quadtree() {}
	quadtree(quadtree& a)
	{
		for (int i = 0; i < 4; i++)
			q[i] = a.q[i];
	}
	bool operator==(quadtree a)
	{
		for (int i = 0; i < 4; i++)
			if (!(q[i] == a.q[i]))return false;
		return true;
	}
}search[1800];
void init()
{
	memset(search, 0, sizeof(search));
	memset(MAP, 0, sizeof(MAP));
	for (int i = 0; i < n; i++)
	{
		cin >> str[i];
		for (int j = 0; j < m; j++)
			MAP[i][j] = str[i][j] - '0';
	}
	count1 = count2 = 0;
	Index = 2;
}
int DFS(int x, int y, int s)
{
	int count2_2 = count2;
	count1++;
	count2++;
	if (s == 1)return MAP[x][y];
	quadtree*temp = new quadtree;
	s /= 2;
	temp->q[0] = DFS(x, y, s);
	temp->q[1] = DFS(x + s, y, s);
	temp->q[2] = DFS(x, y + s, s);
	temp->q[3] = DFS(x + s, y + s, s);
	int flag = temp->q[0];
	for (int i = 1; i < 4; i++)
		if (!(temp->q[i] == temp->q[0])) {
			flag = 2; break;
		}
	if (flag == 0 || flag == 1) { count1 -= 4; count2 -= 4; return flag; }//合并删除
	for (int i = 2; i < Index; i++)
		if (search[i]==*temp)
		{
			count2 = count2_2;
			return i;
		}
	search[Index++] = quadtree(*temp);
	return Index - 1;
}
int main()
{
	while (scanf_s("%d %d", &n, &m) && n&&m)
	{
		init();
		if (n < m)swap(n, m);
		int k = 1;
		while (k < n)k = k << 1;
		DFS(0, 0, k);
		cout << count1 << " " << count2 << endl;
	}
	//system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值