题面描述:
给两棵四分树的先序遍历,求二者合并之后(黑色部分合并)黑色像素的个数。p表示灰色,f表示黑色(full),e表示白色(empty)
样例输入:
3
ppeeefpffeefe
pefepeefe
peeef
peefe
peeef
peepefefe
样例输出:
There are 640 black pixels.
There are 512 black pixels.
There are 384 black pixels.
题解:
发现它是一棵树的先序遍历,所以说 苟利国 照着建树方法递归输入就好(注意回车符)
#include<cstdio>
#include<cstring>
bool have[33][33];
int T;
void getnum(char opt,int x1,int y1,int x2,int y2)
{
if(opt=='\n') return;
if(opt=='f')
{
for(int x=x1;x<=x2;x++)
for(int y=y1;y<=y2;y++)
have[x][y]=1;
return;
}
if(opt=='e') return;
char c;
c=getchar();
getnum(c,((x1+x2)>>1)+1,y1,x2,(y1+y2)>>1);
c=getchar();
getnum(c,x1,y1,(x1+x2)>>1,(y1+y2)>>1);
c=getchar();
getnum(c,x1,((y1+y2)>>1)+1,(x1+x2)>>1,y2);
c=getchar();
getnum(c,((x1+x2)>>1)+1,((y1+y2)>>1)+1,x2,y2);
}
int main()
{
T=0;
char c=getchar();
while(c>57||c<48) c=getchar();
while(c>47&&c<58) T*=10,T+=c-48,c=getchar();//读入T
while(T--)
{
memset(have,0,sizeof(have));
for(int nowtimes=1;nowtimes<=2;nowtimes++)
{
c=getchar();
getnum(c,1,1,32,32);
c=getchar();
getnum(c,1,1,32,32);
}
int ans=0;
for(int x=1;x<=32;x++)
for(int y=1;y<=32;y++)
if(have[x][y]) ans++;
printf("There are %d black pixels.\n",ans);
}
}