TopcoderArchive
Archive
Algorithm problem

Trim

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given a String[], pixels, containing a list of black pixels you must draw on a 50 x 50 grid that is initially completely white. Each element of pixels is formated as "r c" (quotes for clarity only), where r is the row and c is the column (both 0-based) of a single pixel. Once all the pixels have been drawn, trim the grid and return it as a String[].

The trim procedure removes all outer rows and columns that contain no black pixels. In other words, determine the left-most column that contains at least one black pixel and remove all columns to the left of that column, determine the top-most row that contains at least one black pixel and remove all rows above that row, etc... Black pixels should be represented in the return String[] as upper case 'X' characters, and white pixels should be represented as '.' characters. The jth character of the ith element of the String[] is the pixel at row i, column j.

Definition

Class:
Trim
Method:
trim
Parameters:
String[]
Returns:
String[]
Method signature:
String[] trim(String[] pixels)
(be sure your method is public)

Constraints

  • pixels contains between 1 and 50 elements, inclusive.
  • Each element of pixels is formatted as "r c" (quotes for clarity only), where r and c are integers between 0 and 49, inclusive, with no extra leading zeros.

Examples

  1. {"4 4", "14 14", "4 14", "14 4"}
    Returns: {"X.........X", "...........", "...........", "...........", "...........", "...........", "...........", "...........", "...........", "...........", "X.........X" }
  2. {"4 4", "14 14"}
    Returns: {"X..........", "...........", "...........", "...........", "...........", "...........", "...........", "...........", "...........", "...........", "..........X" }
  3. {"4 4"}
    Returns: {"X" }
  4. {"4 4", "4 14", "4 4"}
    Returns: {"X.........X" }
    Duplicate pixels are allowed.
Back to all problems