-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetCode-1091-Shortest-Path-in-Binary-Matrix.java
175 lines (139 loc) · 5.96 KB
/
LeetCode-1091-Shortest-Path-in-Binary-Matrix.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
class Solution {
// DFS (TLE)
// public int shortestPathBinaryMatrix(int[][] grid) {
// if (grid == null && grid.length == 0) return 0;
// int[] res = new int[1];
// res[0] = Integer.MAX_VALUE;
// if (grid[0][0] != 0) return -1;
// DFS(grid, 0, 0, 1, res);
// return res[0] == Integer.MAX_VALUE ? -1 : res[0];
// }
// private void DFS(int[][] grid, int i, int j, int len, int[] res) {
// if (len >= res[0]) return;
// if (i == grid.length -1 && j == grid[0].length - 1) {
// res[0] = len;
// return;
// }
// grid[i][j] = -1;
// int[] directions = new int[] {-1, 0, 1};
// for (int x : directions) {
// for (int y : directions) {
// if (isValid(i + x, j + y, grid) && grid[i + x][j + y] == 0) {
// DFS(grid, i + x, j + y, len + 1, res);
// }
// }
// }
// grid[i][j] = 0;
// }
// private boolean isValid(int i, int j, int[][] grid) {
// if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length) return false;
// return true;
// }
// 2. BFS, level order traversal without visited array (need update the grid array)
/*
Runtime: 9 ms, faster than 99.89% of Java online submissions for Shortest Path in Binary Matrix.
Memory Usage: 43.6 MB, less than 88.80% of Java online submissions for Shortest Path in Binary Matrix.
*/
// public int shortestPathBinaryMatrix(int[][] grid) {
// if (grid == null && grid.length == 0) return 0;
// if (grid[0][0] == 1) return -1;
// int n = grid.length;
// if (grid[n - 1][n - 1] == 1) return -1;
// Queue<int[]> queue = new LinkedList<>();
// queue.offer(new int[]{0, 0});
// grid[0][0] = 1;
// int len = 0;
// int[] directions = new int[] {-1, 0, 1};
// while (queue.size() > 0) {
// len++;
// int size = queue.size();
// for (int k = 0; k < size; k++) {
// int[] curr = queue.poll();
// int i = curr[0], j = curr[1];
// if (i == n - 1 && j == n - 1) return len;
// for (int x : directions) {
// for (int y : directions) {
// if (isValid(i + x, j + y, grid) && grid[i + x][j + y] == 0) {
// queue.offer(new int[]{i + x, j + y});
// grid[i + x][j + y] = 1;
// }
// }
// }
// }
// }
// return -1;
// }
// private boolean isValid(int i, int j, int[][] grid) {
// if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length) return false;
// return true;
// }
// BFS, level order traversal, with visited array, (don't need to update the grid array)
// public int shortestPathBinaryMatrix(int[][] grid) {
// if (grid == null && grid.length == 0) return 0;
// if (grid[0][0] == 1) return -1;
// int n = grid.length;
// if (grid[n - 1][n - 1] == 1) return -1;
// Queue<int[]> queue = new LinkedList<>();
// queue.offer(new int[]{0, 0});
// boolean[][] visited = new boolean[n][n];
// visited[0][0] = true;
// int len = 0;
// int dir[][] = new int[][]{{0,1},{0,-1},{1,0},{-1,0},{1,-1},{-1,1},{-1,-1},{1,1}};
// while (queue.size() > 0) {
// len++;
// int size = queue.size();
// for (int k = 0; k < size; k++) {
// int[] curr = queue.poll();
// int i = curr[0], j = curr[1];
// if (i == n - 1 && j == n - 1) return len;
// for (int l = 0; l < 8; l++) {
// int x = dir[l][0], y = dir[l][1];
// if (isValid(i + x, j + y, grid) && !visited[i + x][j + y] && grid[i + x][j + y] == 0) {
// queue.offer(new int[]{i + x, j + y});
// visited[i + x][j + y] = true;
// }
// }
// }
// }
// return -1;
// }
// private boolean isValid(int i, int j, int[][] grid) {
// if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length) return false;
// return true;
// }
// private int dir[][] = new int[][]{{0,1},{0,-1},{1,0},{-1,0},{1,-1},{-1,1},{-1,-1},{1,1}};
// public int shortestPathBinaryMatrix(int[][] grid) {
// int m = grid.length;
// int n = grid[0].length;
// if(grid[0][0]==1 || grid[m-1][n-1]==1) {
// return -1;
// }
// boolean[][] visited = new boolean[m][n];
// visited[0][0] = true;
// Queue<int[]> queue = new LinkedList<>();
// queue.add(new int[]{0,0});
// int ans=0;
// while (!queue.isEmpty()) {
// int size = queue.size();
// for(int i=0;i<size;i++) {
// int[] pop = queue.remove();
// if(pop[0]==m-1 && pop[1]==n-1) {
// return ans+1;
// }
// for (int k=0;k<8;k++) {
// int nextX = dir[k][0]+pop[0];
// int nextY = dir[k][1]+pop[1];
// if(nextX>=0 && nextX<m && nextY>=0 && nextY<n && !visited[nextX][nextY] && grid[nextX][nextY]==0) {
// queue.add(new int[]{nextX,nextY});
// visited[nextX][nextY]=true;
// }
// }
// }
// ans++;
// }
// return -1;
// }
// 3. A* (A Star) Search Algorithm
public int shortestPathBinaryMatrix(int[][] grid) {
}
}