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 of 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
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 of 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 pixelsQuadtrees
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 of 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;}
- UVA297四叉树
- uva297-四叉树
- uva297(四叉树建立)
- [UVA297] Quadtrees 四叉树
- uva297
- UVA297
- uva297
- uva297
- UVA297
- UVA297建立完全四叉树统计叶子节点
- Quadtrees UVA297
- uva297 - Quadtrees
- UVA297 - Quadtrees
- 四分树--uva297
- UVA297 Quadtrees(递归!!!)
- Uva297——Quadtrees
- 【UVA297】四象树(BZOJ2664)
- uva297 Quadtrees (树的重建)
- mongodb优化
- 内核中socket address family的注册过程
- 用自己写的方法实现n!
- 镜像问题
- Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1) 指针内存问题
- uva297-四叉树
- HDU 4600
- poj3728 The merchant 倍增LCA
- HDU 4601
- poj3690 Constellations 字符串Hash
- 使用setContentView可以在Activity中动态切换显示的View
- HDU 4604
- HDU 4605
- 基于SUSE Linux做NFS文件挂载