- 题目中给出的四分树一定是“满叶”的,因此知道了先序遍历就能确定这棵树,边读入边建图即可;
- 统计答案时同时遍历两棵树;
- 注意在计算答案的过程中可能会出现一颗树的当前节点已经不存在的情况,同样要作为边界处理;
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int maxn=32*32;
struct node{
int a,b,c,d;
char col;
};
node tree1[2055],tree2[2055];
char c;
int dat,cnt=1,ans;
void build(struct node tr[],int root)
{
c=getchar();
tr[root].col=c;
if(c=='p')
{
tr[root].a=++cnt;
build(tr,cnt);
tr[root].b=++cnt;
build(tr,cnt);
tr[root].c=++cnt;
build(tr,cnt);
tr[root].d=++cnt;
build(tr,cnt);
}
}
void dfs(int p1,int p2,int tmp)
{
if(tree1[p1].col=='f'||tree2[p2].col=='f'||tree1[p1].col=='e'&&tree2[p2].col=='e'||p1==0||p2==0)
{
ans+=((tree1[p1].col=='f'||tree2[p2].col=='f')?maxn/tmp:0);
return ;
}
dfs(tree1[p1].a,tree2[p2].a,tmp*4);
dfs(tree1[p1].b,tree2[p2].b,tmp*4);
dfs(tree1[p1].c,tree2[p2].c,tmp*4);
dfs(tree1[p1].d,tree2[p2].d,tmp*4);
}
void intt()
{
for(int i=1;i<=1025;i++)
tree1[i].a=tree1[i].b=tree1[i].c=tree1[i].d=tree2[i].a=tree2[i].b=tree2[i].c=tree2[i].d=0;
ans=0;cnt=1;
}
int main()
{
cin>>dat;
while(dat--)
{
getchar();
build(tree1,1);
getchar();
cnt=1;
build(tree2,1);
dfs(1,1,1);
cout<<"There are "<<ans<<" black pixels."<<endl;
intt();
}
return 0;
}