Logo
(追記) (追記ここまで)

32053번 - Blocking the Way 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
8 초 1024 MB165531.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,1a1,c

ar,1ar,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 ≤ ir, 1 ≤ jc) 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.

  • If ai,j = −1, the square is initially covered by the tile piece.
  • If ai,j = 0, the square is no-entry from the beginning.
  • If ai,j > 0, Bob can make the square no-entry by paying ai,j coins.

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.

  • There exists at least one i satisfying ai,1 = −1 and at least one j satisfying a1,j = −1.
  • ai,c ≠ −1 for all i or ar,j ≠ −1 for all j.

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.

제한

예제 입력 1

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

예제 출력 1

33
40
0
10

힌트

출처

ICPC > Regionals > Asia Pacific > Japan > Japan Domestic Contest > 2024 Japan Domestic Contest H번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /