|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class BJ_26259_백룸 { |
| 7 | + |
| 8 | + private static final int INF = -987654321; |
| 9 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + private static StringTokenizer st; |
| 12 | + |
| 13 | + private static int N, M; |
| 14 | + private static boolean isHorizontal; |
| 15 | + private static int[][] matrix, dp; |
| 16 | + private static boolean[][] wall; |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + init(); |
| 20 | + sol(); |
| 21 | + } |
| 22 | + |
| 23 | + private static void init() throws IOException { |
| 24 | + st = new StringTokenizer(br.readLine()); |
| 25 | + N = Integer.parseInt(st.nextToken()); |
| 26 | + M = Integer.parseInt(st.nextToken()); |
| 27 | + |
| 28 | + matrix = new int[N + 1][M + 1]; |
| 29 | + for (int i = 1; i <= N; i++) { |
| 30 | + st = new StringTokenizer(br.readLine()); |
| 31 | + for (int j = 1; j <= M; j++) { |
| 32 | + matrix[i][j] = Integer.parseInt(st.nextToken()); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + dp = new int[N + 1][M + 1]; |
| 37 | + for (int[] d : dp) Arrays.fill(d, INF); |
| 38 | + dp[1][1] = matrix[1][1]; |
| 39 | + |
| 40 | + wall = new boolean[N + 1][M + 1]; |
| 41 | + st = new StringTokenizer(br.readLine()); |
| 42 | + int x1 = Integer.parseInt(st.nextToken()); |
| 43 | + int y1 = Integer.parseInt(st.nextToken()); |
| 44 | + int x2 = Integer.parseInt(st.nextToken()); |
| 45 | + int y2 = Integer.parseInt(st.nextToken()); |
| 46 | + |
| 47 | + if (x1 == x2 && y1 == y2) return; |
| 48 | + |
| 49 | + isHorizontal = (x1 == x2); |
| 50 | + |
| 51 | + if (isHorizontal && x1 < N) { |
| 52 | + int start = Math.min(y1, y2) + 1; |
| 53 | + int end = Math.max(y1, y2); |
| 54 | + for (int y = start; y <= end; y++) { |
| 55 | + wall[x1 + 1][y] = true; |
| 56 | + } |
| 57 | + } else if (!isHorizontal && y1 < M) { |
| 58 | + int start = Math.min(x1, x2) + 1; |
| 59 | + int end = Math.max(x1, x2); |
| 60 | + for (int x = start; x <= end; x++) { |
| 61 | + wall[x][y1 + 1] = true; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static void sol() throws IOException { |
| 67 | + for (int i = 1; i <= N; i++) { |
| 68 | + for (int j = 1; j <= M; j++) { |
| 69 | + if (i == 1 && j == 1) continue; |
| 70 | + |
| 71 | + if (wall[i][j]) { |
| 72 | + if (isHorizontal && dp[i][j - 1] != INF) { |
| 73 | + dp[i][j] = matrix[i][j] + dp[i][j - 1]; |
| 74 | + } else if (!isHorizontal && dp[i - 1][j] != INF) { |
| 75 | + dp[i][j] = matrix[i][j] + dp[i - 1][j]; |
| 76 | + } |
| 77 | + } else { |
| 78 | + int up = dp[i - 1][j]; |
| 79 | + int left = dp[i][j - 1]; |
| 80 | + if (up != INF || left != INF) { |
| 81 | + dp[i][j] = matrix[i][j] + Math.max(up, left); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + bw.write(dp[N][M] == INF ? "Entity" : String.valueOf(dp[N][M])); |
| 88 | + bw.flush(); |
| 89 | + bw.close(); |
| 90 | + br.close(); |
| 91 | + } |
| 92 | + |
| 93 | +} |
| 94 | +``` |
0 commit comments