哎,太水了,递归建树写了好久的说,太弱了。。。
就是建个四叉树,然后统计占了多少像素,2Y,第一次犯2数组开小了送了个RE。。。
貌似还可以直接建成满四叉树,不过算晕了,没推出子节点咋算的。。真的太弱了= =
#include <stdio.h>
#include <string.h>
struct Node
{
int flag, child[4];
};
int ncase, sz, res;
char str1[2000], str2[2000];
Node treea[4500], treeb[4500];
void build(int left, int right, int fa, char *str, Node *tree)
{
for ( int i = left, k = 0 ; i < right && k < 4 ; i++, k++ )
{
int t = sz++;
if ( str[i] == 'p' )
{
int j, times=4;
for ( j = i+1 ; j < right && times ; j++, times-- )
if ( str[j] == 'p' )
times+=4;
tree[t].flag = 1;
tree[fa].child[k]=t;
build(i+1, j, t, str, tree);
i = j-1;
}
else
{
tree[t].flag = str[i] == 'e' ? 2 :3;
tree[fa].child[k]=t;
}
}
}
void init(char *str, Node *tree)
{
int len = strlen(str);
sz = 1;
build(0, len, 1, str, tree);
}
void dfs(Node *tree, int root, int deep)
{
if (tree[root].flag==2)
return;
if (tree[root].flag==3)
{
res+=1<<deep;
return;
}
if (tree[root].flag==1)
for ( int i = 0 ; i < 4 ; i++ )
dfs(tree, tree[root].child[i], deep-2);
}
void get(int root1, int root2, int deep)
{
if ( !root1 && !root2 )
return;
if (treea[root1].flag==3 || treeb[root2].flag==3)
res+=1<<deep;
if (treea[root1].flag==2||treeb[root2].flag==2)
treea[root1].flag==2 ? dfs(treea, root1, deep) : dfs(treeb, root2, deep);
if (treeb[root2].flag==1&&treea[root1].flag==1)
for ( int i = 0 ; i < 4 ; i++ )
get(treea[root1].child[i], treeb[root2].child[i], deep-2);
}
int main()
{
while ( EOF != scanf("%d", &ncase) )
{
memset(treea, 0, sizeof(treea));
memset(treeb, 0, sizeof(treeb));
scanf("%s %s", str1, str2);
init(str1, treea);
init(str2, treeb);
res = 0;
get(1,1,10);
printf("There are %d black pixels.\n", res);
}
return 0;
}