UVa 297 四分树

感觉特别像那个分治的日程表问题。是f的话就填,否则就不填,然后同一个表填两次。那么就是最后的结果。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <map>
 5 #include <set>
 6 #include <algorithm>
 7 #include <fstream>
 8 #include <cstdio>
 9 #include <cmath>
10 #include <stack>
11 #include <queue>
12 using namespace std;
13 const double Pi=3.14159265358979323846;
14 typedef long long ll;
15 const int MAXN=5000+5;
16 const int dx[5]={0,0,0,1,-1};
17 const int dy[5]={1,-1,0,0,0};
18 const int INF = 0x3f3f3f3f;
19 const int NINF = 0xc0c0c0c0;
20 const ll mod=1e9+7;
21 int a[MAXN][MAXN];
22 void build(string str,int &cnt,int r1,int r2,int c1,int c2)
23  {
24      char s=str[cnt++];
25      if(s=='p')
26      {
27          int midr=(r1+r2)/2,midc=(c1+c2)/2;
28          build(str,cnt,r1,midr,midc+1,c2);
29          build(str,cnt,r1,midr,c1,midc);
30          build(str,cnt,midr+1,r2,c1,midc);
31          build(str,cnt,midr+1,r2,midc+1,c2);         
32     }
33     else if(s=='f')
34     {
35         //wcout <<"in";
36         for(int i=r1;i<=r2;i++)
37             for(int j=c1;j<=c2;j++)
38                 a[i][j]=1;
39     }
40     
41  }
42 
43 int main()
44 {
45     int t;cin>>t;
46     while(t--)
47     {
48         int cnt=0;
49         string str1,str2;
50         cin>>str1>>str2;
51         memset(a,0,sizeof(a));
52         build(str1,cnt,1,32,1,32);
53         /*for(int i=1;i<=32;i++)
54         {
55             for(int j=1;j<=32;j++)
56             {
57                 cout <<a[i][j]<<" ";
58             } 
59             cout <<endl;
60         }*/
61         cnt=0;
62         build(str2,cnt,1,32,1,32);
63         int ans=0;
64         for(int i=1;i<=32;i++)
65         {
66             for(int j=1;j<=32;j++)
67             {
68                 if(a[i][j]) ans++;
69             } 
70             
71         }
72         
73         printf("There are %d black pixels.\n",ans);
74     }
75     return 0;
76 }
77 
78  

 

posted @ 2019-04-08 15:22  Chuhanjing  阅读(80)  评论(0编辑  收藏  举报