24h購物| | PChome| 登入
2013-12-05 16:33:23| 人氣1,873| 回應0 | 上一篇 | 下一篇

[UVA][影像四分樹] 874 - 2D Representations

推薦 0 收藏 0 轉貼0 訂閱站台

2D Representations

Background

   A 2D raster image is defined by a matrix of points or pixels. In a black and white raster image, each pixel (a matrix element) can be black (full) or white (empty). There are several methods known to represent a raster image. Two of them, are the "Quadtree" and the "Run Length code".
   In a Quadtree, the matrix is preferably square with a length that is a power of two. One image is represented following a recursive visit: the image is divided in four image cells (accordingly to the order shown in Figure 1) and each cell is evaluated to be Full, Empty, or Partial. When a Partial cell is found, it is recursively subdivided in four cells and the same principle is repeated again and again until the cell is completely Full or completely Empty. The structure of a quadtree is a tree of nodes and each node can have from zero to four descendents (see Figure 1).


Figure 1 - One image, the order of visit and the quadtree

   In the Run Length code, pixels are grouped in series of empty pixels, full pixels and empty pixels again, etc. In this way, the image can be represented as a series of numbers, being each number of pixels in a group (we can assume that the first group is composed of full pixels). Using the image of Figure 1 as an example, the run length code is: 0, 20, 4, 4, 9, 1, 1, 1, 4, 1, 1, 2, 4, 4, 4, 4 (considering the lower left pixel as the first one).

Problem

   Given an image in the form of a quadtree, the problem is to obtain the correspondent run length code. The image maximum size is 256*256 and the origin of coordinates is the lower left pixel.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

   The first line of the input contains the length of the image and the second line contains the number of nodes N to be considered (both in integer format). Each one of the following N lines describes a different node (in sequence, starting with node 1), with four fields separated by a space. Each field may be one character F (full) or E (empty) or a number that is the index of a descendent node.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

   One line with the run length code of the given image (integers separated by a space). Note that the first group is assumed to be Full.

Sample Input
1

8
5
E 2 F 3
E E F F
4 5 E E
F E E F
F E E E

Sample Output
0 20 4 4 9 1 1 1 4 1 1 2 4 4 4 4

 



題目保證邊一定是 2 的冪次方。
把影像四分樹還原後,在使用 run length 去編碼。

#include <stdio.h>
#include <string.h>
char node[65536][4][10];
char g[256][256];
void getQuadrant(int i, int lx, int ly, int len, int &x, int &y) {
    if(i == 0)
        x = lx, y = ly;
    else if(i == 1)
        x = lx, y = ly+len;
    else if(i == 2)
        x = lx+len, y = ly;
    else
        x = lx+len, y = ly+len;
}
void fill(int lx, int ly, int len, int nd) {
    int i, j, k, v, x, y;
    for(i = 0; i < 4; i++) {
        if(node[nd][i][0] == 'E')
            continue;
        if(node[nd][i][0] == 'F') {
            getQuadrant(i, lx, ly, len, x, y);
            for(j = x; j < x+len; j++)
                for(k = y; k < y+len; k++)
                    g[j][k] = 1;
        } else {
            sscanf(node[nd][i], "%d", &v);
            getQuadrant(i, lx, ly, len, x, y);
            fill(x, y, len/2, v);
        }
    }
}
int main() {
    int testcase;
    int i, j;
    scanf("%d", &testcase);
    while(testcase--) {
        int n, m;
        scanf("%d %d", &n, &m);
        for(i = 1; i <= m; i++)
            scanf("%s %s %s %s", node[i][0], node[i][1], node[i][2], node[i][3]);
        memset(g, 0, sizeof(g));
        fill(0, 0, n/2, 1);
        int color = 1, cnt = 0, f = 0;
        for(i = 0; i < n; i++) {
            for(j = 0; j < n; j++) {
                if(g[i][j] != color) {
                    if(f)    putchar(' ');
                    printf("%d", cnt);
                    color = !color;
                    cnt = 1;
                    f = 1;
                } else {
                    cnt++;
                }
            }
        }
        if(f)    putchar(' ');
        printf("%d\n", cnt);
        if(testcase)
            puts("");
    }
    return 0;
}

台長: Morris
人氣(1,873) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][LCA][Tarjan] 12238 - Ants Colony
此分類上一篇:[UVA][Easy] 12658 - Character Recognition

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文