@@ -157,4 +157,107 @@ public int shortestDistance2(int[][] maze, int[] start, int[] destination) {
157157 return dist [destination [0 ]][destination [1 ]] == Integer .MAX_VALUE ? -1 : dist [destination [0 ]][destination [1 ]];
158158 }
159159
160+
161+ public int shortestDistance3 (int [][] maze , int [] start , int [] destination ) {
162+ int M = maze .length ;
163+ int N = maze [0 ].length ;
164+ int [][] memo = new int [M ][N ];
165+ for (int [] row : memo ) Arrays .fill (row , Integer .MAX_VALUE );
166+ memo [start [0 ]][start [1 ]] = 0 ;
167+ roll (maze , start [0 ], start [1 ], memo , M , N );
168+ return memo [destination [0 ]][destination [1 ]] == Integer .MAX_VALUE ? -1 : memo [destination [0 ]][destination [1 ]];
169+ }
170+
171+ private void roll (int [][] maze , int i , int j , int [][] memo , int M , int N ) {
172+ // top
173+ int t = i - 1 ;
174+ int rt = 1 ;
175+ while (t >= 0 && maze [t ][j ] == 0 ) {
176+ t --;
177+ rt ++;
178+ }
179+ t ++;
180+ rt --;
181+ if (memo [i ][j ] + rt < memo [t ][j ]) {
182+ memo [t ][j ] = memo [i ][j ] + rt ;
183+ roll (maze , t , j , memo , M , N );
184+ }
185+
186+ // bottom
187+ int b = i + 1 ;
188+ int rb = 1 ;
189+ while (b < M && maze [b ][j ] == 0 ) {
190+ b ++;
191+ rb ++;
192+ }
193+ b --;
194+ rb --;
195+ if (memo [i ][j ] + rb < memo [b ][j ]) {
196+ memo [b ][j ] = memo [i ][j ] + rb ;
197+ roll (maze , b , j , memo , M , N );
198+ }
199+
200+ // left
201+ int l = j - 1 ;
202+ int rl = 1 ;
203+ while (l >= 0 && maze [i ][l ] == 0 ) {
204+ l --;
205+ rl ++;
206+ }
207+ l ++;
208+ rl --;
209+ if (memo [i ][j ] + rl < memo [i ][l ]) {
210+ memo [i ][l ] = memo [i ][j ] + rl ;
211+ roll (maze , i , l , memo , M , N );
212+ }
213+
214+ // right
215+ int r = j + 1 ;
216+ int rr = 1 ;
217+ while (r < N && maze [i ][r ] == 0 ) {
218+ r ++;
219+ rr ++;
220+ }
221+ r --;
222+ rr --;
223+ if (memo [i ][j ] + rr < memo [i ][r ]) {
224+ memo [i ][r ] = memo [i ][j ] + rr ;
225+ roll (maze , i , r , memo , M , N );
226+ }
227+ }
228+
229+
230+ /**
231+ * https://leetcode.com/problems/the-maze-ii/solution/
232+ */
233+ public int shortestDistance4 (int [][] maze , int [] start , int [] dest ) {
234+ int [][] distance = new int [maze .length ][maze [0 ].length ];
235+ for (int [] row : distance )
236+ Arrays .fill (row , Integer .MAX_VALUE );
237+ distance [start [0 ]][start [1 ]] = 0 ;
238+ dfs (maze , start , distance );
239+ return distance [dest [0 ]][dest [1 ]] == Integer .MAX_VALUE ? -1 : distance [dest [0 ]][dest [1 ]];
240+ }
241+
242+ public void dfs (int [][] maze , int [] start , int [][] distance ) {
243+ int [][] dirs ={{0 ,1 }, {0 ,-1 }, {-1 ,0 }, {1 ,0 }};
244+ for (int [] dir : dirs ) {
245+ int x = start [0 ] + dir [0 ];
246+ int y = start [1 ] + dir [1 ];
247+ int count = 0 ;
248+ while (x >= 0 && y >= 0 && x < maze .length && y < maze [0 ].length && maze [x ][y ] == 0 ) {
249+ x += dir [0 ];
250+ y += dir [1 ];
251+ count ++;
252+ }
253+ if (distance [start [0 ]][start [1 ]] + count < distance [x - dir [0 ]][y - dir [1 ]]) {
254+ distance [x - dir [0 ]][y - dir [1 ]] = distance [start [0 ]][start [1 ]] + count ;
255+ dfs (maze , new int []{x - dir [0 ],y - dir [1 ]}, distance );
256+ }
257+ }
258+ }
259+
260+
261+
262+
160263}
0 commit comments