//这题用的是最笨的方法,建树然后合并,然后计算
//最开始建树都有点问题,最开始用的是指针指向字符串,通过指针一个一个建树,可是怎么都过不了,于是就参考网上直接传数组,求大神解释一下啊
//还有一个问题就是 最后数据都过了总是runtime error,果然一般runtime error都是数组开小了,,,把数组从1000开到5000 终于AC!!!!!!!!
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct tree
{
char val;
struct tree* son[4];
}*Tree, T;
int sum, pixel, loc;
void dfs( Tree root, int pixel )
{
int i;
if( root->val == 'f' )
sum += pixel;
for( i = 0; i < 4; i++ )
if( root->son[i] )
{
pixel /= 4;
dfs( root->son[i], pixel );
pixel *= 4;
}
return ;
}
Tree newnode(char a[],int terminal)
{
int i;
Tree root;
root=(Tree)malloc(sizeof(T));
if(loc < terminal )
{
root->val=a[loc];
if(a[loc]=='p')
{
for( i = 0; i < 4; i++ )
{
++loc;
root->son[i] = newnode( a, terminal );
}
}
else
for( i = 0; i < 4; i++ )
{
root->son[i] = NULL;
}
}
return root;
}
Tree combine(Tree root1,Tree root2)
{
int i;
Tree root=(Tree)malloc(sizeof(T));
if(root1->val=='f'||root2->val=='f')
{
root->val='f';
for( i = 0; i < 4; i++ )
root->son[i] = NULL;
}
else if(root1->val=='e'&&root2->val=='e')
{
root->val='e';
for( i = 0; i < 4; i++ )
root->son[i] = NULL;
}
else
{
root->val='p';
if(root1->val=='p'&&root2->val=='p')
{
for( i = 0; i < 4; i++ )
root->son[i]=combine(root1->son[i],root2->son[i]);
}
else if(root1->val='p'&&root2->val=='e')
{
for( i = 0; i < 4; i++ )
root->son[i] = root1->son[i];
}
else
{
for( i = 0; i < 4; i++ )
root->son[i] = root2->son[i];
}
}
return root;
}
int main()
{
char s1[5000];
char s2[5000];
int len1, len2;
Tree root1, root2, root;
int N;
scanf( "%d", &N );
getchar();
while( N-- )
{
loc = 0;
sum = 0;
pixel = 1024;
scanf( "%s", s1 );
scanf( "%s", s2 );
root1 = newnode( s1, strlen(s1) );
loc = 0;
root2 = newnode( s2, strlen(s2) );
root = combine( root1, root2);
dfs( root, pixel );
printf( "There are %d black pixels.\n", sum );
}
return 0;
}
uva 297
最新推荐文章于 2022-08-31 14:01:54 发布