uva 297

//这题用的是最笨的方法,建树然后合并,然后计算
//最开始建树都有点问题,最开始用的是指针指向字符串,通过指针一个一个建树,可是怎么都过不了,于是就参考网上直接传数组,求大神解释一下啊
//还有一个问题就是 最后数据都过了总是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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值