Skip to content

Commit 0a36230

Browse files
committed
feat: add solutions to lc problem: No.0864
No.0864.Shortest Path to Get All Keys
1 parent 07f736b commit 0a36230

File tree

6 files changed

+617
-2
lines changed

6 files changed

+617
-2
lines changed

Diff for: solution/0800-0899/0864.Shortest Path to Get All Keys/README.md

+210-1
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,231 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:状态压缩 + BFS**
71+
7072
<!-- tabs:start -->
7173

7274
### **Python3**
7375

7476
<!-- 这里可写当前语言的特殊实现逻辑 -->
7577

7678
```python
77-
79+
class Solution:
80+
def shortestPathAllKeys(self, grid: List[str]) -> int:
81+
m, n = len(grid), len(grid[0])
82+
cnt, start = 0, None
83+
for i, row in enumerate(grid):
84+
for j, v in enumerate(row):
85+
cnt += v.islower()
86+
if v == '@':
87+
start = (i, j)
88+
q = deque([(start[0], start[1], 0)])
89+
dirs = [-1, 0, 1, 0, -1]
90+
ans = 0
91+
mask = (1 << cnt) - 1
92+
vis = set([(start[0], start[1], 0)])
93+
while q:
94+
for _ in range(len(q)):
95+
i, j, state = q.popleft()
96+
if state == mask:
97+
return ans
98+
for k in range(4):
99+
nxt = state
100+
x, y = i + dirs[k], j + dirs[k + 1]
101+
if 0 <= x < m and 0 <= y < n and grid[x][y] != '#':
102+
if grid[x][y].isupper() and (nxt & (1 << (ord(grid[x][y]) - ord('A')))) == 0:
103+
continue
104+
if grid[x][y].islower():
105+
nxt |= 1 << (ord(grid[x][y]) - ord('a'))
106+
if (x, y, nxt) not in vis:
107+
q.append((x, y, nxt))
108+
vis.add((x, y, nxt))
109+
ans += 1
110+
return -1
78111
```
79112

80113
### **Java**
81114

82115
<!-- 这里可写当前语言的特殊实现逻辑 -->
83116

