|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + static int [][] d = {{1, -1}, {1, 0}, {1, 1}}; |
| 6 | + static int [][] map; |
| 7 | + static int m, n, ans; |
| 8 | + public static void main(String[] args) throws Exception{ |
| 9 | + preSetting(); |
| 10 | + for(int i = 0; i < m; i++ ){ |
| 11 | + recur(-1, 0, i, map[0][i]); |
| 12 | + } |
| 13 | + System.out.println(ans); |
| 14 | + } |
| 15 | + |
| 16 | + private static void recur(int preD, int x, int y, int oil){ |
| 17 | + if(x == n - 1){ |
| 18 | + ans = Math.min(ans, oil); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + int nX, nY; |
| 23 | + for(int i = 0; i < 3; i++){ |
| 24 | + if(i == preD) continue; |
| 25 | + |
| 26 | + nX = d[i][0] + x; |
| 27 | + nY = d[i][1] + y; |
| 28 | + |
| 29 | + if(n < nX || m <= nY || nY < 0) continue; |
| 30 | + |
| 31 | + recur(i, nX, nY, oil + map[nX][nY]); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private static void preSetting() throws Exception{ |
| 36 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 37 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 38 | + |
| 39 | + n = Integer.parseInt(st.nextToken()); |
| 40 | + m = Integer.parseInt(st.nextToken()); |
| 41 | + |
| 42 | + map = new int[n][m]; |
| 43 | + ans = Integer.MAX_VALUE; |
| 44 | + for(int i = 0; i < n; i++){ |
| 45 | + st = new StringTokenizer(br.readLine()); |
| 46 | + for(int j = 0; j < m; j++){ |
| 47 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments