• Uva 297 Quadtrees


     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.

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAXN 2000
    
    typedef struct quadrant{
        char value;
        struct quadrant *rightup, *leftup, *leftdown, *rightdown;
    }quadrant, *Quadtree;
    
    Quadtree leftTree = NULL, rightTree = NULL, temp = NULL;
    
    /*
    void print(Quadtree Tree)
    {
        if(Tree != NULL)
        {
            printf("%c", Tree->value);
            print(Tree->leftup);
            print(Tree->rightup);
            print(Tree->rightdown);
            print(Tree->leftdown);
        }
        
        return;
    }
    */
    
    void BuilTree(Quadtree& Tree, char *base, int& current, int len)
    {
        Tree = (Quadtree)malloc(sizeof(quadrant));
        Tree->value = base[current];
        Tree->rightdown = Tree->leftup = Tree->leftdown = Tree->rightup = NULL;
        if(base[current] == 'p')
        {
            BuilTree(Tree->leftup, base, ++current, len);
            BuilTree(Tree->rightup, base, ++current, len);
            BuilTree(Tree->rightdown, base, ++current, len);
            BuilTree(Tree->leftdown, base, ++current, len);
        }
        return;
    }
    
    void CompareTree(Quadtree left, Quadtree right, int& ans, int level)
    {
        switch(left->value)
        {
            case 'p' :
                {
                    switch(right->value)
                    {
                        case 'p' :
                            {
                                CompareTree(left->leftup, right->leftup, ans, level/2);
                                CompareTree(left->rightup, right->rightup, ans, level/2);
                                CompareTree(left->rightdown, right->rightdown, ans, level/2);
                                CompareTree(left->leftdown, right->leftdown, ans, level/2);
                                
                                break;
                            }
                        case 'f' :
                            {
                                ans += level*level;
                                break;
                            }
                        case 'e' :
                            {
                                CompareTree(left->leftup, temp, ans, level/2);
                                CompareTree(left->rightup, temp, ans, level/2);
                                CompareTree(left->rightdown, temp, ans, level/2);
                                CompareTree(left->leftdown, temp, ans, level/2);
                                
                                break;
                            }
                    }
                    break;        
                }
                
            case 'e' :
                {
                    switch(right->value)
                    {
                        case 'p' :
                            {
                                CompareTree(temp, right->leftup, ans, level/2);
                                CompareTree(temp, right->rightup, ans, level/2);
                                CompareTree(temp, right->rightdown, ans, level/2);
                                CompareTree(temp, right->leftdown, ans, level/2);
                                
                                break;
                            }
                        case 'f' :
                            {
                                ans += level*level;
                                break;
                            }
                        case 'e' :
                            {    
                                break;
                            }
                    }
                    break;
                }
            case 'f' :
            {
                ans += level*level;
                break;
            }
            
        }
        return;
    }
    
    void deleteTree(Quadtree& Tree)
    {
            if(Tree != NULL)
        {
            if(Tree->value == 'p')
            {
                deleteTree(Tree->leftup);
                deleteTree(Tree->rightup);
                deleteTree(Tree->rightdown);
                deleteTree(Tree->leftdown);
            }
            
            free(Tree);    
        }
        
        return;
    }
    
    int main()
    {
    
        char left[MAXN], right[MAXN];
        int T;
        temp = (Quadtree)malloc(sizeof(quadrant));
        temp->value = 'e', temp->rightdown = temp->leftup = temp->leftdown = temp->rightup = NULL;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%s%s", left, right);
            int leftlen = strlen(left), rightlen = strlen(right);
            if((leftlen == 1 && left[leftlen-1] == 'f') || (rightlen == 1 && right[rightlen-1] == 'f'))
            {
                printf("There are 1024 black pixels.\n");
                continue;
            }
    
            int current = 0;
            BuilTree(leftTree, left, current, leftlen);
            current = 0;
            BuilTree(rightTree, right, current, rightlen);
            int cnt = 0;
            CompareTree(leftTree, rightTree, cnt, 32);
            printf("There are %d black pixels.\n", cnt);
            deleteTree(leftTree);
            deleteTree(rightTree);
        }
        return 0;
    }

    解题思路:

    建树->两棵树对比->累加黑块的总权值

    PS:为了处理两棵树有不同层的情况,我采用的情况是在比较的过程中,将深度较小的那棵树的深度提到跟另外一棵一样的深度,方法是在递归的时候一直传递一个【empty】树节点

    runtime error (运行时错误)的几种情况
    ①除以零
    ②数组越界:int a[3]; a[10000000]=10;
    ③指针越界:int * p; p=(int *)malloc(5 * sizeof(int)); *(p+1000000)=10;
    ④使用已经释放的空间:int * p; p=(int *)malloc(5 * sizeof(int));free(p); *p=10;
    ⑤数组开得太大,超出了栈的范围,造成栈溢出:int a[100000000];

     这次出错的原因是:在递归比较树的时候一直都在创建一棵节点的空间

  • 相关阅读:
    css修炼宝典
    衡量优秀的卓越的前端工程师
    Bootstrap 快速人门案例——前端最火的插件
    前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)
    前端开发必学技能之一———非关系数据库又像关系数据库的MongoDB快速入门第一步下载与安装
    小米路由器未授权访问漏洞
    linux下更改ssh登录前的banner信息
    centos下编译安装Openssl
    S2-032代码执行
    SSRF漏洞学习
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2980048.html
Copyright © 2020-2023  润新知