zoj1959-Quadtree II

#include<iostream>
#include<string>
#include<cstring>
#include<math.h>
#include<stdio.h>
using namespace std;
const int maxsize=512;
char MAP[maxsize][maxsize];
int n;
struct quadtree
{
	quadtree *q[4];
	string value;
	bool operator == (quadtree temp)
	{
		return temp.value==value;
	}
	quadtree()
	{
		for(int i=0;i<4;i++)
			q[i]=NULL;
	}
};
quadtree *DFS(int r,int c,int s)//行号列号间隔
{
	int i;
	bool f=true;
	quadtree *temp=new quadtree;
	if(s==1)//最小格就赋值
	{
		temp->value='0';
		temp->value+=MAP[r][c];
		return temp;
	}
	s/=2;
	temp->q[0]=DFS(r,c,s);//跳格继续递归
	temp->q[1]=DFS(r,c+s,s);
	temp->q[2]=DFS(r+s,c,s);
	temp->q[3]=DFS(r+s,c+s,s);
	for(i=1;i<4;++i)//检测是否需要合并
	{
		if(!(*temp->q[0]==*temp->q[i])||temp->q[0]->value=="1")//不用合并
		{
			f=false;
			break;
		}
	}
	if(f)//需要
	{
		temp->value=temp->q[0]->value;
		for(i=0;i<4;++i)
		{
			delete temp->q[i];
			temp->q[i]=NULL;//防止野指针
		}
	}
	else
		temp->value="1";
	return temp;
}
void preDFS(quadtree *node)
{
	if(node)
	{
		if(node->value=="1")
			cout<<'Q';
		else
			if(node->value=="01")
				cout<<'B';
			else
				cout<<'W';
		for(int i=0;i<4;i++)
			preDFS(node->q[i]);
	}
}
void deltree(quadtree *&node)//回收内存
{
	if(node)
	{
		for(int i=0;i<4;++i)
		{
			deltree(node->q[i]);
		}
		 delete node;
	}
}
int main()
{
	string str;
	int num;
	quadtree *order;
	char ch;
	cin>>str>>str>>n;
	cin.get(ch);
	cin.ignore(50,'\n');
	cin.ignore(50,'\n');
	memset(MAP,'0',sizeof(MAP));
	for(int i=0;i<n;i++)//c
	{
		int k=0;
		while(cin.get(ch))
		{
			if(ch=='\n')
				break;
			if(ch=='x')
			{
				int sum=0;
				scanf("%x",&num);
				for(int j=7;j>=0;j--)
				{
					if(sum<num)
					{
						sum+=pow(2.0,j);
						MAP[i][j+8*k]='1';
					}
					if(sum>num)
					{
						sum-=pow(2.0,j);
						MAP[i][j+8*k]='0';
					}
					if(sum==num)
						break;
				}
				k++;
			}
		}
	}
	cin.clear();
	cout<<n<<endl;
	order=DFS(0,0,n);
	preDFS(order);
	cout<<endl;
	deltree(order);
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值