zoukankan      html  css  js  c++  java
  • 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 }

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

  • 相关阅读:
    Java实现Labeling Balls(拓扑排序的应用)
    Java实现Labeling Balls(拓扑排序的应用)
    Java实现Labeling Balls(拓扑排序的应用)
    Java实现Labeling Balls(拓扑排序的应用)
    string与QString转换(string既可以是utf8,也可以是gbk)
    Qt4.8.6详细安装步骤(使用了i686-4.8.2-release-posix-dwarf-rt_v3-rev3,手动设置gcc和gdb)非常清楚 good
    qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
    小试X64 inline HOOK,hook explorer.exe--->CreateProcessInternalW监视进程创建
    RtlAdjustPrivilege进程提权,权限ID对照表
    jQuery AJAX 网页无刷新上传示例
  • 原文地址:https://www.cnblogs.com/SBSOI/p/5575046.html
Copyright © 2011-2022 走看看