###### Quadtree - ZOJ 1955 四分树

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.

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 */
};

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;
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(四分树实现)

2012-11-27 15:31:03

2016-12-22 22:44:42

2016-12-23 14:10:17

2007-11-20 11:22:00

2012-08-20 23:45:15

2014-02-18 20:37:52

2014年02月18日 1.75MB 下载

#### ZOJ题目类型汇总

2017-02-18 21:45:48

#### ZOJ 题目 分类

2016-10-21 18:04:13

#### zoj 3732 Graph Reconstruction(构造)

2013-11-25 14:55:26