After the rain, Chris can finally walk back to his hostel. In order to do so, he must go through a park which can be seen as a matrix of size . The entrance is on the bottom left and the exit is on the top right corner of the park. Chris wants to take the shortest path, so he will only move upward or to the right.
Unfortunately, the snails decided to come out for some fresh air! Each unit square has some number of snails on it. What is the minimum number of snails Chris will need to pass through?
Details and Assumptions:
Sample Input
1 2 3 4 |
|
Sample Output
1 |
|
Explanation
Chris walks to the squares that has number of snails respectively.
Inspired by the number of snails I saw on my way back.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
(Java) I opened the input file in the BufferedReader
ipf
. Then:The table
accu[x]
keeps track of the least number of snails from position x in the current row to the exit at the top-right. The main decision making happens using the minimum function:accu[x]
represents the number of snails encountered after moving upward, andaccu[x+1]
the number of snails encountered after moving to the right.Output: