297 - Quadtrees

本文介绍了一种利用四分树数据结构来表示图像,并通过先序遍历构建树的方法。重点阐述了如何计算两个图像在进行加法操作后,黑色像素的数量。通过构建树和遍历算法,可以高效地得出最终图像的黑色像素总数。

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;

                }

            }

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值