TopcoderArchiveVisit Topcoder
Archive22 Aug 2006
TCCC06 Round 3

Diamonds

Problem statement, definition, constraints, and public examples.

Problem Statement

Given a rectangular grid consisting of the characters '#' and '.', find two non-overlapping diamond shapes among the '#' characters such that the sum of their radii is maximized. There is a diamond with radius r centered at xC, yC if all characters at position x, y (where |x - xC| + |y - yC| + 1 <= r) in the grid are '#'. The left grid below contains a diamond with radius 3. All the '#' characters in the grid are part of this diamond. The right grid contains a diamond with radius 2. It is centered at (2, 1) and contains five '#' characters (coordinates are zero-based).

{ "..#..",     { "###.",
  ".###.",       "####",
  "#####",       "..##"}
  ".###.",
  "..#.."}

Create a class Diamonds that contains the method maxRadiusSum that takes a String[] grid and returns an int, the maximum sum of the radii of two non-overlapping diamonds.

Definition

Class:
Diamonds
Method:
maxRadiusSum
Parameters:
String[]
Returns:
int
Method signature:
int maxRadiusSum(String[] grid)
(be sure your method is public)

Notes

  • A diamond can have radius 0. See example 4.

Constraints

  • grid will contain between 1 and 50 elements, inclusive.
  • Each element in grid will contain between 1 and 50 characters, inclusive.
  • All elements in grid will contain the same number of characters.
  • Each character in the elements in grid will be either a '#' or a '.'.

Examples

  1. { "..#..",
      ".###.",
      "#####",
      ".###.",
      "..#.."}
    Returns: 3
    Here we have one diamond with radius 3. We can either pick that diamond and an empty diamond, or a diamond with radius 2 (there are 5 of them) and another with radius 1 (a single '#'). In both cases, we end up with a sum of 3. Note that all radius 2 diamonds overlap with each other.
  2. { "..#..",
      ".###.",
      "#####",
      ".####",
      "..#.."}
    Returns: 4
    This is the grid from the previous example with one extra '#'. We should now choose the diamond with radius 3 and use the extra '#' as our second diamond (radius 1).
  3. {"...###..",
     "..#####.",
     ".#######",
     "#######.",
     "######..",
     "#####...",
     ".###....",
     "..#....."}
    Returns: 6
    The largest diamond has radius 4, but we should instead take two diamonds of radius 3 to get the maximum sum.
  4. {"##################################################",
     "#########.########################################",
     "#####..######################.####################",
     ".###.#######.#######.######################.###.##",
     "####.#.#####################.#####################",
     "##############.########.#########.################",
     "###################################.##############",
     "#################################..###############",
     "##########.#####################.#########.#######",
     "######.####.############.#########################",
     "##################.##############.################",
     "#############################.##.##.##############",
     "###########..###########.#.########.##############",
     "#.################.######.########################",
     "######.##########.##################.###########.#",
     "###################.########.#####################",
     "##################################################",
     "###############################.##.#######.#######",
     "######.###########################.##############.",
     "######################################.#.#########",
     "###########.#.####################.###########.###",
     "########.#################.#######################",
     "#######.#.####################.###################",
     "###.######################.#####################.#",
     "###################..####.#######.######.#########",
     "#.#################.##############################",
     "###################.#########.####################",
     "########.#########################################",
     "##################################.#############.#",
     "####.##############.###########################.##",
     "##################################.###.#####.#####",
     "###########.##.###########################.#######",
     "################.################.#######.######.#",
     "##################################################",
     "#######.##########################.###############",
     "#################################################.",
     "#########.##############..########################",
     "###.#########################################.####",
     "#####################################.###########.",
     "########################.###############.#.#######",
     "#################################################.",
     "##########################.#######.######.########",
     "##################.########.#.################.###",
     "######.######.###################.#.#.############",
     "####.#########.#########################.#########",
     "##################..#################.############",
     "#####.#.#######################.##.#############.#",
     "####.#############################.###############",
     "##########.########.##############.###.###########"}
    Returns: 14
  5. {".#"}
    Returns: 1
Back to all problems