Shirlies
宁静专注认真的程序媛~

#include "stdio.h"
#include "stdlib.h"

typedef struct nodes
{
 char ch;
 struct nodes *child[4];
}node;

char s1[10000];
int sum;
int pos;

node* newnode()
{
 node* t=(node *)malloc(sizeof(node));
 if(t!=NULL)
 {
  t->child[0]=t->child[1]=t->child[2]=t->child[3]=NULL;
 }
 return t;
}

void build(node **p)//有点迷糊,为什么不可以用指针即void build(node **p,char *s),就是将数组传给指针*s,一变成那个就是错的,起初用的是那个指针,想不明白是哪里错了,网上的做法是直接数组,纠结啊,到底是什么机理啊!!
{
 node *t;
 int i;
 if(s1[pos]=='\0') return;
 
 if(s1[pos]=='p')
 {
  (*p)->ch=s1[pos];
  pos++;
  for(i=0;i<4;i++)
  {
   t=newnode();
   (*p)->child[i]=t;
   build(&t);
  }
 }
 else
  (*p)->ch=s1[pos++];

}

void join(node *r1,node *r2,int dep)
{
 int i;
 if(r1->ch=='p'&&r2->ch=='p')
 {
  for(i=0;i<4;i++)
  {
   join(r1->child[i],r2->child[i],dep>>2);
  }
 }
 else if(r1->ch=='p'&&r2->ch=='e')
 {
  for(i=0;i<4;i++)
  {
   join(r1->child[i],r2,dep>>2);
  }
 }
 else if(r1->ch=='e'&&r2->ch=='p')
 {
  for(i=0;i<4;i++)
  {
   join(r1,r2->child[i],dep>>2);
  }
 }else if(r1->ch=='f'||r2->ch=='f')
 {
  sum+=dep;
 }
}

int main()
{
 int t;
 node *root1,*root2;

 scanf("%d",&t);

 while(t--)
 {
  sum=0;
  root1=newnode();
  scanf("%s",&s1);
  pos=0;

  build(&root1);

  root2=newnode();
  scanf("%s",&s1);
  pos=0;

  build(&root2);

  join(root1,root2,1024);
  printf("There are %d black pixels.\n",sum);
 }

 return 0;
}


  


 

 

posted on 2012-02-04 13:06  Shirlies  阅读(232)  评论(0编辑  收藏  举报