297 - Quadtrees

来源:互联网 发布:烟台网络党校 编辑:程序博客网 时间:2024/04/29 17:37

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

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

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

This particularartist believes in what he calls the preferred fullness: for animage to be interesting (i.e. to sell for big bucks) the most importantproperty is the number of filled (black) pixels in the image. So, before addingtwo images together, he would like to know how many pixels will be black in theresulting image. Your job is to write a program that, given the quadtreerepresentation of two images, calculates the number of pixels that are black inthe image, which is the result of adding the two images together.

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

Input Specification

The first line ofinput specifies the number of test cases (N) your program has toprocess.

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

Output Specification

For each testcase, 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

Thereare 640 black pixels.

There are 512black pixels.

There are 384black pixels.

代码:

/*

题意:给两棵四分树的先序遍历,求二者合并之后(黑色部分合并)黑色像素的个数。

 p表示中间结点,f表示黑色(full),e表示白色(empty)

算法:先建树,然后统计

*/

#include<iostream>

#include<cstring>

using namespacestd;

 

int num;

int buf[32][32];

 

void draw(strings,int & p,intr,int c,int w);

//把字符串s[p..]导出到以(r,c)为左上角,边长为w的缓冲区中

 

int main()

{

    int test;

    cin>>test;

    while(test--)

    {

        memset(buf,0,sizeof(buf));

        num=0;

        string s;

        for(int i=0;i<2;i++)

        {

            int p=0;

            cin>>s;

            draw(s,p,0,0,32);

        }

        cout<<"There are"<<num<<" black pixels.\n";

    }

}

 

void draw(strings,int & p,int r,int c,int w)

{

    char ch=s[p++];

    if(ch=='p')

    {

        draw(s,p,r,c+w/2,w/2);

        draw(s,p,r,c,w/2);

        draw(s,p,r+w/2,c,w/2);

        draw(s,p,r+w/2,c+w/2,w/2);

    }

    else if(ch=='f')//  画黑像素(白像素不画)

    {

        for(int i=r;i<r+w;i++)

        {

            for(int j=c;j<c+w;j++)

            {

                if(buf[i][j]==0)

                {

                    num++;

                    buf[i][j]=1;

                }

            }

        }

    }

}

0 0
原创粉丝点击