| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 8 초 | 1024 MB | 16 | 5 | 5 | 31.250% |
Alice is playing a puzzle game where she slides a tile piece to move it to its destination on a rectangular grid board consisting of unit squares. Only parallel moves of the piece on the game board are allowed; no rotations nor lifting up. Some squares on the board are marked no-entry where any parts of the tile piece should not enter.
The tile piece is composed of a number of unit squares joined tightly along their edges. The tile piece is initially placed to touch both the left and the top ends of the board. The goal of Alice is to slide the tile piece to make it touch both the right and the bottom ends of the board.
Bob, on the other hand, wants to make Alice's goal unachievable by changing some squares to no-entry. Each square, except for those initially covered by the tile piece, can be made no-entry by paying the specified number of coins. Compute the minimum number of coins Bob has to pay in total to make Alice's goal unachievable.
The input consists of multiple datasets, each in the following format.
r c
a1,1 ⋯ a1,c
⋮
ar,1 ⋯ ar,c
Here, r and c are integers between 2 and 100, inclusive, representing the numbers of rows and columns of the board, respectively. The following ai,j (1 ≤ i ≤ r, 1 ≤ j ≤ c) are integers between −1 and 109, inclusive. Here, ai,j represents the state of the square (i, j) on row i and column j, as follows.
The number of (i, j) satisfying ai,j = −1 is between 1 and 100, inclusive. All the squares corresponding to them are connected directly or indirectly with one or more of their edges. Furthermore, they are contiguous in each row and each column. That is, if ai,j1 = −1 and ai,j2 = −1 for two columns j1 and j2 in the same row i where j1 < j2, then ai,j = −1 for all j such that j1 < j < j2. Similarly, if ai1,j = −1 and ai2,j = −1 for two rows i1 and i2 in the same column j where i1 < i2, then ai,j = −1 for all i such that i1 < i < i2.
It is guaranteed that, in the initial state, the tile piece is placed so that some of its edges touch the left end of the board and some touch the top end. It is also guaranteed that Alice has not yet achieved her goal. That is, the following two conditions are satisfied.
The end of the input is indicated by a line consisting of two zeros. The number of datasets does not exceed 50. The sum of r of all the datasets does not exceed 1000. The sum of c of all the datasets does not exceed 1000. The total number of (i, j) satisfying ai,j = −1 of all the datasets does not exceed 1000.
For each dataset, output on a single line the minimum number of coins Bob has to pay.
3 3 -1 33 22 99 0 99 11 44 99 4 3 -1 90 10 -1 90 20 20 50 99 10 20 99 3 3 -1 33 22 99 99 99 11 44 0 3 4 99 -1 10 99 -1 -1 -1 99 99 -1 99 99 0 0
33 40 0 10
ICPC > Regionals > Asia Pacific > Japan > Japan Domestic Contest > 2024 Japan Domestic Contest H번