@@ -210,5 +210,105 @@ Meanwhile, we need to use some ideas from the shortest path to update the path.
210210 }
211211 ```
212212
213+ ### Amazon Session
214+ * Method 1 : dfs
215+ ```Java
216+ class Solution {
217+ private static final int [][] dir = new int [][]{{0 , 1 }, {1 , 0 }, {- 1 , 0 }, {0 , - 1 }};
218+ private int height, width;
219+ private int [][] maze;
220+ private int [] destination;
221+ private int result;
222+ private int [][] dist;
223+ public int shortestDistance (int [][] maze , int [] start , int [] destination ) {
224+ this . height = maze. length;
225+ this . width = maze[0 ]. length;
226+ this . maze = maze;
227+ this . destination = destination;
228+ this . result = Integer . MAX_VALUE ;
229+ this . dist = new int [height][width];
230+ for (int [] dd: dist) Arrays . fill(dd, Integer . MAX_VALUE );
231+ dist[start[0 ]][start[1 ]] = 0 ;
232+ if (start[0 ] == destination[0 ] && start[1 ] == destination[1 ]) return 0 ;
233+ for (int d = 0 ; d < 4 ; d++ ){
234+ dfs(start, 0 , d);
235+ }
236+ return this . result == Integer . MAX_VALUE ? - 1 : this . result;
237+ }
238+ private void dfs (int [] cur , int temp , int pre ){
239+ // cur: current position
240+ // temp: current step
241+ // pre: previous direction
242+ if (cur[0 ] == this . destination[0 ] && cur[1 ] == this . destination[1 ]){
243+ this . result = Math . min(this . result, temp);
244+ }else if (dist[cur[0 ]][cur[1 ]] < this . result){
245+ int tx = 0 , ty = 0 ;
246+ for (int d = 0 ; d < 4 ; d++ ){ // 4 directions
247+ if (d + pre == 3 || d == pre) continue ;
248+ int currentStep = temp + 1 ;
249+ tx = cur[0 ] + dir[d][0 ];
250+ ty = cur[1 ] + dir[d][1 ];
251+ while (tx >= 0 && tx < height && ty >= 0 && ty < width && maze[tx][ty] != 1 ){
252+ tx += dir[d][0 ];
253+ ty += dir[d][1 ];
254+ currentStep++ ;
255+ }
256+ tx -= dir[d][0 ];
257+ ty -= dir[d][1 ];
258+ -- currentStep;
259+ if (dist[tx][ty] <= currentStep) continue ;
260+ else {
261+ dist[tx][ty] = currentStep;
262+ dfs(new int []{tx, ty}, currentStep, d);
263+ }
264+ }
265+ }
266+ }
267+ }
268+ ```
269+
270+ * Method 2 : bfs
271+ ```Java
272+ class Solution {
273+ private static final int [][] dir = new int [][]{{0 , 1 }, {0 , - 1 }, {1 , 0 }, {- 1 , 0 }};
274+ public int shortestDistance (int [][] maze , int [] start , int [] destination ) {
275+ int height = maze. length, width = maze[0 ]. length;
276+ if (start[0 ] == destination[0 ] && start[1 ] == destination[1 ]) return 0 ;
277+ int [][] dist = new int [height][width];
278+ for (int [] dd : dist) Arrays . fill(dd, Integer . MAX_VALUE );
279+ dist[start[0 ]][start[1 ]] = 0 ;
280+ Queue<int[]> q = new LinkedList<> ();
281+ q. offer(start);
282+ int result = Integer . MAX_VALUE ;
283+ while (! q. isEmpty()){
284+ int [] cur = q. poll();
285+ if (cur[0 ] == destination[0 ] && cur[1 ] == destination[1 ]){
286+ result = Math . min(result, dist[cur[0 ]][cur[1 ]]);
287+ continue ;
288+ }
289+ int tx = 0 , ty = 0 ;
290+ for (int d = 0 ; d < 4 ; d++ ){
291+ int step = dist[cur[0 ]][cur[1 ]] + 1 ;
292+ tx = cur[0 ] + dir[d][0 ];
293+ ty = cur[1 ] + dir[d][1 ];
294+ while (tx >= 0 && tx < height && ty >= 0 && ty < width && maze[tx][ty] != 1 ){
295+ tx += dir[d][0 ];
296+ ty += dir[d][1 ];
297+ ++ step;
298+ }
299+ -- step;
300+ tx -= dir[d][0 ];
301+ ty -= dir[d][1 ];
302+ if (tx == cur[0 ] && ty == cur[1 ]) continue ;
303+ if (dist[tx][ty] > step){
304+ dist[tx][ty] = step;
305+ if (dist[tx][ty] < result) q. offer(new int []{tx, ty});
306+ }
307+ }
308+ }
309+ return result == Integer . MAX_VALUE ? - 1 : result;
310+ }
311+ }
312+ ```
213313### Reference
2143141. [Java 6ms BFS by optimizing solution #2 ](https: // leetcode.com/problems/the-maze-ii/discuss/290079/Java-6ms-BFS-by-optimizing-solution-2)
0 commit comments