题目大意:两个四叉树相加可以表示一条长为1024的图像像素,四叉树的叶子节点有1024个,两个四叉树相加的方式为,对应的叶子节点有任一为黑,则为黑。每个像素可以是黑色或白色,计算这图像的黑色个数。
解题思路:字符串代表四叉树,如:ppeeefpffeefe,第一个为p,所以,分支四个,第一个分支为peeef,第二个分支为pffee,第三个分支为f,第四个分支为e。对于每个分支也是同样的判断往下。当某个分支第一项为f或e时,就不用往下分了。因为他的下分支颜色都确定了。然后将最终分的结果传给字符串,两个字符串遍历一次,就可以计算黑色个数了。用dfs(还是叫递归)计算出长度为1024的字符串模拟出这叶子节点。然后遍历。
ac代码:
#include <iostream>
#include <cstring>
using namespace std;
char kk[1024], ll[1024];
void dfs(string &a, char b[], int m, int n)
{
int len=a.size(), count=0, tt;
string temp[4];
for (int i=0; i<4; i++)
temp[i] = "";
if (a[0] == 'e'){
for (int i=m; i<n; i++)
b[i] = 'e';
b[n] = '\0';
return ;
}
else if (a[0] == 'f'){
for (int i=m; i<n; i++)
b[i] = 'f';
b[n] = '\0';
return ;
}
else if (a[0] == 'p'){
for (int i=1; i<len; i++){
tt = i;
for (int j=i; j<tt+1; j++,i++){
temp[count] += a[j];
if (a[j] == 'p')
tt += 4;
}
i--;
count++;
}
dfs(temp[0], b, m, m+(n-m)/4);
dfs(temp[1], b, m+(n-m)/4, m+(n-m)/2);
dfs(temp[2], b, m+(n-m)/2, m+3*(n-m)/4);
dfs(temp[3], b, m+3*(n-m)/4, n);
}
return ;
}
int main()
{
int n, sum;
string a, b, c, d;
scanf("%d", &n);
while (n--){
sum = 0;
cin >> a >> b;
dfs(a, kk, 0, 1024);
dfs(b, ll, 0, 1024);
for (int i=0; i<1024; i++)
if (kk[i] == 'f' || ll[i] == 'f')
sum++;
printf("There are %d black pixels.\n", sum);
}
return 0;
}