# uva 297

P表示的是他们还可以分成四个小格子（节点下有四个子女），e表示为空，f表示满，就是被涂黑了。。。

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
//最多有六层  共有1365个
struct Node
{
int level;
int flag;
Node * son[4];
Node()
{
flag = -1;
level = 1;
}
};
int sum=0;
char s1[10005], s2[10005];
char *p;
Node * build(int level)
{
Node* t = (Node*)malloc(sizeof(Node));
if (*p == 'p')
{
t->flag = 0;
t->level=++level;
//      for (int i = 0; i < 4; i++)
//      {
++p;t->son[0] = build(t->level);
++p;t->son[1] = build(t->level);
++p; t->son[2] = build(t->level);
++p; t->son[3] = build(t->level);
//      }
t->level--;
}
else if (*p == 'e')
{
t->flag = 1;
t->level = level;
for (int i = 0; i < 4; i++)
{
t->son[i] = NULL;
}
}
else if (*p == 'f')
{
t->flag = 2;
t->level = level;
for (int i = 0; i < 4; i++)
{
t->son[i] = NULL;
}
}
return t;
}
int  DFS(Node *t1, int sum)
{

if (!t1) return sum;
if (t1->flag == 2)
{
sum = sum + pow(4, 6 - t1->level);
return sum;
}
return sum;
}
void dfs(Node *t1,Node *t2)
{
if (t1 == NULL && t2 == NULL)
return;
if (t1->flag == 1 && t2->flag == 1)
return;
if (t1->flag == 2 || t2->flag == 2) // 两个都是f
{
sum = sum + pow(4, 6 - t1->level);
return;
}
if (t1->flag == 0  && t2->flag ==0) //两个都是P
{
dfs(t1->son[0], t2->son[0]);
dfs(t1->son[1], t2->son[1]);
dfs(t1->son[2], t2->son[2]);
dfs(t1->son[3], t2->son[3]);
}
if (t1->flag == 0 && t2->flag == 1) //一个e一个P
{
sum=DFS(t1->son[0], sum);
sum = DFS(t1->son[1], sum);
sum = DFS(t1->son[2], sum);
sum = DFS(t1->son[3], sum);
}
else if (t2->flag == 0 && t1->flag == 1)
{
sum = DFS(t2->son[0], sum);
sum = DFS(t2->son[1], sum);
sum = DFS(t2->son[2], sum);
sum = DFS(t2->son[3], sum);
}

}
int main()
{
int n;
cin >> n;
getchar();
while (n--)
{
Node * root1 =new Node();
Node *root2 = new Node();
cin >> s1;
p = s1;
root1=build(root1->level);
cin >> s2;
p = s2;
root2=build(root2->level);
sum = 0;
dfs(root1,root2);
printf("There are %d black pixels.\n",sum);
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：uva 297 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)