uva 297 quadtrees——yhx

Posted 报告振兴哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uva 297 quadtrees——yhx相关的知识,希望对你有一定的参考价值。

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.

 1 #include<cstdio>
 2 #include<cstring>
 3 int map[40][40];
 4 void draw(int x,int y,int l)
 5 {
 6     int i,j,k,p,q;
 7     char c;
 8     scanf("%c",&c);
 9     if (c==p)
10     {
11         draw(x,y+l/2,l/2);  //四分
12         draw(x,y,l/2);
13         draw(x+l/2,y,l/2);
14         draw(x+l/2,y+l/2,l/2);
15     }
16     if (c==f)
17       for (i=x;i<=x+l-1;i++)    //注意减1
18         for (j=y;j<=y+l-1;j++)
19           map[i][j]=1;
20 }
21 int main()
22 {
23     int i,j,k,n,ans;
24     char c;
25     scanf("%d",&n);
26     for (i=1;i<=n;i++)
27     {
28         scanf("%*c");
29         memset(map,0,sizeof(map));  //清空
30         draw(1,1,32);
31         scanf("%*c");     //读入回车符
32         draw(1,1,32);
33         ans=0;
34         for (j=1;j<=32;j++)
35           for (k=1;k<=32;k++)
36             if (map[j][k]) ans++;
37         printf("There are %d black pixels.\n",ans);
38     }
39 }

其实和前两道题差不多,都是递归求树。

以上是关于uva 297 quadtrees——yhx的主要内容,如果未能解决你的问题,请参考以下文章

Quadtrees UVA - 297

UVA - 297 Quadtrees (四分树)

UVA297:Quadtrees(四分树)

紫书Quadtrees UVA - 297 四叉树涂色

uva 140 bandwidth (好题) ——yhx

uva 839 not so mobile——yhx