A river flows from north to south. You want to harness the river's waters to generate hydroelectricity to keep up with the increasing power demands of your state.
You want to build a hydroelectric power plant in the bottom-right cell. However, to ensure that construction is efficient, you need to stop the river water from coming into that cell. To facilitate this, you decide to build a dam on the river based on the way it flows. You may only build a dam in any cell that is not a rock.
The river water has a natural current (direction of flow). It starts flowing from the top-left cell, and through any cell, it only flows either to the right or to the bottom.
A snap-shot of the flow of river water is shown as an example below:
The natural flow of this river can be modeled using a flow model in the form of a grid.
...##
.#.##
.....
##.#.
##...
This is what the symbols in the flow model mean:
Using the flow model, you may build a dam (marked as D) only in three cells as shown below to prevent water from entering the bottom-right cell:
D..##
.#.##
..D..
##.#.
##..D
If you build the dam in the center (as shown in the grid below), the flow of river water to the bottom-right cell is stopped:
...##
.#.##
..D..
##.#.
##...
A snap-shot of the building of a dam at the center is shown below:
However, if you build the dam in any other cell other than the cell in the center, it will not stop the water from entering the bottom-right cell. For example, if you build the dam as follows, the water will still reach the bottom-right cell because at least one path still exists from the top-left cell to the bottom-right cell.
...##
.#D##
.....
##.#.
##...
Task
To build the dam in cell (i, j), you need to spend cost(i, j) units of money. Your task to build the dam by spending the least amount of money.
Determine the minimum cost required to build exactly one dam based on the given river model such that you can stop the river water from entering the bottom-right cell.
Example
Assumptions
Approach
You may build a dam on any of the cells that are marked with D below:
D.##
.D.#
#.D.
##.D
The cheapest cell out of these is the cell, which has the cost of building the dam is 2. And thus, that is the answer to the problem.
Function description
Complete the buildDam function provided in the editor. This function takes the following 4 parameters and returns the minimum cost to build a dam;
Input format
Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).
Output format
Print a single line containing a single integer representing the minimum cost to build a dam on some valid cell.
Constraints
1≤N≤1031≤M≤1031≤costij≤109
Note: It is guaranteed that at least one path exists for the river to go from the top-left to the bottom-right cell.
Code snippets (also called starter code/boilerplate code)
This question has code snippets for C, CPP, Java, and Python.
You can build a dam only in cells that cost 8, 5, and 9. Building a dam in any other cell would have either been:
OR
The cheapest place to build a dam, in this case, is the central cell, which has the cheapest cost of 5, and thus, that is the answer to the problem.