數據結構:四分樹


紫書原題,UVA297

利用四叉樹處理圖片,給你兩張黑白圖片的四叉樹,問兩張圖片疊加后黑色的面積

給出兩顆四分樹的先序遍歷,求合並之后黑色像素的個數,p表示斑馬結點,f表示黑色,e表示白色

四分樹是一個神奇的樹,只需要給出先序遍歷就可以確定整棵樹

四分樹也可以用來實現二維線段樹,只不過太惡心啦

 1 #include<cstdio>
 2 #include<cstring>
 3 const int len=32;
 4 const int maxn=1024+10;
 5 char s[maxn];
 6 int cnt;
 7 int buf[len][len];
 8 //把字符串s導出到以r,c為左上角,邊長為w的緩沖區中
 9 //2 1
10 //3 4 
11 void draw(const char* s,int &p,int r,int c,int w)
12 {
13     char ch=s[p++];
14     if(ch=='p')
15     {
16         draw(s,p,r,c+w/2,w/2);  //1
17         draw(s,p,r,c,w/2);  //2
18         draw(s,p,r+w/2,c,w/2);  //3
19         draw(s,p,r+w/2,c+w/2,w/2);  //4
20     }
21     else if(ch=='f')  //畫黑像素
22     {
23         for(int i=r;i<r+w;i++)
24             for(int j=c;j<c+w;j++)
25                 if(buf[i][j]==0)
26                 {
27                     buf[i][j]=1;cnt++;
28                 }
29     } 
30 }
31 int main()
32 {
33     int T;
34     scanf("%d",&T);
35     while(T--)
36     {
37         memset(buf,0,sizeof(buf));
38         cnt=0;
39         for(int i=0;i<2;i++)
40         {
41             scanf("%s",s);
42             int p=0;
43             draw(s,p,0,0,len);
44         }
45         printf("There are %d black pixels.\n",cnt);
46     }
47     return 0;
48 }

有個BUG就是cnt放在前面就涼了,為啥呀


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM