TopCoder problem "TopCraft" used in TCI '02 Semifinals 3 (Division I Level Three)



Problem Statement

    

703 Gaming, inc. has released a new game called TopCraft. In this game, an army of coding professionals must attack the evil bugs infesting the world. These coding professionals (referred to in game terminology as "units") are classified as either division 1 or division 2 units.

In order to control units, the player must drag a selection rectangle across the screen. Any units encompassed by the rectangle get selected for control.

Stan Ford has just bought this game to kill time. Now he's stuck in a position where he has to attack the evil bugs as fast as he can. To accomplish this, he wants to select all of the division 1 units on his screen with as few selection rectangles as possible, but without selecting any division 2 units in the process. Write a method that, given the screen configuration, returns the minimum number of selection rectangles necessary to select all of the division 1 units.

The screen will be represented by a String[] with 10 elements, each of length 10. Each character will be either a '.' for a clear space, a '1' for a division 1 unit, or a '2' for a division 2 unit.

 

Definition

    
Class:TopCraft
Method:select
Parameters:String[]
Returns:int
Method signature:int select(String[] select)
(be sure your method is public)
    
 

Notes

-The same division 1 unit may be selected multiple times, if that leads to the least number of selection rectangles.
 

Constraints

-units will contain 10 elements exactly.
-each element of units will be 10 characters in length, exactly.
-each element of units will be composed of the three characters '.', '1', and '2'.
-there will be at least one occurence of '1' and at least one occurence of '2' in units.
-there will be no more than 20 total combined units.
 

Examples

0)
    
{".........."
,".........."
,".........."
,".1.1.1...."
,".........."
,".1.1.1...."
,".......2.."
,".1.1.1...."
,"........1."
,".........."}
Returns: 2
A selection rectangle can be drawn around the block of 9 1s, and then another one around only the isolated 1:
..........
..........
|-----|...
|1.1.1|...
|.....|...
|1.1.1|...
|.....|2..
|1.1.1||-|
|-----||1|
.......|-|
1)
    
{".........."
,".........."
,".........."
,"..111....."
,"..121....."
,"..111....."
,".........."
,".........."
,".........."
,".........."}
Returns: 4
2)
    
{".........."
,".1....1..."
,"..2......."
,"...1.1...."
,"....2....1"
,".....1..2."
,"..1......."
,".......1.."
,".2........"
,"....111..."}
Returns: 4

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4372&pm=648

Writer:

chogyonim

Testers:

alexcchan , lbackstrom

Problem categories:

Recursion, Search