[UVA][四分] 806 - Spatial Structures@Morris' Blog|PChome Online 人新台
2013-03-27 16:26:37| 人1,158| 回0 | 上一篇 | 下一篇

[UVA][四分] 806 - Spatial Structures

0 收藏 0 0 站台


  Spatial Structures 

Computer graphics, image processing, and GIS (geographic information systems) all make use of a data structure called a quadtree. Quadtrees represent regional or block data efficiently and support efficient algorithms for operations like the union and intersection of images.

A quadtree for a black and white image is constructed by successively dividing the image into four equal quadrants. If all the pixels in a quadrant are the same color (all black or all white) the division process for that quadrant stops. Quadrants that contain both black and white pixels are subdivided into four equal quadrants and this process continues until each subquadrant consists of either all black or all white pixels. It is entirely possible that some subquadrants consist of a single pixel.

For example, using 0 for white and 1 for black, the region on the left below is represented by the matrix of zeros and ones in the middle. The matrix is divided into subquadrants as shown on the right where gray squares represent subquadrants that consist entirely of black pixels.

epsfbox{p806a.eps}

A quadtree is constructed from the block structure of an image. The root of the tree represents the entire array of pixels. Each non-leaf node of a quadtree has four children, corresponding to the four subquadrants of the region represented by the node. Leaf nodes represent regions that consist of pixels of the same color and thus are not subdivided. For example, the image shown above, with the block structure on the right, is represented by the quadtree below.

epsfbox{p806b.eps}

Leaf nodes are white if they correspond to a block of all white pixels, and black if they correspond to a block of all black pixels. In the tree, each leaf node is numbered corresponding to the block it represents in the diagram above. The branches of a non-leaf node are ordered from left-to-right as shown for the northwest, northeast, southwest, and southeast quadrants (or upper-left, upper-right, lower-left, lower-right).

A tree can be represented by a sequence of numbers representing the root-to-leaf paths of black nodes. Each path is a base five number constructed by labeling branches with 1, 2, 3, or 4 with NW = 1, NE = 2, SW = 3, SE = 4, and with the least significant digit of the base five number corresponding to the branch from the root. For example, the node labeled 4 has path NE, SW which is 325 (base 5) or 1710 (base 10); the node labeled 12 has path SW, SE or 435 = 2310 ; and the node labeled 15 has path SE, SW, NW or 1345 = 4410 . The entire tree is represented by the sequence of numbers (in base 10)

9 14 17 22 23 44 63 69 88 94 113

Write a program that converts images into root-to-leaf paths and converts root-to-leaf paths into images.

Input 

The input contains one or more images. Each image is square, and the data for an image starts with an integer span class="MATH">n, where | n| is the length of a side of the square (always a power of two, with | n| ≤ 64) followed by a representation of the image. A representation is either a sequence of n2 zeros and ones comprised of | n| lines of | n| digits per line, or the sequence of numbers that represent the root-to-leaf paths of each black node in the quadtree that represents the image.

If n is positive, the zero/one representation follows; if n is negative, the sequence of black node path numbers (in base 10) follows. The sequence is terminated by the number -1. A one-node tree that represents an all-black image is represented by the number 0. A one-node tree that represents an all-white image is represented by an empty sequence (no numbers).

The end of data is signaled by a value of 0 for n.

Output 

For each image in the input, first output the number of the image, as shown in the sample output. Then output the alternate form of the image.

If the image is represented by zeros and ones, the output consists of root-to-leaf paths of all black nodes in the quadtree that represents the image. The values should be base 10 representations of the base 5 path numbers, and the values should be printed in sorted order. If there are more than 12 black nodes, print a newline after every 12 nodes. The total number of black nodes should be printed after the path numbers.

If the image is represented by the root-to-leaf paths of black nodes, the output consists of an ASCII representation of the image with the character `.' used for white/zero and the character `*' used for black/one. There should be n characters per line for an n×n image.

Print a blank line between cases.

Sample Input 

8 00000000 00000000 00001111 00001111 00011111 00111111 00111100 00111000 -8 9 14 17 22 23 44 63 69 88 94 113 -1 2 00 00 -4 0 -1 0 

Sample Output 

