uva-297 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 tex2html_wrap_inline34 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.
题目大概意思:两棵树合并,求黑色的相素是多少。
思路:建树,P节点下分4个节点,其它的都只有一个节点,建完树遍历比较,最后计算黑色相素。
#include<iostream>

using namespace std;

struct node
{
    char data;
    node* child[4];
    node(char a):data(a)
    {
        for(int i=0; i<4; i++)child[i]=0;
    }
};
node* build(const string& a,int& index)
{
    node*root=new node(a[index]);
    if(a[index]=='p')
        for(int i=0; i<4; i++)
            root->child[i]=build(a,++index);
    return root;
}

void Union(node*&root1,node*&root2)
{
    if(root1||root2)
    {
        if(root2&&!root1)
        {
            root1=root2;
        }
        else if(root1&&root2)
        {
            if(root2->data=='f')
                root1=root2;
            else if(root1->data=='e'&&root2->data=='p')
                root1=root2;
        }
        for(int i=0; i<4; i++)
            Union(root1->child[i],root2->child[i]);
    }
}

node* combine(node* tree1 , node * tree2)
{
    if (tree1 == NULL)
    {
        return tree2;
    }
    else if (tree2 == NULL)
    {
        return tree1;
    }
    else if(tree1->data == 'f' || tree2->data == 'f')
    {
        tree1 = new node('f');
        return tree1;
    }
    if (tree2->data=='p')
    {
        tree1->data='p';
    }
    for(int i=0; i<4; i++)
    {
        tree1->child[i]=combine(tree1->child[i],tree2->child[i]);
    }
    return tree1;
}
void get_ans(node*root,int level,int&sum)
{
    if(root)
    {
        if(root->data=='f')
        {
            if(level==1)sum+=1024;
            else if(level==2)sum+=256;
            else if(level==3)sum+=64;
            else if(level==4)sum+=16;
            else if(level==5)sum+=4;
            else if(level==6)sum+=1;
        }
        for(int i=0; i<4; i++)
        {
            get_ans(root->child[i],level+1,sum);
        }
    }
}

int main()
{
    string a,b;
    int cas;
    cin>>cas;
    while(cas--)
    {
        cin>>a>>b;
        int index1=0,index2=0;
        node*root1=build(a,index1);
        node*root2=build(b,index2);
        root1=combine(root1,root2);
        int ans=0;
        get_ans(root1,1,ans);
        cout<<"There are "<<ans<<" black pixels."<<endl;
    }
    return 0;
}


原文地址:https://www.cnblogs.com/xinyuyuanm/p/3177837.html