File tree 3 files changed +9
-9
lines changed
solution/1000-1099/1091.Shortest Path in Binary Matrix
3 files changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -220,18 +220,18 @@ function shortestPathBinaryMatrix(grid: number[][]): number {
220
220
if (grid [0 ][0 ]) {
221
221
return - 1 ;
222
222
}
223
- const n = grid .length ;
223
+ const max = grid .length - 1 ;
224
224
grid [0 ][0 ] = 1 ;
225
225
let q: number [][] = [[0 , 0 ]];
226
226
for (let ans = 1 ; q .length > 0 ; ++ ans ) {
227
227
const nq: number [][] = [];
228
228
for (const [i, j] of q ) {
229
- if (i === n - 1 && j === n - 1 ) {
229
+ if (i === max && j === max ) {
230
230
return ans ;
231
231
}
232
232
for (let x = i - 1 ; x <= i + 1 ; ++ x ) {
233
233
for (let y = j - 1 ; y <= j + 1 ; ++ y ) {
234
- if (x >= 0 && x < n && y >= 0 && y < n && ! grid [x ][y ]) {
234
+ if (grid [x ]?. [y ] === 0 ) {
235
235
grid [x ][y ] = 1 ;
236
236
nq .push ([x , y ]);
237
237
}
Original file line number Diff line number Diff line change @@ -206,18 +206,18 @@ function shortestPathBinaryMatrix(grid: number[][]): number {
206
206
if (grid [0 ][0 ]) {
207
207
return - 1 ;
208
208
}
209
- const n = grid .length ;
209
+ const max = grid .length - 1 ;
210
210
grid [0 ][0 ] = 1 ;
211
211
let q: number [][] = [[0 , 0 ]];
212
212
for (let ans = 1 ; q .length > 0 ; ++ ans ) {
213
213
const nq: number [][] = [];
214
214
for (const [i, j] of q ) {
215
- if (i === n - 1 && j === n - 1 ) {
215
+ if (i === max && j === max ) {
216
216
return ans ;
217
217
}
218
218
for (let x = i - 1 ; x <= i + 1 ; ++ x ) {
219
219
for (let y = j - 1 ; y <= j + 1 ; ++ y ) {
220
- if (x >= 0 && x < n && y >= 0 && y < n && ! grid [x ][y ]) {
220
+ if (grid [x ]?. [y ] === 0 ) {
221
221
grid [x ][y ] = 1 ;
222
222
nq .push ([x , y ]);
223
223
}
Original file line number Diff line number Diff line change @@ -2,18 +2,18 @@ function shortestPathBinaryMatrix(grid: number[][]): number {
2
2
if ( grid [ 0 ] [ 0 ] ) {
3
3
return - 1 ;
4
4
}
5
- const n = grid . length ;
5
+ const max = grid . length - 1 ;
6
6
grid [ 0 ] [ 0 ] = 1 ;
7
7
let q : number [ ] [ ] = [ [ 0 , 0 ] ] ;
8
8
for ( let ans = 1 ; q . length > 0 ; ++ ans ) {
9
9
const nq : number [ ] [ ] = [ ] ;
10
10
for ( const [ i , j ] of q ) {
11
- if ( i === n - 1 && j === n - 1 ) {
11
+ if ( i === max && j === max ) {
12
12
return ans ;
13
13
}
14
14
for ( let x = i - 1 ; x <= i + 1 ; ++ x ) {
15
15
for ( let y = j - 1 ; y <= j + 1 ; ++ y ) {
16
- if ( x >= 0 && x < n && y >= 0 && y < n && ! grid [ x ] [ y ] ) {
16
+ if ( grid [ x ] ?. [ y ] === 0 ) {
17
17
grid [ x ] [ y ] = 1 ;
18
18
nq . push ( [ x , y ] ) ;
19
19
}
You can’t perform that action at this time.
0 commit comments