TopCoder problem "WaterPressure" used in TCO '03 Round 3 (Division I Level Two)

Problem Statement

    You have been given a String[] layout containing a map of an underground cavern. The jth character of the ith element of layout will correspond to row i column j in the map. Water has begun flowing in at the upper lefthand corner (row 0, column 0), and will soon makes its way throughout the cavern. Each character in layout will either be a digit (0-9) or 'X' (except for the upper lefthand corner, which will always be '.', and can be thought of as water). An 'X' means that water will never penetrate the solid rock present in that area. A digit describes how much water pressure is required to penetrate that area. The water will travel according to the following rules:
  • 1) Each second, beginning with second 0, the total amount of water increases by 1 in the squares where water has reached.
  • 2) The total water pressure is equal to the total amount of water divided by the total number of squares the water has reached. This need not be an integral amount.
  • 3) At the end of each second, after the total amount of water has been incremented, each square adjacent to the water (not diagonally) is checked for strength. Every adjacent square, whose digit value is strictly smaller than the total water pressure, will become covered with water.
Above, the total amount of water, and total amount of water pressure are values corresponding to the map as a whole, and not individual squares. Note that the water will never leave the map. When calculating which squares become covered during a given second, all squares adjacent to water are considered simultaneously. In other words, one square getting flooded will not then change the pressure, and prevent another square from getting flooded that same second. In addition, only squares directly adjacent to the water may be flooded during any second. Before second 0 occurs, the square in the upperleft hand corner is already flooded, and the total water is 1. This implies, after the increment occurs during second 0, the total water will be 2, and the total water pressure will be 2. Your method will return the second during which the last square containing a digit is flooded. If this will never occur, return -1.


Method signature:int howLong(String[] layout)
(be sure your method is public)


-Total water pressure need not be an integer.


-layout will contain between 1 and 50 elements inclusive.
-Each element of layout will contain between 1 and 50 characters inclusive.
-Each element of layout will contain the same number of characters.
-The 0th character in the 0th element of layout will be (quotes for clarity) '.' . Every other character in layout will be a digit ('0'-'9') or 'X'.
-layout will contain at least one digit.


Returns: 0
During second 0 the total water will be incremented to 2. Thus, the total pressure is 2. This is clearly greater than 0, so both '0' squares will be flooded. Since no digit squares remain unflooded, your method returns 0.
Returns: 1
Once second 0 is complete:

Total water = 2

Total filled = 3

Total pressure = 2/3

Current layout ('.' denotes flooded):
During second 1 the final digit containing square is flooded.
Returns: 14
Returns: 71
Returns: -1
The 'X' blocks the digit containing squares. Since the water can never reach the '0's, your method returns -1.
Returns: 18566

Problem url:

Problem stats url:




Problem categories: