@@ -8,7 +8,9 @@ Tag : 「BFS」
8
8
9
9
你还记得那条风靡全球的贪吃蛇吗?
10
10
11
- 我们在一个 ` n*n ` 的网格上构建了新的迷宫地图,蛇的长度为 ` 2 ` ,也就是说它会占去两个单元格。蛇会从左上角(` (0, 0) ` 和 ` (0, 1) ` )开始移动。我们用 ` 0 ` 表示空单元格,用 ` 1 ` 表示障碍物。蛇需要移动到迷宫的右下角(` (n-1, n-2) ` 和 ` (n-1, n-1) ` )。
11
+ 我们在一个 ` n*n ` 的网格上构建了新的迷宫地图,蛇的长度为 ` 2 ` ,也就是说它会占去两个单元格。蛇会从左上角(` (0, 0) ` 和 ` (0, 1) ` )开始移动。我们用 ` 0 ` 表示空单元格,用 ` 1 ` 表示障碍物。
12
+
13
+ 蛇需要移动到迷宫的右下角(` (n-1, n-2) ` 和 ` (n-1, n-1) ` )。
12
14
13
15
每次移动,蛇可以这样走:
14
16
@@ -75,8 +77,7 @@ Tag : 「BFS」
75
77
76
78
在得到新蛇尾位置 $(nx, ny)$ 之后,计算新蛇头的位置 $(tx, ty)$。需要确保整条蛇没有越界,没有碰到障碍物,并且旋转转移时,额外检查 $(x + 1, y + 1)$ 位置是否合法。
77
79
78
-
79
- 代码:
80
+ Java 代码:
80
81
``` Java
81
82
class Solution {
82
83
int [][] dirs = new int [][]{{1 ,0 ,0 },{0 ,1 ,0 },{0 ,0 ,1 }};
@@ -105,6 +106,85 @@ class Solution {
105
106
}
106
107
}
107
108
```
109
+ C++ 代码:
110
+ ``` C++
111
+ class Solution {
112
+ public:
113
+ int minimumMoves(vector<vector<int >>& g) {
114
+ vector<vector<int >> dirs = {{1,0,0}, {0,1,0}, {0,0,1}};
115
+ int n = g.size();
116
+ queue<vector<int >> d;
117
+ d.push({0, 0, 0, 0});
118
+ vector<vector<vector<bool >>> vis(n, vector<vector<bool >>(n, vector<bool >(2, false)));
119
+ vis[ 0] [ 0 ] [ 0] = true;
120
+ while (!d.empty()) {
121
+ vector<int > info = d.front();
122
+ d.pop();
123
+ int x = info[ 0] , y = info[ 1] , cd = info[ 2] , step = info[ 3] ;
124
+ for (vector<int >& dir : dirs) {
125
+ int nx = x + dir[ 0] , ny = y + dir[ 1] , nd = cd ^ dir[ 2] ;
126
+ int tx = nd == 0 ? nx : nx + 1, ty = nd == 0 ? ny + 1 : ny;
127
+ if (nx >= n || ny >= n || tx >= n || ty >= n) continue;
128
+ if (g[ nx] [ ny ] == 1 || g[ tx] [ ty ] == 1) continue;
129
+ if (vis[ nx] [ ny ] [ nd] ) continue;
130
+ if (cd != nd && g[ x + 1] [ y + 1 ] == 1) continue;
131
+ if (nx == n - 1 && ny == n - 2 && nd == 0) return step + 1;
132
+ d.push({nx, ny, nd, step + 1});
133
+ vis[ nx] [ ny ] [ nd] = true;
134
+ }
135
+ }
136
+ return -1;
137
+ }
138
+ };
139
+ ```
140
+ Python 代码:
141
+ ```Python
142
+ class Solution:
143
+ def minimumMoves(self, g: List[List[int]]) -> int:
144
+ dirs = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
145
+ n = len(g)
146
+ d = deque([(0,0,0,0)])
147
+ vis = [[[0]*2 for _ in range(n)] for _ in range(n)]
148
+ vis[0][0][0] = 1
149
+ while d:
150
+ x, y, cd, step = d.popleft()
151
+ for dir in dirs:
152
+ nx, ny, nd = x + dir[0], y + dir[1], cd ^ dir[2]
153
+ tx, ty = nx + (nd == 1), ny + (nd == 0)
154
+ if nx >= n or ny >= n or tx >= n or ty >= n: continue
155
+ if g[nx][ny] == 1 or g[tx][ty] == 1: continue
156
+ if vis[nx][ny][nd]: continue
157
+ if cd != nd and g[x + 1][y + 1] == 1: continue
158
+ if nx == n - 1 and ny == n - 2 and nd == 0: return step + 1
159
+ d.append((nx, ny, nd, step + 1))
160
+ vis[nx][ny][nd] = 1
161
+ return -1
162
+ ```
163
+ TypeScript 代码:
164
+ ``` TypeScript
165
+ function minimumMoves(g : number [][]): number {
166
+ const n = g .length ;
167
+ const d: [number , number , number , number ][] = [[0 ,0 ,0 ,0 ]];
168
+ const vis: boolean [][][] = Array .from ({ length: n }, () => Array .from ({ length: n }, () => [false , false ]));
169
+ vis [0 ][0 ][0 ] = true ;
170
+ const dirs: [number , number , number ][] = [[1 ,0 ,0 ], [0 ,1 ,0 ], [0 ,0 ,1 ]];
171
+ while (d .length > 0 ) {
172
+ const [x, y, cd, step] = d .shift ()! ;
173
+ for (const dir of dirs ) {
174
+ const nx = x + dir [0 ], ny = y + dir [1 ], nd = cd ^ dir [2 ];
175
+ const tx = nd === 0 ? nx : nx + 1 , ty = nd === 0 ? ny + 1 : ny ;
176
+ if (nx >= n || ny >= n || tx >= n || ty >= n ) continue ;
177
+ if (g [nx ][ny ] === 1 || g [tx ][ty ] === 1 ) continue
178
+ if (vis [nx ][ny ][nd ]) continue ;
179
+ if (cd !== nd && g [x + 1 ][y + 1 ] === 1 ) continue ;
180
+ if (nx === n - 1 && ny === n - 2 && nd === 0 ) return step + 1 ;
181
+ d .push ([nx , ny , nd , step + 1 ]);
182
+ vis [nx ][ny ][nd ] = true ;
183
+ }
184
+ }
185
+ return - 1 ;
186
+ };
187
+ ```
108
188
* 时间复杂度:$O(n^2)$
109
189
* 空间复杂度:$O(n^2 \times C)$,其中 $C = 2$ 代表蛇可变状态方向
110
190
0 commit comments