Quadtree - ZOJ 1955 四分树

Quadtree

Time Limit: 2 Seconds       Memory Limit: 65536 KB

While searching for treasures in an ancient Aztec ruin, Florida Jones (the brother of famous Indiana Jones) stumbles across a papyrus roll lettered with a long string of symbols. There are three different symbols occuring in the string which we will call B, W and Q here.

Being somewhat experienced in cryptography, Florida Jones recognizes the code immediately as the famous Quadtree Encoding Scheme that has been invented 3000 years ago.

With the Quadtree system, secret pictures (like treasure maps) were encoded in the following way: If the whole picture was black, it was encoded by the letter B. If it was completely white, it was encoded by W. If both colors were used (what was usually the case), it was encoded by Qxxxx where each x was a string that recursively encoded one quarter of the picture (in the order top left, top right, bottom left, bottom right). As the Aztecs always used quadratic pictures with n*n pixels where n was a power of two, this method always worked perfectly. 

A 2*2 chess board, for instance, would be encoded as QWBBW, a 4*4 chess board as QQWBBWQWBBWQWBBWQWBBW.

Your job is to decode the quadtree string and output the picture in the XBM format (see output specification).


Input

The input contains an integer n (8 <= n <= 512) on the first line, giving the size of the picture in pixels per row/column. n will always be a power of two. 

On the second line, a string consisting of the letters B, W and Q follows. The string encodes a picture with n*n pixels with the quadtree scheme.


Output

  • On the first line, print "#define quadtree_width n" where n is the picture size given in the input.
  • On the second line, print "#define quadtree_height n" accordingly.
  • On the third line, print "static char quadtree_bits[] = {".
  • Then, print n lines (each one encoding one pixel row of the picture) with n/8 hexadecimal numbers per line.
    Each hexadecimal number is composed of 8 bits that encode 8 pixels from left to right (where the leftmost bit has the value 1 and the rightmost bit has the value 128). The hexadecimal numbers should be printed in the form 0xdd where d is one character of the set { 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f }. 
    Example: The 8 pixels WBBBBWWB would be written as 0x9e. (2+4+8+16+128 = 158 = 0x9e) 
    Print a comma after each hexadecimal number.
  • On the last line, print "};".


Sample Input

16
QQWBBWQWBBWQWBBWQWBBW


Sample Output

Note: The comments (enclosed by /* and */) are not part of the output. They should help to explain the XBM format. 

#define quadtree_width 16
#define quadtree_height 16
static char quadtree_bits[] = {
0xf0,0xf0, /* WWWWBBBB WWWWBBBB */
0xf0,0xf0, /* WWWWBBBB WWWWBBBB */
0xf0,0xf0, /* WWWWBBBB WWWWBBBB */
0xf0,0xf0, /* WWWWBBBB WWWWBBBB */
0x0f,0x0f, /* BBBBWWWW BBBBWWWW */
0x0f,0x0f, /* BBBBWWWW BBBBWWWW */
0x0f,0x0f, /* BBBBWWWW BBBBWWWW */
0x0f,0x0f, /* BBBBWWWW BBBBWWWW */
0xf0,0xf0, /* WWWWBBBB WWWWBBBB */
0xf0,0xf0, /* WWWWBBBB WWWWBBBB */
0xf0,0xf0, /* WWWWBBBB WWWWBBBB */
0xf0,0xf0, /* WWWWBBBB WWWWBBBB */
0x0f,0x0f, /* BBBBWWWW BBBBWWWW */
0x0f,0x0f, /* BBBBWWWW BBBBWWWW */
0x0f,0x0f, /* BBBBWWWW BBBBWWWW */
0x0f,0x0f, /* BBBBWWWW BBBBWWWW */
};


题意:在一个n*n的矩阵里,用BWQ表示其内容,对于某一个矩阵,B表示其全是B,W表示其全是W,Q表示其WB都有,其后依照左上,右上,左下,右下的顺序给出这四个子矩阵的内容。

思路:四分树的思想,dfs即可。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
int n,tree[520][520],pos,len;
char s[1000010];
char trans[30]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
void dfs(int x,int y,int num)
{
    int i,j,k;
    char c=s[++pos];
    if(c=='Q')
    {
        num/=2;
        dfs(x,y,num);
        dfs(x,y+num,num);
        dfs(x+num,y,num);
        dfs(x+num,y+num,num);
    }
    else
    {
        if(c=='W')
          k=0;
        else
          k=1;
        for(i=x;i<=x+num;i++)
           for(j=y;j<=y+num;j++)
              tree[i][j]=k;
    }
}
void print()
{
    int i,j,k,ret;
    printf("#define quadtree_width %d\n",n);
    printf("#define quadtree_height %d\n",n);
    printf("static char quadtree_bits[] = {\n");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j+=8)
        {
            ret=0;
            for(k=0;k<8;k++)
               ret+=tree[i][j+k]*(1<<k);
            printf("0x%c%c,",trans[ret/16],trans[ret%16]);
        }
        printf("\n");
    }
    printf("};\n");
}
int main()
{
    int i,j,k;
    while(~scanf("%d",&n))
    {
        scanf("%s",s+1);
        len=strlen(s+1);
        pos=0;
        dfs(1,1,n);
        print();
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我可以回答这个问题。四叉是一种数据结构,它可以用于空间划分和查询。以下是一个简单的C语言代码实现: ```c #include <stdio.h> #include <stdlib.h> #define MAX_POINTS 10 typedef struct point { int x; int y; } Point; typedef struct quadtree { Point point; struct quadtree* nw; struct quadtree* ne; struct quadtree* sw; struct quadtree* se; } Quadtree; Quadtree* create_quadtree(int x1, int y1, int x2, int y2) { Quadtree* quadtree = (Quadtree*)malloc(sizeof(Quadtree)); quadtree->point.x = (x1 + x2) / 2; quadtree->point.y = (y1 + y2) / 2; quadtree->nw = NULL; quadtree->ne = NULL; quadtree->sw = NULL; quadtree->se = NULL; return quadtree; } void insert_point(Quadtree* quadtree, Point point) { if (quadtree == NULL) { return; } if (quadtree->nw == NULL && quadtree->ne == NULL && quadtree->sw == NULL && quadtree->se == NULL) { quadtree->point = point; return; } if (point.x < quadtree->point.x && point.y < quadtree->point.y) { if (quadtree->nw == NULL) { quadtree->nw = create_quadtree(0, 0, quadtree->point.x, quadtree->point.y); } insert_point(quadtree->nw, point); } else if (point.x >= quadtree->point.x && point.y < quadtree->point.y) { if (quadtree->ne == NULL) { quadtree->ne = create_quadtree(quadtree->point.x, 0, MAX_POINTS, quadtree->point.y); } insert_point(quadtree->ne, point); } else if (point.x < quadtree->point.x && point.y >= quadtree->point.y) { if (quadtree->sw == NULL) { quadtree->sw = create_quadtree(0, quadtree->point.y, quadtree->point.x, MAX_POINTS); } insert_point(quadtree->sw, point); } else { if (quadtree->se == NULL) { quadtree->se = create_quadtree(quadtree->point.x, quadtree->point.y, MAX_POINTS, MAX_POINTS); } insert_point(quadtree->se, point); } } void print_quadtree(Quadtree* quadtree) { if (quadtree == NULL) { return; } printf(&quot;(%d, %d)\n&quot;, quadtree->point.x, quadtree->point.y); print_quadtree(quadtree->nw); print_quadtree(quadtree->ne); print_quadtree(quadtree->sw); print_quadtree(quadtree->se); } int main() { Quadtree* quadtree = create_quadtree(0, 0, MAX_POINTS, MAX_POINTS); Point points[MAX_POINTS] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}, {13, 14}, {15, 16}, {17, 18}, {19, 20}}; for (int i = 0; i < MAX_POINTS; i++) { insert_point(quadtree, points[i]); } print_quadtree(quadtree); return 0; } ``` 这个代码实现了一个简单的四叉,可以插入点并打印整个四叉

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值