Quadtree - ZOJ 1955 四分树

原创 2015年07月08日 16:34:33

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();
    }
}



ACM zoj 1955(四分树实现)

这是道方阵颜色分割的题目,因而可以用四分树来解决。其实这道题对我来说,难点在于将要分割的颜色提取出来,因为当你遇到Q时说明你要将后面的颜色分成四块,如果在这之中又出现Q的话,那你就要用同样的方法将Q后...
  • iqRocket
  • iqRocket
  • 2012年11月27日 15:31
  • 2440

ZOJ,PKU--训练题分类

ZOJ题目分类  初学者题:  1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 ...
  • shao1996
  • shao1996
  • 2016年05月27日 21:43
  • 709

poj1623 象限四分树

Squadtrees Time Limit: 10000MS   Memory Limit: 32768K Total Submissions: 389   Acc...
  • u012866104
  • u012866104
  • 2015年06月22日 14:21
  • 1077

UVA—297四分树(非二叉树)

题意: 用一个四分树来表示一个黑白图像,方法是用根节点来表示整个图像,然后再进行等分,按照从右上角的图像开始逆时针的顺序编 号,依次有四个节点,如果某子节点对应的区域是全黑或者全白,则直接用黑或者...
  • shao1996
  • shao1996
  • 2016年05月27日 21:27
  • 928

四叉树空间索引原理及其实现

                   今天依然在放假中,在此将以前在学校写的四叉树的东西拿出来和大家分享。 四叉树索引的基本思想是将地理空间递归划分为不同层次的树结构。它将已...
  • zhanghefu
  • zhanghefu
  • 2014年05月17日 09:02
  • 6755

2D空间中使用Quadtree四叉树进行碰撞检测优化

很多游戏中都需要使用碰撞检测算法检测两个物体的碰撞,但通常这些碰撞检测算法都很耗时很容易拖慢游戏的速度。这里我们学习一下使用四叉树来对碰撞检测进行优化,优化的根本是碰撞检测时跳过那些明显离得很远的物体...
  • cordova
  • cordova
  • 2016年12月22日 22:44
  • 2652

习题6-8 空间结构 UVa806

1.题目描述:点击打开链接 2.解题思路:本题考查四分树的遍历。本题实质上是已知一棵四分树求黑色结点的编号;已知黑色结点的编号反过来构造四分树。第一种情况相当于编码,我们用先序遍历解决。用encod...
  • u014800748
  • u014800748
  • 2015年04月04日 01:29
  • 1040

UVa 297.Quadtrees【非二叉树之四分树】【7月31】

Quadtrees A quadtree is a representation format used to encode images. The fundamental idea behin...
  • a995549572
  • a995549572
  • 2015年07月31日 16:17
  • 611

zoj3772--【水题】线段树区间查询+矩阵乘法

思路来源:http://blog.csdn.net/u013654696/article/details/23037407#comments 【思路不是自己的,但代码是自己敲的,由于伦家不懂】...
  • u011652573
  • u011652573
  • 2014年04月08日 21:24
  • 694

zoj-2112(主席树动态求区间第k小数)

总算是把动态求区间第k个数的算法看明白了。 在主席树的基础上,如果有修改操作,则要通过套树状数组来实现任意区间求第k小的问题。 刚开始看不明白什么意思,现在有一点理解。树状数组的每个元素是一个线段...
  • u014492306
  • u014492306
  • 2015年08月25日 19:30
  • 1255
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Quadtree - ZOJ 1955 四分树
举报原因:
原因补充:

(最多只允许输入30个字)