|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static int[][] map, dp; |
| 9 | + private static int[] wall; |
| 10 | + private static boolean vertical; |
| 11 | + private static int N, M; |
| 12 | +
|
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + init(); |
| 15 | + DP(); |
| 16 | +
|
| 17 | + if (dp[N-1][M-1] == -1000000001) bw.write("Entity"); |
| 18 | + else bw.write(dp[N-1][M-1] + "\n"); |
| 19 | + bw.flush(); |
| 20 | + bw.close(); |
| 21 | + br.close(); |
| 22 | + } |
| 23 | +
|
| 24 | + private static void init() throws IOException { |
| 25 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 26 | + N = Integer.parseInt(st.nextToken()); |
| 27 | + M = Integer.parseInt(st.nextToken()); |
| 28 | +
|
| 29 | + map = new int[N][M]; |
| 30 | + dp = new int[N][M]; |
| 31 | + wall = new int[4]; |
| 32 | +
|
| 33 | + for (int i = 0; i < N; i++) { |
| 34 | + st = new StringTokenizer(br.readLine()); |
| 35 | + for (int j = 0; j < M; j++) { |
| 36 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 37 | + dp[i][j] = -1000000001; |
| 38 | + } |
| 39 | + } |
| 40 | +
|
| 41 | + st = new StringTokenizer(br.readLine()); |
| 42 | + wall[0] = Integer.parseInt(st.nextToken()); |
| 43 | + wall[1] = Integer.parseInt(st.nextToken()); |
| 44 | + wall[2] = Integer.parseInt(st.nextToken()); |
| 45 | + wall[3] = Integer.parseInt(st.nextToken()); |
| 46 | +
|
| 47 | + vertical = wall[1] == wall[3]; |
| 48 | + } |
| 49 | +
|
| 50 | + private static void DP() { |
| 51 | + dp[0][0] = map[0][0]; |
| 52 | +
|
| 53 | + for (int i = 0; i < N; i++) { |
| 54 | + for (int j = 0; j < M; j++) { |
| 55 | + if (i == 0 && j == 0) continue; |
| 56 | +
|
| 57 | + if (i > 0 && dp[i-1][j] != -1000000001 && canMove(i-1, j, 0)) { |
| 58 | + dp[i][j] = Math.max(dp[i][j], dp[i-1][j] + map[i][j]); |
| 59 | + } |
| 60 | +
|
| 61 | + if (j > 0 && dp[i][j-1] != -1000000001 && canMove(i, j-1, 1)) { |
| 62 | + dp[i][j] = Math.max(dp[i][j], dp[i][j-1] + map[i][j]); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +
|
| 68 | + private static boolean canMove(int x, int y, int dir) { |
| 69 | + if (vertical && dir == 1) { |
| 70 | + int x_min = Math.min(wall[0], wall[2]); |
| 71 | + int x_max = Math.max(wall[0], wall[2]); |
| 72 | + if ((x_min <= x && x < x_max) && y+1 == wall[1]) return false; |
| 73 | + } else if (!vertical && dir == 0) { |
| 74 | + int y_min = Math.min(wall[1], wall[3]); |
| 75 | + int y_max = Math.max(wall[1], wall[3]); |
| 76 | + if (x+1 == wall[0] && (y_min <= y && y < y_max)) return false; |
| 77 | + } |
| 78 | + return true; |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
0 commit comments