程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA 297 Quadtrees(四叉樹建樹、合並與遍歷)

UVA 297 Quadtrees(四叉樹建樹、合並與遍歷)

編輯:C++入門知識

K - Quadtrees
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Appoint description:

Description

Download as PDF


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.<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+VGhlIGlucHV0IGZvciBlYWNoIHRlc3QgY2FzZSBpcyB0d28gc3RyaW5ncywgZWFjaCBzdHJpbmcgb24gaXRzIG93biBsaW5lLiBUaGUgc3RyaW5nIGlzIHRoZSBwcmUtb3JkZXIgcmVwcmVzZW50YXRpb24gb2YgYSBxdWFkdHJlZSwgaW4gd2hpY2ggdGhlIGxldHRlciA="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,E,F來表示,P表示父節點,F表示黑色,E表示白色,整個圖的大小為1024。每一個子圖都能分成四個部分(當顏色不同的時候才需要劃分),現在要把兩個圖合並成一個圖,求合並後圖有多少黑色像素。


#include
#include
#include
int T;
char s1[2049],s2[2049];
struct quadtree
{
    int num;
    quadtree *next[4];
    quadtree()
    {
        num=0;
        for(int i=0; i<4; i++)next[i]=0;
    }
};
quadtree *build(char *s)///建樹
{
    quadtree *now=new quadtree;
    int len=strlen(s);
    if(s[0]!='p')
    {
        now->num=1;
        if(s[0]!='f')
        {
            delete now;
            now=NULL;
        }
        return now;
    }
    int up=4;///子樹數目
    int d=1;
    for(int i=1; d<=up&&inext[d-1]=build(s+i);
            int dx=0,dy=4;
            while(dxnext[d-1]=build(s+i);
        }
        d++;
    }
    return now;
}
quadtree *merge_(quadtree *p,quadtree *q)///合並
{
    if(p||q)
    {
        quadtree *root=new quadtree;
        if(p&&q)for(int i=0; i<4; i++)
            {
                if(p->num||q->num)
                {
                    root->num=1;    ///子樹已經全為黑色,不需要繼續遞歸
                    continue;
                }
                root->next[i]=merge_(p->next[i],q->next[i]);
            }
        else if(p==NULL&&q)for(int i=0; i<4; i++)
            {
                if(q->num)
                {
                    root->num=1;;    ///子樹已經全為黑色,不需要繼續遞歸
                    continue;
                }
                root->next[i]=merge_(NULL,q->next[i]);
            }
        else for(int i=0; i<4; i++)
            {
                if(p->num)
                {
                    root->num=1;;    ///子樹已經全為黑色,不需要繼續遞歸
                    continue;
                }
                root->next[i]=merge_(p->next[i],NULL);
            }
        return root;
    }
    return NULL;
}
int dfs(quadtree *p,int num)
{
    if(p==NULL)return 0;
    int sum=0;
    if(p->num)sum+=num;
    for(int i=0; i<4; i++)
    {
        sum+=dfs(p->next[i],num/4);
    }
    return sum;
}
int main()
{
    //freopen("in.txt","r",stdin);
    quadtree *root1,*root2,*root;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s%s",s1,s2);
        root=root1=root2=NULL;
        root1=build(s1);
        root2=build(s2);
        root=merge_(root1,root2);
        printf("There are %d black pixels.\n",dfs(root,1024));
    }
    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved