uva297-四叉树

来源:互联网 发布:南风知我意txt下载 编辑:程序博客网 时间:2024/06/05 11:34



 Quadtrees 

A quadtree is a representation format used to encode images. The fundamental idea behind the quadtree is that any image can be split into four quadrants. Each quadrant may again be split in four sub quadrants, etc. In the quadtree, the image is represented by a parent node, while the four quadrants are represented by four child nodes, in a predetermined order.

Of course, if the whole image is a single color, it can be represented by a quadtree consisting of a single node. In general, a quadrant needs only to be subdivided if it consists of pixels of different colors. As a result, the quadtree need not be of uniform depth.

A modern computer artist works with black-and-white images oftex2html_wrap_inline34 units, for a total of 1024 pixels per image. One of the operations he performs is adding two images together, to form a new image. In the resulting image a pixel is black if it was black in at least one of the component images, otherwise it is white.

This particular artist believes in what he calls the preferred fullness: for an image to be interesting (i.e. to sell for big bucks) the most important property is the number of filled (black) pixels in the image. So, before adding two images together, he would like to know how many pixels will be black in the resulting image. Your job is to write a program that, given the quadtree representation of two images, calculates the number of pixels that are black in the image, which is the result of adding the two images together.

In the figure, the first example is shown (from top to bottom) as image, quadtree, pre-order string (defined below) and number of pixels. The quadrant numbering is shown at the top of the figure.

Input Specification

The first line of input specifies the number of test cases (N) your program has to process.

The input for each test case is two strings, each string on its own line. The string is the pre-order representation of a quadtree, in which the letter 'p' indicates a parent node, the letter 'f' (full) a black quadrant and the letter 'e' (empty) a white quadrant. It is guaranteed that each string represents a valid quadtree, while the depth of the tree is not more than 5 (because each pixel has only one color).

Output Specification

For each test case, print on one line the text 'There are X black pixels.', where X is the number of black pixels in the resulting image.

Example Input

3ppeeefpffeefepefepeefepeeefpeefepeeefpeepefefe

Example Output

There are 640 black pixels.There are 512 black pixels.There are 384 black pixels
 Quadtrees 

A quadtree is a representation format used to encode images. The fundamental idea behind the quadtree is that any image can be split into four quadrants. Each quadrant may again be split in four sub quadrants, etc. In the quadtree, the image is represented by a parent node, while the four quadrants are represented by four child nodes, in a predetermined order.

Of course, if the whole image is a single color, it can be represented by a quadtree consisting of a single node. In general, a quadrant needs only to be subdivided if it consists of pixels of different colors. As a result, the quadtree need not be of uniform depth.

A modern computer artist works with black-and-white images oftex2html_wrap_inline34 units, for a total of 1024 pixels per image. One of the operations he performs is adding two images together, to form a new image. In the resulting image a pixel is black if it was black in at least one of the component images, otherwise it is white.

This particular artist believes in what he calls the preferred fullness: for an image to be interesting (i.e. to sell for big bucks) the most important property is the number of filled (black) pixels in the image. So, before adding two images together, he would like to know how many pixels will be black in the resulting image. Your job is to write a program that, given the quadtree representation of two images, calculates the number of pixels that are black in the image, which is the result of adding the two images together.

In the figure, the first example is shown (from top to bottom) as image, quadtree, pre-order string (defined below) and number of pixels. The quadrant numbering is shown at the top of the figure.

Input Specification

The first line of input specifies the number of test cases (N) your program has to process.

The input for each test case is two strings, each string on its own line. The string is the pre-order representation of a quadtree, in which the letter 'p' indicates a parent node, the letter 'f' (full) a black quadrant and the letter 'e' (empty) a white quadrant. It is guaranteed that each string represents a valid quadtree, while the depth of the tree is not more than 5 (because each pixel has only one color).

Output Specification

For each test case, print on one line the text 'There are X black pixels.', where X is the number of black pixels in the resulting image.

Example Input

3ppeeefpffeefepefepeefepeeefpeefepeeefpeepefefe

Example Output

There are 640 black pixels.There are 512 black pixels.There are 384 black pixels
思路就是建立四叉树,看了别人的代码再写的,因为有思路但写不出来,心得就是注意递归变量的设置,全局或者局部要弄清楚不同,我因为设置成局部调试了一上午·····诶
#include<stdio.h>#include<stdlib.h>#include<string.h>char str1[2000],str2[2000];int sum=0,len;//len作为字符下标,设成全局是为了递归遍历,由于之前没设置全局一直错,一直错···· struct tree{char ch;struct tree*child[4];//4个儿子 };void init(struct tree*temp){     for(int i=0;i<4;i++)temp->child[i]=NULL;}struct tree*buildtree(struct tree*root,char*str){     if(len==strlen(str))return NULL;     root->ch=str[len++]; if(root->ch=='p'){     for(int i=0;i<4;i++){        root->child[i]=new tree,init(root->child[i]);root->child[i]=buildtree(root->child[i],str);    }  } return root;}void find_answer(struct tree*root1,struct tree*root2,int factor){if(root1==NULL&&root2==NULL)return;//父亲都是叶节点了,不用再查询了if(root1==NULL){//只有节点1为空          if(root2->ch=='p'){//节点2又为非叶节点,那么它肯定有四个儿子             for(int i=0;i<4;i++)   find_answer(root1,root2->child[i],factor+1);          } else{       if(root2->ch=='f')sum+=1024>>(factor*2);  return;  }}if(root2==NULL){//只有节点1为空          if(root1->ch=='p'){//节点2又为非叶节点,那么它肯定有四个儿子             for(int i=0;i<4;i++)   find_answer(root1->child[i],root2,factor+1);          } else{       if(root1->ch=='f')sum+=1024>>(factor*2);  return;  }}if(root1->ch=='f'||root2->ch=='f'){ sum+=1024>>(factor*2); return;}for(int i=0;i<4;i++)    find_answer(root1->child[i],root2->child[i],factor+1);}int main(){int t;struct tree*root1,*root2;//两个四叉树点 scanf("%d",&t);while(t--){scanf("%s%s",str1,str2);root1=new tree;root2=new tree;init(root1),init(root2);len=0,root1=buildtree(root1,str1);len=0,root2=buildtree(root2,str2);sum=0; find_answer(root1,root2,0);printf("There are %d black pixels.\n",sum);}return 0;}


原创粉丝点击