84117
```java
118+
class Solution {
119+
public int shortestPathAllKeys(String[] grid) {
120+
int m = grid.length, n = grid[0].length();
121+
int cnt = 0;
122+
int sx = 0, sy = 0;
123+
for (int i = 0; i < m; ++i) {
124+
for (int j = 0; j < n; ++j) {
125+
char c = grid[i].charAt(j);
126+
if (Character.isLowerCase(c)) {
127+
++cnt;
128+
} else if (c == '@') {
129+
sx = i;
130+
sy = j;
131+
}
132+
}
133+
}
134+
Deque<int[]> q = new ArrayDeque<>();
135+
q.offer(new int[]{sx, sy, 0});
136+
int[] dirs = {-1, 0, 1, 0, -1};
137+
int ans = 0;
138+
int mask = (1 << cnt) - 1;
139+
boolean[][][] vis = new boolean[m][n][1 << cnt];
140+
vis[sx][sy][0] = true;
141+
while (!q.isEmpty()) {
142+
for (int t = q.size(); t > 0; --t) {
143+
int[] p = q.poll();
144+
int i = p[0], j = p[1], state = p[2];
145+
if (state == mask) {
146+
return ans;
147+
}
148+
for (int k = 0; k < 4; ++k) {
149+
int nxt = state;
150+
int x = i + dirs[k], y = j + dirs[k + 1];
151+
if (x >= 0 && x < m && y >= 0 && y < n) {
152+
char c = grid[x].charAt(y);
153+
if (c == '#' || (Character.isUpperCase(c) && (nxt & (1 << (c - 'A'))) == 0)) {
154+
continue;
155+
}
156+
if (Character.isLowerCase(c)) {
157+
nxt |= 1 << (c - 'a');
158+
}
159+
if (!vis[x][y][nxt]) {
160+
vis[x][y][nxt] = true;
161+
q.offer(new int[]{x, y, nxt});
162+
}
163+
}
164+
}
165+
}
166+
++ans;
167+
}
168+
return -1;
169+
}
170+
}
171+
```
172+
173+
### **C++**
174+
175+
```cpp
176+
class Solution {
177+
public:
178+
int shortestPathAllKeys(vector<string>& grid) {
179+
int m = grid.size(), n = grid[0].size();
180+
int cnt = 0;
181+
int sx = 0, sy = 0;
182+
for (int i = 0; i < m; ++i)
183+
{
184+
for (int j = 0; j < n; ++j)
185+
{
186+
char c = grid[i][j];
187+
if (islower(c)) ++cnt;
188+
else if (c == '@')
189+
{
190+
sx = i;
191+
sy = j;
192+
}
193+
}
194+
}
195+
queue<vector<int>> q;
196+
q.push({sx, sy, 0});
197+
int mask = (1 << cnt) - 1;
198+
vector<vector<vector<bool>>> vis(m, vector<vector<bool>>(n, vector<bool>(1 << cnt)));
199+
vis[sx][sy][0] = true;
200+
int ans = 0;
201+
vector<int> dirs = {-1, 0, 1, 0, -1};
202+
while (!q.empty())
203+
{
204+
for (int t = q.size(); t; --t)
205+
{
206+
auto p = q.front();
207+
q.pop();
208+
int i = p[0], j = p[1], state = p[2];
209+
if (state == mask) return ans;
210+
for (int k = 0; k < 4; ++k)
211+
{
212+
int nxt = state;
213+
int x = i + dirs[k], y = j + dirs[k + 1];
214+
if (x >= 0 && x < m && y >= 0 && y < n)
215+
{
216+
char c = grid[x][y];
217+
if (c == '#' || (isupper(c) && (nxt & (1 << (c - 'A'))) == 0)) continue;
218+
if (islower(c)) nxt |= 1 << (c - 'a');
219+
if (!vis[x][y][nxt])
220+
{
221+
vis[x][y][nxt] = true;
222+
q.push({x, y, nxt});
223+
}
224+
}
225+
}
226+
}
227+
++ans;
228+
}
229+
return -1;
230+
}
231+
};
232+
```
85233
234+
### **Go**
235+
236+
```go
237+
func shortestPathAllKeys(grid []string) int {
238+
m, n := len(grid), len(grid[0])
239+
cnt := 0
240+
sx, sy := 0, 0
241+
for i, row := range grid {
242+
for j, c := range row {
243+
if 'a' <= c && c <= 'z' {
244+
cnt++
245+
} else if c == '@' {
246+
sx, sy = i, j
247+
}
248+
}
249+
}
250+
q := [][]int{{sx, sy, 0}}
251+
vis := make([][][]bool, m)
252+
for i := range vis {
253+
vis[i] = make([][]bool, n)
254+
for j := range vis[i] {
255+
vis[i][j] = make([]bool, 1<<cnt)
256+
}
257+
}
258+
vis[sx][sy][0] = true
259+
dirs := []int{-1, 0, 1, 0, -1}
260+
ans := 0
261+
mask := (1 << cnt) - 1
262+
for len(q) > 0 {
263+
for t := len(q); t > 0; t-- {
264+
p := q[0]
265+
q = q[1:]
266+
i, j, state := p[0], p[1], p[2]
267+
if state == mask {
268+
return ans
269+
}
270+
for k := 0; k < 4; k++ {
271+
nxt := state
272+
x, y := i+dirs[k], j+dirs[k+1]
273+
if x >= 0 && x < m && y >= 0 && y < n {
274+
c := grid[x][y]
275+
if c == '#' {
276+
continue
277+
}
278+
if 'A' <= c && c <= 'Z' && (nxt&(1<<(c-'A'))) == 0 {
279+
continue
280+
}
281+
if 'a' <= c && c <= 'z' {
282+
nxt |= 1 << (c - 'a')
283+
}
284+
if !vis[x][y][nxt] {
285+
vis[x][y][nxt] = true
286+
q = append(q, []int{x, y, nxt})
287+
}
288+
}
289+
}
290+
}
291+
ans++
292+
}
293+
return -1
294+
}
86295
```
87296

88297
### **...**

0 commit comments

Comments
 (0)