• uva 297 quadtrees——yhx


    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.

     1 #include<cstdio>
     2 #include<cstring>
     3 int map[40][40];
     4 void draw(int x,int y,int l)
     5 {
     6     int i,j,k,p,q;
     7     char c;
     8     scanf("%c",&c);
     9     if (c=='p')
    10     {
    11         draw(x,y+l/2,l/2);  //四分
    12         draw(x,y,l/2);
    13         draw(x+l/2,y,l/2);
    14         draw(x+l/2,y+l/2,l/2);
    15     }
    16     if (c=='f')
    17       for (i=x;i<=x+l-1;i++)    //注意减1
    18         for (j=y;j<=y+l-1;j++)
    19           map[i][j]=1;
    20 }
    21 int main()
    22 {
    23     int i,j,k,n,ans;
    24     char c;
    25     scanf("%d",&n);
    26     for (i=1;i<=n;i++)
    27     {
    28         scanf("%*c");
    29         memset(map,0,sizeof(map));  //清空
    30         draw(1,1,32);
    31         scanf("%*c");     //读入回车符
    32         draw(1,1,32);
    33         ans=0;
    34         for (j=1;j<=32;j++)
    35           for (k=1;k<=32;k++)
    36             if (map[j][k]) ans++;
    37         printf("There are %d black pixels.
    ",ans);
    38     }
    39 }

    其实和前两道题差不多,都是递归求树。

  • 相关阅读:
    7种jvm垃圾回收器,这次全部搞懂
    3分钟学会redis主从复制搭建,及原理
    一文搞懂什么是递归,程序员必会算法之一
    Redis持久化方式:RDB和AOF详解,对比
    jvm垃圾回收算法详解,看这一篇就够了(求点赞)
    Redis命令大全,满足你的日常工作,看这一篇就够了(求点赞)
    Java自定义异常为什么性能差(求点赞)阿里双十一的性能凶手之一
    jvm类加载器,类加载机制详解,看这一篇就够了(求点赞)
    show processlist 命令详解,MySQL优化看这一篇就够了
    asp.net中的post和get请求操作
  • 原文地址:https://www.cnblogs.com/AwesomeOrion/p/5271884.html
Copyright © 2020-2023  润新知