1
1
package g3501_3600 .s3552_grid_teleportation_traversal ;
2
2
3
- // #Medium #2025_05_18_Time_133_ms_ (99.44%)_Space_60.94_MB_(99.44 %)
3
+ // #Medium #2025_05_18_Time_139_ms_ (99.44%)_Space_61.40_MB_(98.89 %)
4
4
5
5
import java .util .ArrayList ;
6
6
import java .util .LinkedList ;
11
11
public class Solution {
12
12
private static final int [][] ADJACENT = new int [][] {{0 , 1 }, {1 , 0 }, {-1 , 0 }, {0 , -1 }};
13
13
14
- public int minMoves (String [] matrix ) {
15
- int m = matrix .length ;
16
- int n = matrix [0 ].length ();
17
- if ((m == 1 && n == 1 )
18
- || (matrix [0 ].charAt (0 ) != '.'
19
- && matrix [m - 1 ].charAt (n - 1 ) == matrix [0 ].charAt (0 ))) {
20
- return 0 ;
21
- }
14
+ private List <int []>[] initializePortals (int m , int n , String [] matrix ) {
22
15
List <int []>[] portalsToPositions = new ArrayList [26 ];
23
16
for (int i = 0 ; i < 26 ; i ++) {
24
17
portalsToPositions [i ] = new ArrayList <>();
@@ -31,8 +24,14 @@ public int minMoves(String[] matrix) {
31
24
}
32
25
}
33
26
}
34
- boolean [][] visited = new boolean [m ][n ];
35
- Queue <int []> queue = new LinkedList <>();
27
+ return portalsToPositions ;
28
+ }
29
+
30
+ private void initializeQueue (
31
+ Queue <int []> queue ,
32
+ boolean [][] visited ,
33
+ String [] matrix ,
34
+ List <int []>[] portalsToPositions ) {
36
35
if (matrix [0 ].charAt (0 ) != '.' ) {
37
36
int idx = matrix [0 ].charAt (0 ) - 'A' ;
38
37
for (int [] pos : portalsToPositions [idx ]) {
@@ -43,6 +42,46 @@ public int minMoves(String[] matrix) {
43
42
queue .offer (new int [] {0 , 0 });
44
43
}
45
44
visited [0 ][0 ] = true ;
45
+ }
46
+
47
+ private boolean isValidMove (int r , int c , int m , int n , boolean [][] visited , String [] matrix ) {
48
+ return !(r < 0 || r == m || c < 0 || c == n || visited [r ][c ] || matrix [r ].charAt (c ) == '#' );
49
+ }
50
+
51
+ private boolean processPortal (
52
+ int r ,
53
+ int c ,
54
+ int m ,
55
+ int n ,
56
+ Queue <int []> queue ,
57
+ boolean [][] visited ,
58
+ String [] matrix ,
59
+ List <int []>[] portalsToPositions ) {
60
+ int idx = matrix [r ].charAt (c ) - 'A' ;
61
+ for (int [] pos : portalsToPositions [idx ]) {
62
+ if (pos [0 ] == m - 1 && pos [1 ] == n - 1 ) {
63
+ return true ;
64
+ }
65
+ queue .offer (pos );
66
+ visited [pos [0 ]][pos [1 ]] = true ;
67
+ }
68
+ return false ;
69
+ }
70
+
71
+ public int minMoves (String [] matrix ) {
72
+ int m = matrix .length ;
73
+ int n = matrix [0 ].length ();
74
+ if ((m == 1 && n == 1 )
75
+ || (matrix [0 ].charAt (0 ) != '.'
76
+ && matrix [m - 1 ].charAt (n - 1 ) == matrix [0 ].charAt (0 ))) {
77
+ return 0 ;
78
+ }
79
+
80
+ List <int []>[] portalsToPositions = initializePortals (m , n , matrix );
81
+ boolean [][] visited = new boolean [m ][n ];
82
+ Queue <int []> queue = new LinkedList <>();
83
+ initializeQueue (queue , visited , matrix , portalsToPositions );
84
+
46
85
int moves = 0 ;
47
86
while (!queue .isEmpty ()) {
48
87
int sz = queue .size ();
@@ -51,22 +90,12 @@ public int minMoves(String[] matrix) {
51
90
for (int [] adj : ADJACENT ) {
52
91
int r = adj [0 ] + curr [0 ];
53
92
int c = adj [1 ] + curr [1 ];
54
- if (r < 0
55
- || r == m
56
- || c < 0
57
- || c == n
58
- || visited [r ][c ]
59
- || matrix [r ].charAt (c ) == '#' ) {
93
+ if (!isValidMove (r , c , m , n , visited , matrix )) {
60
94
continue ;
61
95
}
62
96
if (matrix [r ].charAt (c ) != '.' ) {
63
- int idx = matrix [r ].charAt (c ) - 'A' ;
64
- for (int [] pos : portalsToPositions [idx ]) {
65
- if (pos [0 ] == m - 1 && pos [1 ] == n - 1 ) {
66
- return moves + 1 ;
67
- }
68
- queue .offer (pos );
69
- visited [pos [0 ]][pos [1 ]] = true ;
97
+ if (processPortal (r , c , m , n , queue , visited , matrix , portalsToPositions )) {
98
+ return moves + 1 ;
70
99
}
71
100
} else {
72
101
if (r == m - 1 && c == n - 1 ) {
0 commit comments