A canal network is a grid of R rows and C columns; each cell charges a non-negative toll collected when a barge enters it (the top-left starting cell is always entered). A barge starts at the top-left cell and must reach the bottom-right cell, moving on each step to the cell immediately right or immediately below.
Among all such routes, some achieve the smallest possible total toll. Determine how many distinct routes achieve that minimum total.
Line 1: two integers R and C.
Next R lines: each contains C non-negative integers, the tolls of that row.
A single integer: the number of distinct right/down routes whose total toll equals the minimum achievable total toll.
Example 1
Input
2 2 1 1 1 1
Expected
2
Explanation
Both routes cost 1+1+1 = 3, which is the minimum, so 2 routes achieve it.
Example 2
Input
2 2 1 5 2 1
Expected
1
Explanation
Down-then-right costs 1+2+1 = 4 while right-then-down costs 1+5+1 = 7; only 1 route reaches the minimum 4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →