297 - Quadtrees (UVa)

时间:2022-10-11 04:17:45

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 297 - Quadtrees (UVa) 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.

297 - Quadtrees (UVa)

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.

题意:这道题的意思是有一块图片,总共分为1024块像素。然后每一个大正方形可以分为四块。以此类推,直到最后分为最小的1024块像素之一。

即每棵树如果有子节点,那么就会有4个子树。具体看一下上面的图就一目了然了。

那么这道题我看到四个儿子就退缩了。没有用树来做。

反正也就1024块方块,我就直接用模拟的形式将其染色。在输入的时候,利用栈的原理来做。就是只有非叶子才能入栈,然后要用变量来标记一下这是第几个儿子。

不过我看了网上其他人的代码,用树来做也不是很麻烦。不过做起来还是没有直接染色做的方便吧。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#define maxn 1024
using namespace std;
struct node
{
char ch;
int x;
}temp;
char ch;
int t,i,j,lz;
int a1[1200]= {0},a2[1200]= {0};
void input(int *a1)
{
stack<node> s;
temp.ch='p';
temp.x=0;
int bj=0;
s.push(temp);
while(cin>>ch)
{
if (ch=='p')
{
temp=s.top();
s.pop();
temp.x++;
s.push(temp);
temp.ch='p';
temp.x=0;
s.push(temp);
}
else
{
temp=s.top();
lz=maxn/pow(4,(s.size()-1));
if (ch=='f')
{
for (i=bj; i<bj+lz; i++)
a1[i]=1;
}
bj=bj+lz;//这个变量很重要,每次分块之后,不过染没染色,要移动一个模块。
s.pop();
temp.x++;
s.push(temp);
}
temp=s.top(); while(temp.x==4)
{
s.pop();
temp=s.top();
}
//cout<<temp.x<<endl;
if (s.size()==1) break;
}
s.pop();
}
int main ()
{
char s1[2000],s2[2000];
cin>>t;
while(t--)
{
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
input(a1);
input(a2);
int sum=0;
for (i=0; i<maxn; i++)
if (a1[i] || a2[i])
sum++;
printf("There are %d black pixels.\n",sum);
}
return 0;
}