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
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 가 있다 는 것 을 나타 낸다. 그 다음 에 왼쪽 위, 오른쪽 위, 왼쪽 아래, 오른쪽 아래 의 순서에 따라 이 네 개의 행렬 의 내용 을 제시한다.
사고: 4 분 나무의 사상, dfs 면 됩 니 다.
AC 코드 는 다음 과 같 습 니 다.
#include
#include
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);
printf("#define quadtree_height %d
",n);
printf("static char quadtree_bits[] = {
");
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<
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
POJ 2260 (ZOJ 1949) Error Correction 문제 하나A boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.