建树+广搜一棵树;最下面有更段代码。
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int maze[20][20];
struct node
{
int num;
node *next[4];
};
void init_tree(node *root)
{
int i;
for(i=0; i<4; i++)
root->next[i] = NULL;
}
void built(int istart,int iend,int jstart,int jend,node *root)
{
int dive,i,j;
int istart1=istart,iend1=iend,jstart1=jstart,jend1=jend;
for(dive=0; dive<4; dive++)
{
if(dive==0) istart=istart1,iend=istart1+(iend1-istart1+1)/2-1,jstart=jstart1,jend=jstart1+(jend1-jstart1+1)/2-1;
else if(dive==1) istart=istart1,iend=istart1+(iend1-istart1+1)/2-1,jstart=jstart1+(jend1-jstart1+1)/2,jend=jend1;
else if(dive==2) istart=istart1+(iend1-istart1+1)/2,iend=iend1,jstart=jstart1,jend=jstart1+(jend1-jstart1+1)/2-1;
else istart=istart1+(iend1-istart1+1)/2,iend=iend1,jstart=jstart1+(jend1-jstart1+1)/2,jend=jend1;
int flag0=0,flag1=0,tree_point;
for(i=istart; i<=iend; i++)
{
for(j=jstart; j<=jend; j++)
{
if(maze[i][j]==0) flag0=1;
if(maze[i][j]==1) flag1=1;
}
}
if(flag0&&flag1) tree_point=2;
else if(flag0&&!flag1) tree_point=0;
else if(!flag0&&flag1) tree_point=1;
node *p;
p=new node;
init_tree(p);
root->next[dive]=p;
p->num=tree_point;
if(tree_point==1||tree_point==0) continue ;
built(istart,iend,jstart,jend,p);
}
}
void print(node *root)
{
node *ss,*tt;
queue<node*>q;
q.push(root);
while(!q.empty())
{
ss=q.front();
int su=ss->num;
if(su==2) printf("1");
else if(su==0) printf("00");
else if(su==1) printf("01");
q.pop();
for(int i=0; i<4; i++)
{
if(ss->next[i] !=NULL)
{
tt=ss->next[i];
q.push(tt);
}
}
}
printf("\n");
}
int check_map(int n)
{
int i,j,sum=0;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
sum+=maze[i][j];
if(sum==n*n) return 1;
else if(sum==0) return 0;
else return -1;
}
void delete_tree(node *root)
{
int i;
for(i=0; i<4; i++)
{
if(root->next[i]!=NULL)
{
delete_tree(root->next[i]);
}
}
delete root;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,j;
memset(maze,0,sizeof(maze));
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
scanf("%d",&maze[i][j]);
}
int ans=check_map(n);
if(ans==0) printf("00\n");
else if(ans==1) printf("01\n");
else
{
node *tree;
tree=new node;
init_tree(tree);
tree->num=2;
built(1,n,1,n,tree);
print(tree);
delete_tree(tree);
}
}
return 0;
}
/********************************************************************************************************/
#include <cstring>
#include <vector>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
//#define N 10005
using namespace std;
int zu[20][20],n;
struct sb
{
int x,y,x1,y1;
bool Get()
{
int ans=0;
for(int i=x;i<=x1;i++)
for(int j=y;j<=y1;j++)
ans+=zu[i][j];
if(!ans)
return true;
if(ans==(x1-x+1)*(y1-y+1))
return true;
return false;
}
};
int main()
{
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&zu[i][j]);
//
sb s1,s2;
s1.x=1;
s1.y=1;
s1.x1=n;
s1.y1=n;
queue<sb>Q;
Q.push(s1);
while(!Q.empty())
{
s1=Q.front();
Q.pop();
if(s1.Get())
{
printf("0%d",zu[s1.x1][s1.y1]);
continue;
}
printf("1");
int xx=(s1.x+s1.x1)/2;
int yy=(s1.y+s1.y1)/2;
s2=s1;
s2.y1=yy;
s2.x1=xx;
Q.push(s2);
//
s2=s1;
s2.x1=xx;
s2.y=yy+1;
Q.push(s2);
//
s2=s1;
s2.x=xx+1;
s2.y1=yy;
Q.push(s2);
//
s2=s1;
s2.x=xx+1;
s2.y=yy+1;
Q.push(s2);
}
printf("\n");
}
}