TopCoder problem "MinimumTours" used in SRM 411 (Division I Level Three)



Problem Statement

    Little Bonnie has taken a vacation to Ha Long Bay. There are a few thousand stone islands in the bay, making it one of the most beautiful natural scenes in Vietnam. Bonnie is so impressed by this fact that she has decided to visit all of the islands.



She bought a map of all the islands in the bay. This map is given in the String[] islandMap, which is a two-dimensional matrix where each cell is either '.' or lowercase 'x'. '.' cells represent sea and 'x' cells represent land. Two cells are connected if they have a point in common, so each cell may connect to at most 8 other cells. An island is defined as a maximal connected group of 'x' cells. A trip between two islands is defined as a connected group of '.' cells where, for each of the two islands, there is at least one '.' cell in the trip which is connected to a cell in that island. If there is no such connected group of '.' cells between two islands, then there is no trip between them. Note that an island can be nested inside another island.



A tour is a sequence of islands where there is a trip between every pair of consecutive islands in the sequence. Little Bonnie wants to visit every island exactly once, and she wants to do this using the minimum possible number of tours. Return the number of tours she will have to take.
 

Definition

    
Class:MinimumTours
Method:getMinimumTours
Parameters:String[]
Returns:int
Method signature:int getMinimumTours(String[] islandMap)
(be sure your method is public)
    
 

Notes

-It is possible for a tour to have only one island.
-Bonnie cannot leave the mapped area at any time.
-It is assumed that Bonnie has another way to travel from the last island of one tour to the first island of another tour, so you don't have to take this into account when solving the problem.
 

Constraints

-islandMap will contain between 1 and 50 elements, inclusive.
-Each element of islandMap will contain between 1 and 50 characters, inclusive.
-Each element of islandMap will contain the same number of characters.
-Each character in islandMap will be either '.' or lowercase 'x'.
-There will be at least one island in the map.
 

Examples

0)
    
{
"..x..x..x..",
"..x..x..x..",
"..x..x..x..",
"..x..x..x.."
}
Returns: 1
Only one tour is needed. Bonnie can just go from the leftmost island through the middle one to the rightmost island. Or she can choose to go in the reverse way.
1)
    
{
"x....x....x",
".....x.....",
".....x.....",
".....x.....",
"xxxxxxxxxxx",
".....x.....",
".....x.....",
".....x.....",
"x....x....x"
}
Returns: 3
At least three tours are required to cover the five islands. One possible set of three tours is to go from the small island in the top-left corner through the big cross-shaped island to the top-right island, and each of the two other islands makes up a one-island tour.
2)
    
{
"x....x....x",
".....x.....",
".....x.....",
"....x.x....",
"xxxx...xxxx",
"....x.x....",
".....x.....",
".....x.....",
"x....x....x"
}
Returns: 1
There is always a trip between any two islands. So only one tour is enough to visit all five islands.
3)
    
{
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"x............................x",
"x..xxxxxxxxxx....xxxxxxxxxx..x",
"x..x........x....x........x..x",
"x..x..xxxx..x....x.xxxxxx.x..x",
"x..x........x....x.x....x.x..x",
"x..xxxxxxxxxx....x.x.x..x.x..x",
"x................x.x....x.x..x",
"x................x.xxxxxx.x..x",
"x..xxxxxxxxxx....x........x..x",
"x..x........x....x........x..x",
"x..x..xxxx..x....x.xxxxxx.x..x",
"x..x........x....x........x..x",
"x..xxxxxxxxxx....xxxxxxxxxx..x",
"x............................x",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
Returns: 2
An island can be nested inside another one. And two tours are needed in this example.
4)
    
{
"xxxxxxxxxxxxx....xxxxxxxxxxxxxxxxxxxxxx",
"x...........x....x.....................",
"x.xxxxxxxxx.x....x.xxxxxxxxxxxxxxxxxxx.",
"x.x.......x.x....x.x.................x.",
"x.x.xxxxx.x.x....x.x.xxxxxxxxxxx.....x.",
"x.x...x...x.x....x.x.x..........x....x.",
"x.x...x...x.x....x.x.x.xxx..xxx...x..x.",
"x.x.......x.x....x.x.x.x....x..x..x..x.",
"x.xxxxxxxxx.x....x.x.x.xxx..x..x..x..x.",
"x...........x....x.x.x.x....x..x..x..x.",
"xxxxxxxxxxxxx....x.x.x.xxx..xxx...x..x.",
"x................x.x.x......xx....x..x.",
"x................x.x.x......x.x...x..x.",
"x................x.x.x......x..x..x..x.",
"x................x.x.x...........x...x.",
"x................x.x.xxxxxxxxxxxx....x.",
"x................x.x.................x.",
"x................x.xxxxxxxxxxxxxxxxxxx.",
"x................x.....................",
"x................xxxxxxxxxxxxxxxxxxxxxx"
}
Returns: 1

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=12183&pm=7620

Writer:

hken

Testers:

PabloGilberto , Olexiy , misof , ivan_metelsky

Problem categories:

Graph Theory, Search