四叉树 bnuoj

点击打开题目链接


建树+广搜一棵树;最下面有更短代码(很巧妙)。

 

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<queue>
  4 #include<string.h>
  5 #include<algorithm>
  6 
  7 using namespace std;
  8 
  9 int maze[20][20];
 10 struct node
 11 {
 12     int num;
 13     node *next[4];
 14 };
 15 void init_tree(node *root)
 16 {
 17     int i;
 18     for(i=0; i<4; i++)
 19         root->next[i] = NULL;
 20 }
 21 void built(int istart,int iend,int jstart,int jend,node *root)
 22 {
 23     int dive,i,j;
 24     int istart1=istart,iend1=iend,jstart1=jstart,jend1=jend;
 25     for(dive=0; dive<4; dive++)
 26     {
 27         if(dive==0) istart=istart1,iend=istart1+(iend1-istart1+1)/2-1,jstart=jstart1,jend=jstart1+(jend1-jstart1+1)/2-1;
 28         else if(dive==1) istart=istart1,iend=istart1+(iend1-istart1+1)/2-1,jstart=jstart1+(jend1-jstart1+1)/2,jend=jend1;
 29         else if(dive==2) istart=istart1+(iend1-istart1+1)/2,iend=iend1,jstart=jstart1,jend=jstart1+(jend1-jstart1+1)/2-1;
 30         else istart=istart1+(iend1-istart1+1)/2,iend=iend1,jstart=jstart1+(jend1-jstart1+1)/2,jend=jend1;
 31         int flag0=0,flag1=0,tree_point;
 32         for(i=istart; i<=iend; i++)
 33         {
 34             for(j=jstart; j<=jend; j++)
 35             {
 36                 if(maze[i][j]==0) flag0=1;
 37                 if(maze[i][j]==1) flag1=1;
 38             }
 39         }
 40         if(flag0&&flag1) tree_point=2;
 41         else if(flag0&&!flag1) tree_point=0;
 42         else if(!flag0&&flag1) tree_point=1;
 43         node *p;
 44         p=new node;
 45         init_tree(p);
 46         root->next[dive]=p;
 47         p->num=tree_point;
 48         if(tree_point==1||tree_point==0) continue ;
 49         built(istart,iend,jstart,jend,p);
 50     }
 51 }
 52 void print(node *root)
 53 {
 54     node *ss,*tt;
 55     queue<node*>q;
 56     q.push(root);
 57     while(!q.empty())
 58     {
 59         ss=q.front();
 60         int su=ss->num;
 61         if(su==2) printf("1");
 62         else if(su==0) printf("00");
 63         else if(su==1) printf("01");
 64         q.pop();
 65         for(int i=0; i<4; i++)
 66         {
 67             if(ss->next[i] !=NULL)
 68             {
 69                 tt=ss->next[i];
 70                 q.push(tt);
 71             }
 72         }
 73     }
 74     printf("\n");
 75 }
 76 int check_map(int n)
 77 {
 78     int i,j,sum=0;
 79     for(i=1; i<=n; i++)
 80         for(j=1; j<=n; j++)
 81             sum+=maze[i][j];
 82     if(sum==n*n) return 1;
 83     else if(sum==0) return 0;
 84     else return -1;
 85 }
 86 void delete_tree(node *root)
 87 {
 88     int i;
 89     for(i=0; i<4; i++)
 90     {
 91         if(root->next[i]!=NULL)
 92         {
 93             delete_tree(root->next[i]);
 94         }
 95     }
 96     delete root;
 97 }
 98 int main()
 99 {
100     int n;
101     while(scanf("%d",&n)!=EOF)
102     {
103         int i,j;
104         memset(maze,0,sizeof(maze));
105         for(i=1; i<=n; i++)
106         {
107             for(j=1; j<=n; j++)
108                 scanf("%d",&maze[i][j]);
109         }
110         int ans=check_map(n);
111         if(ans==0) printf("00\n");
112         else if(ans==1) printf("01\n");
113         else
114         {
115             node *tree;
116             tree=new node;
117             init_tree(tree);
118             tree->num=2;
119             built(1,n,1,n,tree);
120             print(tree);
121             delete_tree(tree);
122         }
123     }
124     return 0;
125 }
View Code

 

短代码:

 1 #include <cstring>
 2 #include <vector>
 3 #include<stack>
 4 #include<queue>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<iostream>
 8 #include<cstring>
 9 //#define N 10005
10 using namespace std;
11 int zu[20][20],n;
12 struct sb
13 {
14     int x,y,x1,y1;
15     bool Get()
16     {
17         int ans=0;
18         for(int i=x;i<=x1;i++)
19             for(int j=y;j<=y1;j++)
20             ans+=zu[i][j];
21         if(!ans)
22             return true;
23         if(ans==(x1-x+1)*(y1-y+1))
24             return true;
25         return false;
26     }
27 };
28 int main()
29 {
30     while(scanf("%d",&n)==1)
31     {
32         for(int i=1;i<=n;i++)
33             for(int j=1;j<=n;j++)
34             scanf("%d",&zu[i][j]);
35 //
36             sb s1,s2;
37             s1.x=1;
38             s1.y=1;
39             s1.x1=n;
40             s1.y1=n;
41             queue<sb>Q;
42             Q.push(s1);
43             while(!Q.empty())
44             {
45                 s1=Q.front();
46                 Q.pop();
47                 if(s1.Get())
48                 {
49                     printf("0%d",zu[s1.x1][s1.y1]);
50                     continue;
51                 }
52                 printf("1");
53                 int xx=(s1.x+s1.x1)/2;
54                 int yy=(s1.y+s1.y1)/2;
55                 s2=s1;
56                 s2.y1=yy;
57                 s2.x1=xx;
58                 Q.push(s2);
59 //
60                 s2=s1;
61                 s2.x1=xx;
62                 s2.y=yy+1;
63                 Q.push(s2);
64 //
65                 s2=s1;
66                 s2.x=xx+1;
67                 s2.y1=yy;
68                 Q.push(s2);
69 //
70                 s2=s1;
71                 s2.x=xx+1;
72                 s2.y=yy+1;
73                 Q.push(s2);
74             }
75             printf("\n");
76     }
77 }
View Code

 

posted @ 2016-04-20 21:01  Code-dream  阅读(192)  评论(0编辑  收藏  举报