1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| import java.util.Scanner;
public class Main { static int MAXSIZE = (int)1e3 + 3; static int[][][] dp = new int[MAXSIZE][MAXSIZE][2]; static int[][] map = new int[MAXSIZE][MAXSIZE]; static int sx, sy, fx, fy; static int k; static int n, m; static int[] dx = {0, 1, 0, -1}; static int[] dy = {1, 0, -1, 0}; static boolean isValid(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; } static int dfs(int x, int y, int p) { if(x == fx && y == fy) { return 1; } if(dp[x][y][p] != -1) { return dp[x][y][p]; } for(int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if(!isValid(nx, ny)) continue; if(p == 0) { if(map[x][y] >= map[nx][ny]) { int canReach = dfs(nx, ny, 0); if(canReach == 1) { dp[x][y][p] = 1; return 1; } } if(map[x][y] < map[nx][ny] && map[x][y] + k >= map[nx][ny]) { int canReach =dfs(nx, ny, 1); if(canReach == 1) { dp[x][y][p] = 1; return 1; } } } else { if(map[x][y] >= map[nx][ny]) { int canReach = dfs(nx, ny, 1); if(canReach == 1) { dp[x][y][p] = 1; return 1; } } } } dp[x][y][p] = 0; return 0; } static void fillArrays() { for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { for(int k = 0; k < 2; k++) { dp[i][j][k] = -1; } } } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); n = scan.nextInt(); m = scan.nextInt(); k = scan.nextInt(); sx = scan.nextInt(); sy = scan.nextInt(); fx = scan.nextInt(); fy = scan.nextInt(); fillArrays(); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { map[i][j] = scan.nextInt(); } } if(dfs(sx, sy, 0) == 1) { System.out.println("Yes"); } else { System.out.println("No"); } scan.close(); } }
|