TopcoderArchive
Archive
Algorithm problem

Fill

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a 20 x 20 pixel canvas containing only black and white colors. The canvas is given as a String[], canvas, where character j of element i (both 0-indexed) represents the pixel at (i, j). Black pixels are represented as uppercase 'X' characters and white pixels are represented as '.' characters. You are also given ints, r and c, representing the coordinates at which you will execute a flood fill command. The flood fill should color all pixels black that can be reached from (r, c) in the original canvas (including (r, c), which is guaranteed to be white originally). In this problem, a pixel can be reached if and only if it is a white pixel and there is a path of white pixels from (r, c) to that pixel consisting solely of steps in the four cardinal directions (up, down, left, right - no diagonals). Return the resulting canvas as a String[] in the same format as canvas.

Definition

Class:
Fill
Method:
fill
Parameters:
String[], int, int
Returns:
String[]
Method signature:
String[] fill(String[] canvas, int r, int c)
(be sure your method is public)

Constraints

  • canvas will contain exactly 20 elements.
  • Each element of canvas will contain exactly 20 characters.
  • Each character in each element of canvas will be either '.' or uppercase 'X'.
  • r and c will each be between 0 and 19, inclusive.
  • Character c of element r of canvas (both 0-indexed) will be '.'.

Examples

  1. {"XXXXXXXXXXXXXXXXXXXX",
     "XX................XX",
     "XX................XX",
     "XX................XX",
     "XX................XX",
     "XX................XX",
     "XXXXXXXXXXXXXXXXXXXX",
     "XX................XX",
     "XX................XX",
     "XX................XX",
     "XX................XX",
     "XX................XX",
     "XXXXXXXXXXXXXXXXXXXX",
     "XX................XX",
     "XX................XX",
     "XX................XX",
     "XX................XX",
     "XX................XX",
     "XX................XX",
     "XXXXXXXXXXXXXXXXXXXX" }
    1
    2
    Returns: {"XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XX................XX", "XX................XX", "XX................XX", "XX................XX", "XX................XX", "XXXXXXXXXXXXXXXXXXXX", "XX................XX", "XX................XX", "XX................XX", "XX................XX", "XX................XX", "XX................XX", "XXXXXXXXXXXXXXXXXXXX" }
  2. {"XXXXXXXXXXXXXXXXXXXX",
     "X.XXXXXXXXXXXXXXX.XX",
     "XX.XXXXXXXXXXXXX.XXX",
     "XXX.XXXXXXXXXXX.XXXX",
     "XXXX.XXXXXXXXX.XXXXX",
     "XXXXX.XXXXXXX.XXXXXX",
     "XXXXXX.XXXXX.XXXXXXX",
     "XXXXXXX.XXX.XXXXXXXX",
     "XXXXXXXX.X.XXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX",
     "XXXXXXXXX.XXXXXXXXXX" }
    15
    9
    Returns: {"XXXXXXXXXXXXXXXXXXXX", "X.XXXXXXXXXXXXXXX.XX", "XX.XXXXXXXXXXXXX.XXX", "XXX.XXXXXXXXXXX.XXXX", "XXXX.XXXXXXXXX.XXXXX", "XXXXX.XXXXXXX.XXXXXX", "XXXXXX.XXXXX.XXXXXXX", "XXXXXXX.XXX.XXXXXXXX", "XXXXXXXX.X.XXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXX" }
Back to all problems