Digging a canal Digging a canal

The robots are trying to colonize a new island. There are many rivers on this island and it would be good idea for them to use ships designed for water, rather than their spaceships. Some of the territories do not have a river for transportation from one edge of a field to another. The robots can dig a canal and convert the land to water if need be. We must explore a field map and count the minimum number of cells necessary for a canal that would allow the robots to sail from northern edge of the map to southern edge. The map is in a 2D array, where 0 is water, 1 is land. Cells are connected by their edges.

digging_canal digging_canal

Input: A map as a list of lists with 1 or 0.

Output: The minimum number of cells necessary for a canal as an integer.

Example:

checkio([[1, 1, 1, 1, 0, 1, 1],
         [1, 1, 1, 1, 0, 0, 1],
         [1, 1, 1, 1, 1, 0, 1],
         [1, 1, 0, 1, 1, 0, 1],
         [1, 1, 0, 1, 1, 1, 1],
         [1, 0, 0, 1, 1, 1, 1],
         [1, 0, 1, 1, 1, 1, 1]]) == 2
checkio([[0, 0, 0, 0, 0, 0, 0],
         [1, 1, 1, 1, 1, 1, 1],
         [1, 1, 1, 1, 1, 1, 1],
         [1, 1, 1, 1, 1, 1, 1],
         [1, 1, 0, 1, 0, 1, 1],
         [1, 0, 0, 0, 0, 0, 1],
         [0, 0, 0, 0, 0, 0, 0]]) == 3
checkio([[1, 1, 1, 1, 1, 0, 1, 1],
         [1, 0, 1, 1, 1, 0, 1, 1],
         [1, 0, 1, 0, 1, 0, 1, 0],
         [1, 0, 1, 1, 1, 0, 1, 1],
         [0, 0, 1, 1, 0, 0, 0, 0],
         [1, 0, 1, 1, 1, 1, 1, 1],
         [1, 0, 1, 1, 1, 1, 1, 1],
         [1, 1, 1, 0, 1, 1, 1, 1]]) == 2

How it is used: This concept is a mix of graph-search and optimization problems. It can be useful in navigational software and for network optimization.

Precondition: 0 < len(land_map) < 10
all(0 < len(row) < 10 for row in land_map)

How to use TryIt: