TopCoder problem "FloorIndicator" used in SRM 421 (Division II Level Three)



Problem Statement

    Imagine a skyscraper with 10^N floors, numbered 0 to 10^N-1. The floor indicator in the elevator shows the floor number using exactly N digits, padding it with leading zeroes if necessary. Each digit is shown as a 5x3 field of small lamps, some of which are lit and some of which are unlit. Here is a representation of all the digits from 0 to 9 ('#' represents lit lamps and '.' represents unlit lamps) :

###...#.###.###.#.#.###.###.###.###.###

#.#...#...#...#.#.#.#...#.....#.#.#.#.#

#.#...#.###.###.###.###.###...#.###.###

#.#...#.#.....#...#...#.#.#...#.#.#...#

###...#.###.###...#.###.###...#.###.###



Here, as in the actual floor indicator, consecutive digits are separated by a single column of unlit lamps. Some of the lamps in the floor indicator are malfunctioning and always remain unlit. Imagine that you are stuck in this elevator and you want to know the current floor number. You decide to calculate it as the average value of all floor numbers that could possibly be represented by the current state of the indicator, assuming that any number of the unlit lamps might be malfunctioning. You are given the state of the indicator as a String[] where each element represents a row of lamps, and rows are given from top to bottom. Return the average that you calculate, or -1 if no valid floor number could possibly be represented by the indicator.
 

Definition

    
Class:FloorIndicator
Method:averageFloor
Parameters:int, String[]
Returns:double
Method signature:double averageFloor(int N, String[] indicator)
(be sure your method is public)
    
 

Notes

-The returned value must be accurate to within a relative or absolute value of 1E-9.
 

Constraints

-N will be between 1 and 9, inclusive.
-indicator will contain exactly 5 elements.
-Each element of indicator will contain exactly 4*N - 1 characters.
-Each element of indicator will be either '#' or '.' (dot).
-All characters corresponding to lamps in separating columns will be '.' (dots).
 

Examples

0)
    
1
{"###",
 "#.#",
 "###",
 "#.#",
 "###"}
Returns: 8.0
This digit is clearly "8", so it is the only possible floor.
1)
    
2
{"###.###",
 "#.#.#.#",
 "#.#.###",
 "#.#...#",
 "###.###"}
Returns: 48.5
Although the indicator shows "09", both digits can also be partially lit "8", so the possible floor numbers are: 08,09,88,89. This results in average floor number (8+9+88+89)/4 = 48.5.
2)
    
2
{".......",
 ".......",
 ".......",
 ".......",
 "......."}
Returns: 49.5
This indicator is completely broken, so any floor number between 0 and 99 is possible.
3)
    
1
{"...",
 ".#.",
 "...",
 "...",
 "..."}
Returns: -1.0
No digit fits here.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=9871

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13512&pm=9871

Writer:

Alexus

Testers:

PabloGilberto , Olexiy , ivan_metelsky

Problem categories:

Simple Math, Simple Search, Iteration, String Parsing