This page is still under construction.

Parts of this page are still being built. What you see may change.

Quad Tree

Interview

Time limit1sMemory limit128 MB

Summary
Decode a quad tree string into an n by n black and white image, then print each row as XBM hexadecimal bytes.
Level

Medium5 of 10

Topics
Recursion, Divide and conquer, Implementation, Bit manipulation
Solved
No attempts yet

Problem

While searching for treasure at an ancient Aztec ruin, Hanshin found a papyrus scroll covered with a long text. The text was made of only three kinds of characters: BB, WW, and QQ.

Having studied a little cryptography, Hanshin recognized this as a famous 3000-year-old quad tree encoding.

Quad tree encoding compresses a picture (such as a treasure map) with the following rules:

  • If the whole picture is black, encode it as BB.
  • If the whole picture is white, encode it as WW.
  • If it contains both black and white parts, encode it as QxxxxQxxxx: recursively split the picture into four parts in the order top-left, top-right, bottom-left, bottom-right, and encode each part (xx) again.

Every picture is an n×nn \times n square of pixels where nn is a power of two, and it is always encoded as a perfect quad tree.

For example, a 2×22 \times 2 checkerboard is written as QWBBWQWBBW, and a 4×44 \times 4 checkerboard is written as QQWBBWQWBBWQWBBWQWBBWQQWBBWQWBBWQWBBWQWBBW.

Write a program that decodes such a quad tree string into an XBM-format file.

Input

The first line contains an integer nn (8≤n≤5128 \le n \le 512), the width and height of the picture in pixels; nn is always a power of two.

The second line contains a string of only BB, WW, and QQ that encodes the n×nn \times n picture as a quad tree.

Output

Print the XBM file contents in the following format:

  • Line 1: #define quadtree_width n, where nn is the width in pixels.
  • Line 2: #define quadtree_height n, where nn is the height in pixels.
  • Line 3: static char quadtree_bits[] = {
  • The next nn lines: each row of the picture converted into n/8n/8 hexadecimal values. Each hex value packs 8 pixels from left to right into an 8-bit number, where the leftmost pixel has bit value 1 and the rightmost pixel has bit value 128. A black pixel is 1 and a white pixel is 0. Write each value in the form 0xdd (two lowercase hex digits) and append a comma (,) after every value. For example, the 8 pixels WBBBBWWBWBBBBWWB give 2+4+8+16+128=1582 + 4 + 8 + 16 + 128 = 158 = 0x9e.
  • Last line: };

Examples3

  1. Example 1

    Input
    16
    QQWBBWQWBBWQWBBWQWBBW
    
    Expected output
    #define quadtree_width 16
    #define quadtree_height 16
    static char quadtree_bits[] = {
    0xf0,0xf0,
    0xf0,0xf0,
    0xf0,0xf0,
    0xf0,0xf0,
    0x0f,0x0f,
    0x0f,0x0f,
    0x0f,0x0f,
    0x0f,0x0f,
    0xf0,0xf0,
    0xf0,0xf0,
    0xf0,0xf0,
    0xf0,0xf0,
    0x0f,0x0f,
    0x0f,0x0f,
    0x0f,0x0f,
    0x0f,0x0f,
    };
    
  2. Example 2

    Input
    8
    B
    
    Expected output
    #define quadtree_width 8
    #define quadtree_height 8
    static char quadtree_bits[] = {
    0xff,
    0xff,
    0xff,
    0xff,
    0xff,
    0xff,
    0xff,
    0xff,
    };
    
  3. Example 3

    Input
    8
    W
    
    Expected output
    #define quadtree_width 8
    #define quadtree_height 8
    static char quadtree_bits[] = {
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    };