码迷,mamicode.com
首页 > 其他好文 > 详细

Quadtrees--四叉树

时间:2015-03-09 20:48:49      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

Description

技术分享

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

3
ppeeefpffeefe
pefepeefe
peeef
peefe
peeef
peepefefe

Example Output

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


 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <list>
13 #include <iomanip>
14 #include <cstdlib>
15 #include <sstream>
16 using namespace std;
17 const int INF=0x5fffffff;
18 const double EXP=1e-8;
19 const int mod=100000007;
20 const int MS=1500;
21 const int len=32;
22 char str[MS];
23 int buf[len][len];
24 int ans;
25 //2 1
26 //3 4
27 
28 void draw(const char *s,int &p,int r,int c,int w)
29 {
30     char ch=s[p++];
31     if(ch==p)
32     {
33         draw(s,p,r,c+w/2,w/2);  //1
34         draw(s,p,r,c,w/2);      //2
35         draw(s,p,r+w/2,c,w/2);  //3
36         draw(s,p,r+w/2,c+w/2,w/2);//4
37     }
38     else if(ch==f)  //处理黑色像素
39     {
40         for(int i=r;i<r+w;i++)
41             for(int j=c;j<c+w;j++)
42         {
43             if(buf[i][j]==0)   //两个图像的相同的位置都是黑色像素的话,
44             {                  //是算一个的,buf其标记的作用
45                 buf[i][j]=1;
46                 ans++;
47             }
48         }
49     }
50 }
51 
52 int main()
53 {
54     int T;
55     scanf("%d",&T);
56     while(T--)
57     {
58         memset(buf,0,sizeof(buf));
59         ans=0;
60         int p=0;
61         scanf("%s",str);
62         draw(str,p,0,0,len);
63         scanf("%s",str);
64         p=0;
65         draw(str,p,0,0,len);
66         printf("There are %d black pixels.\n",ans);
67     }
68     return 0;
69 }

 

 

Quadtrees--四叉树

标签:

原文地址:http://www.cnblogs.com/767355675hutaishi/p/4324347.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!