Image 1 9 14 17 22 23 44 63 69 88 94 113 Total number of black nodes = 11 Image 2 ........ ........ ....**** ....**** ...***** ..****** ..****.. ..***... Image 3 Total number of black nodes = 0 Image 4 **** **** **** **** 



Miguel Revilla 2004-09-17

的出一直很糟糕。
首先,忘了看到一行最多只能印 12 字。

此外一很的地方,如果出是字的中隔行

如果出是片的中不隔

此外都是空一行。

太了。

#include <stdio.h>
#include <algorithm>
using namespace std;
char g[65][65];
struct Node {
    int wb; // 1 white 2 black 3 no
} ND[32767];
int dfs(int k, int lx, int rx, int ly, int ry) {
    int mx = (lx+rx)/2, my = (ly+ry)/2;
    if(lx == rx && ly == ry) {
        ND[k].wb = (g[lx][ly] == '0') ? 1 : 2;
        return ND[k].wb;
    }
    ND[k].wb = 0;
    ND[k].wb |= dfs(k*4-2, lx, mx, ly, my);
    ND[k].wb |= dfs(k*4-1, lx, mx, my+1, ry);
    ND[k].wb |= dfs(k*4, mx+1, rx, ly, my);
    ND[k].wb |= dfs(k*4+1, mx+1, rx, my+1, ry);
    return ND[k].wb;
}
void color(int k, int lx, int rx, int ly, int ry, long long num) {
    int mx = (lx+rx)/2, my = (ly+ry)/2;
    if(num == 0) {
        ND[k].wb = 2;
        int i, j;
        for(i = lx; i <= rx; i++)
            for(j = ly; j <= ry; j++)
                g[i][j] = '*';
        return;
    }
    long long v = num%5;
    if(v == 1)
        color(k*4-2, lx, mx, ly, my, num/5);
    else if(v == 2)
        color(k*4-1, lx, mx, my+1, ry, num/5);
    else if(v == 3)
        color(k*4, mx+1, rx, ly, my, num/5);
    else
        color(k*4+1, mx+1, rx, my+1, ry, num/5);
}
long long buf[32767], bidx;
void pdfs(int k, long long num, long long base) {
    if(ND[k].wb == 1 || ND[k].wb == 2) {
        if(ND[k].wb == 2)
            buf[bidx++] = num;
        return;
    }
    pdfs(k*4-2, num + base, base*5);
    pdfs(k*4-1, num + base*2, base*5);
    pdfs(k*4, num + base*3, base*5);
    pdfs(k*4+1, num + base*4, base*5);
}
int main() {
    int n, i, j;
    int cases = 0, lastop = 0;
    while(scanf("%d", &n) == 1 && n) {
        if(lastop == 1 && n > 0)
            puts("\n");
        if(lastop == 1 && n < 0)
            puts("");
        if(lastop == -1 && n > 0)
            puts("");
        if(n > 0) {
            lastop = 1;
            for(i = 0; i < n; i++)
                scanf("%s", &g[i]);
            dfs(1, 0, n-1, 0, n-1);
            bidx = 0;
            pdfs(1, 0, 1);
            sort(buf, buf+bidx);
            printf("Image %d\n", ++cases);
            if(bidx) {
                for(i = 0; i < bidx; i++) {
                    if(i%12)    putchar(' ');
                    printf("%lld", buf[i]);
                    if((i+1)%12 == 0)
                        puts("");
                }
                if(bidx%12)
                    puts("");
            }
            printf("Total number of black nodes = %d\n", bidx);
        } else {
            lastop = -1;
            n = -n;
            bidx = 0;
            while(scanf("%lld", &buf[bidx]) == 1 && buf[bidx] >= 0)
                bidx++;
            for(i = 0; i < n; i++)
                for(j = 0; j < n; j++)
                    g[i][j] = '.';
            for(i = 0; i < bidx; i++)
                color(1, 0, n-1, 0, n-1, buf[i]);
            printf("Image %d\n", ++cases);
            for(i = 0; i < n; i++) {
                g[i][n] = 0;
                puts(g[i]);
            }
        }
    }
    return 0;
}

台: Morris
人(1,158) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][何、交] 453 - Intersecting Circles
此分上一篇:[UVA][矩形集] 870 - Intersecting Rectangles

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86