TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

BlackAndWhite

Problem statement, definition, constraints, and public examples.

Problem Statement

A rectangular black-and-white image has been encoded for serial transmission in the format described below. Your task is to decode the image and return a String[] containing the rows of the image, from top to bottom. Each pixel is represented as 'B' if it is black, and 'W' if it is white.

The encoded image is a String image that satisfies the following grammar:

    <img> = 'B' | 'W' | 'H' <img> <img> | 'V' <img> <img>
Notice that the String contains only the characters 'B', 'W', 'H', and 'V'. The encoding is recursive. A 'B' indicates that the image is entirely black, and a 'W' indicates that the image is entirely white. An 'H' is followed first by an encoding of the upper half of the image, and then by an encoding of the lower half of the image. A 'V' is followed first by an encoding of the left half of the image, and then by an encoding of the right half of the image. If the image cannot be divided evenly in half, then the extra row or column goes in the upper half or the left half, respectively. For example, if we know that the image is 10 pixels wide and 6 pixels high, then the encoded String
   HBVHHWBVBWW
would be decoded as
   BBBBBBBBBB
   BBBBBBBBBB
   BBBBBBBBBB
   WWWWWWWWWW
   BBBBBWWWWW
   BBBWWWWWWW
The following annotations explain each character of the encoded string.
   H -- the 10-by-6 image is split into a 10-by-3 upper half and a 10-by-3 lower half 
     B -- the 10-by-3 upper half is all black
     V -- the 10-by-3 lower half is split into a 5-by-3 left half and a 5-by-3 right half
       H -- the 5-by-3 left half is split into a 5-by-2 upper half and a 5-by-1 lower half
         H -- the 5-by-2 upper half is split into a 5-by-1 upper half and a 5-by-1 lower half
           W -- the 5-by-1 upper half is all white 
           B -- the 5-by-1 lower half is all black
         V -- the 5-by-1 lower half is split into a 3-by-1 left half and a 2-by-1 right half
           B -- the 3-by-1 left half is all black
           W -- the 2-by-1 right half is all white
       W -- the 5-by-3 right half is all white
Notice that an image cannot be decoded without knowing the intended size of the resulting image. You will be given the intended width and height of the image, along with the encoded String image. You must return the decoded image as a String[].

Definition

Class:
BlackAndWhite
Method:
decode
Parameters:
int, int, String
Returns:
String[]
Method signature:
String[] decode(int width, int height, String image)
(be sure your method is public)

Constraints

  • width is between 1 and 50, inclusive.
  • height is between 1 and 50, inclusive.
  • image contains between 1 and 50 characters, inclusive.
  • image contains only the characters 'B', 'W', 'H', and 'V'.
  • image satisfies the grammar shown above.

Examples

  1. 10
    6
    "HBVHHWBVBWW"
    Returns: { "BBBBBBBBBB",  "BBBBBBBBBB",  "BBBBBBBBBB",  "WWWWWWWWWW",  "BBBBBWWWWW",  "BBBWWWWWWW" }
    The example above.
  2. 5
    4
    "W"
    Returns: { "WWWWW",  "WWWWW",  "WWWWW",  "WWWWW" }
  3. 2
    1
    "HBW"
    Returns: { "BB" }
    This image is split into a black rectangle and a white rectangle, where the white rectangle happens to be empty.
  4. 1
    1
    "HVHVHVHVHHHHVVVBBBBBBBBBBBBWWWW"
    Returns: { "B" }
Back to all problems