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
| class Solution { public boolean isPossibleToCutPath(int[][] grid) { m = grid.length; n = grid[0].length; this.grid = grid; return !dfs(0, 0) || !dfs(0, 0); }
private boolean dfs(int i, int j) { if (i == m - 1 && j == n - 1) return true; grid[i][j] = 0; for (int[] dir : dirs) { int row = i + dir[0], col = j + dir[1]; if (isValid(row, col) && grid[row][col] == 1 && dfs(row, col)) return true; } return false; }
int m, n; int[][] dirs = new int[][]{{1, 0}, {0, 1}}, grid;
private boolean isValid(int i, int j) { return i < m && j < n; } }
|