Skip to content

Commit 5510b0e

Browse files
committed
feat: add solutions to lc problem: No.1778
No.1778.Shortest Path in a Hidden Grid
1 parent 92cdddb commit 5510b0e

File tree

8 files changed

+364
-9
lines changed

8 files changed

+364
-9
lines changed

.github/workflows/starcharts.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
name: update-starcharts
22

33
on:
4-
schedule:
5-
- cron: "0 0/12 * * *"
64
workflow_dispatch:
75

86
jobs:
97
update-readme:
108
name: Generate starcharts
119
runs-on: ubuntu-latest
10+
if: github.repository == 'doocs/leetcode'
1211
steps:
1312
- uses: MaoLongLong/actions-starcharts@main
1413
with:

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@
164164

165165
## Stars 趋势
166166

167-
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/images/starcharts.svg" alt="Stargazers over time" /></a>
167+
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://starchart.cc/doocs/leetcode.svg" alt="Stargazers over time" /></a>
168+
169+
<!-- 这里先使用 https://starchart.cc/ 自动刷新 stars 数据,之后若有问题,可以使用 GitHub Action: starcharts.yml -->
170+
<!-- <a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/images/starcharts.svg" alt="Stargazers over time" /></a> -->
168171

169172
## 贡献者
170173

README_EN.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ You can also contribute to [doocs/leetcode](https://github.com/doocs/leetcode) u
157157

158158
## Stargazers over time
159159

160-
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/images/starcharts.svg" alt="Stargazers over time" /></a>
160+
<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://starchart.cc/doocs/leetcode.svg" alt="Stargazers over time" /></a>
161+
162+
<!-- <a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/images/starcharts.svg" alt="Stargazers over time" /></a> -->
161163

162164
## Contributors
163165

solution/1700-1799/1778.Shortest Path in a Hidden Grid/README.md

+121-2
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,141 @@ The robot is initially standing on cell (1, 0), denoted by the -1.
9090

9191
<!-- 这里可写通用的实现逻辑 -->
9292

93+
**方法一:DFS 建图 + BFS 求最短路**
94+
95+
相似题目:[1810. 隐藏网格下的最小消耗路径](/solution/1800-1899/1810.Minimum%20Path%20Cost%20in%20a%20Hidden%20Grid/README.md)
96+
9397
<!-- tabs:start -->
9498

9599
### **Python3**
96100

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

99103
```python
100-
104+
# """
105+
# This is GridMaster's API interface.
106+
# You should not implement it, or speculate about its implementation
107+
# """
108+
# class GridMaster(object):
109+
# def canMove(self, direction: str) -> bool:
110+
#
111+
#
112+
# def move(self, direction: str) -> bool:
113+
#
114+
#
115+
# def isTarget(self) -> None:
116+
#
117+
#
118+
119+
class Solution(object):
120+
def findShortestPath(self, master: 'GridMaster') -> int:
121+
def dfs(i, j):
122+
nonlocal target
123+
if master.isTarget():
124+
target = (i, j)
125+
for dir, ndir, a, b in dirs:
126+
x, y = i + a, j + b
127+
if master.canMove(dir) and (x, y) not in s:
128+
s.add((x, y))
129+
master.move(dir)
130+
dfs(x, y)
131+
master.move(ndir)
132+
133+
target = None
134+
s = set()
135+
dirs = [['U', 'D', -1, 0], ['D', 'U', 1, 0],
136+
['L', 'R', 0, -1], ['R', 'L', 0, 1]]
137+
dfs(0, 0)
138+
if target is None:
139+
return -1
140+
s.remove((0, 0))
141+
q = deque([(0, 0)])
142+
ans = -1
143+
while q:
144+
ans += 1
145+
for _ in range(len(q)):
146+
i, j = q.popleft()
147+
if (i, j) == target:
148+
return ans
149+
for _, _, a, b in dirs:
150+
x, y = i + a, j + b
151+
if (x, y) in s:
152+
s.remove((x, y))
153+
q.append((x, y))
154+
return -1
101155
```
102156

103157
### **Java**
104158

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

107161
```java
108-
162+
/**
163+
* // This is the GridMaster's API interface.
164+
* // You should not implement it, or speculate about its implementation
165+
* class GridMaster {
166+
* boolean canMove(char direction);
167+
* void move(char direction);
168+
* boolean isTarget();
169+
* }
170+
*/
171+
172+
class Solution {
173+
private static final char[] dir = {'U', 'R', 'D', 'L'};
174+
private static final char[] ndir = {'D', 'L', 'U', 'R'};
175+
private static final int[] dirs = {-1, 0, 1, 0, -1};
176+
private static final int N = 1010;
177+
private Set<Integer> s;
178+
private int[] target;
179+
180+
public int findShortestPath(GridMaster master) {
181+
target = null;
182+
s = new HashSet<>();
183+
s.add(0);
184+
dfs(0, 0, master);
185+
if (target == null) {
186+
return -1;
187+
}
188+
s.remove(0);
189+
Deque<int[]> q = new ArrayDeque<>();
190+
q.offer(new int[]{0, 0});
191+
int ans = -1;
192+
while (!q.isEmpty()) {
193+
++ans;
194+
for (int n = q.size(); n > 0; --n) {
195+
int[] p = q.poll();
196+
int i = p[0], j = p[1];
197+
if (target[0] == i && target[1] == j) {
198+
return ans;
199+
}
200+
for (int k = 0; k < 4; ++k) {
201+
int x = i + dirs[k], y = j + dirs[k + 1];
202+
if (s.contains(x * N + y)) {
203+
s.remove(x * N + y);
204+
q.offer(new int[]{x, y});
205+
}
206+
}
207+
}
208+
}
209+
return -1;
210+
}
211+
212+
private void dfs(int i, int j, GridMaster master) {
213+
if (master.isTarget()) {
214+
target = new int[]{i, j};
215+
}
216+
for (int k = 0; k < 4; ++k) {
217+
char d = dir[k], nd = ndir[k];
218+
int x = i + dirs[k], y = j + dirs[k + 1];
219+
if (master.canMove(d) && !s.contains(x * N + y)) {
220+
s.add(x * N + y);
221+
master.move(d);
222+
dfs(x, y, master);
223+
master.move(nd);
224+
}
225+
}
226+
}
227+
}
109228
```
110229

111230
### **...**

solution/1700-1799/1778.Shortest Path in a Hidden Grid/README_EN.md

+117-2
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,128 @@ We now know that the target is the cell (0, 1), and the shortest path to the tar
9191
### **Python3**
9292

9393
```python
94-
94+
# """
95+
# This is GridMaster's API interface.
96+
# You should not implement it, or speculate about its implementation
97+
# """
98+
# class GridMaster(object):
99+
# def canMove(self, direction: str) -> bool:
100+
#
101+
#
102+
# def move(self, direction: str) -> bool:
103+
#
104+
#
105+
# def isTarget(self) -> None:
106+
#
107+
#
108+
109+
class Solution(object):
110+
def findShortestPath(self, master: 'GridMaster') -> int:
111+
def dfs(i, j):
112+
nonlocal target
113+
if master.isTarget():
114+
target = (i, j)
115+
for dir, ndir, a, b in dirs:
116+
x, y = i + a, j + b
117+
if master.canMove(dir) and (x, y) not in s:
118+
s.add((x, y))
119+
master.move(dir)
120+
dfs(x, y)
121+
master.move(ndir)
122+
123+
target = None
124+
s = set()
125+
dirs = [['U', 'D', -1, 0], ['D', 'U', 1, 0],
126+
['L', 'R', 0, -1], ['R', 'L', 0, 1]]
127+
dfs(0, 0)
128+
if target is None:
129+
return -1
130+
s.remove((0, 0))
131+
q = deque([(0, 0)])
132+
ans = -1
133+
while q:
134+
ans += 1
135+
for _ in range(len(q)):
136+
i, j = q.popleft()
137+
if (i, j) == target:
138+
return ans
139+
for _, _, a, b in dirs:
140+
x, y = i + a, j + b
141+
if (x, y) in s:
142+
s.remove((x, y))
143+
q.append((x, y))
144+
return -1
95145
```
96146

97147
### **Java**
98148

99149
```java
100-
150+
/**
151+
* // This is the GridMaster's API interface.
152+
* // You should not implement it, or speculate about its implementation
153+
* class GridMaster {
154+
* boolean canMove(char direction);
155+
* void move(char direction);
156+
* boolean isTarget();
157+
* }
158+
*/
159+
160+
class Solution {
161+
private static final char[] dir = {'U', 'R', 'D', 'L'};
162+
private static final char[] ndir = {'D', 'L', 'U', 'R'};
163+
private static final int[] dirs = {-1, 0, 1, 0, -1};
164+
private static final int N = 1010;
165+
private Set<Integer> s;
166+
private int[] target;
167+
168+
public int findShortestPath(GridMaster master) {
169+
target = null;
170+
s = new HashSet<>();
171+
s.add(0);
172+
dfs(0, 0, master);
173+
if (target == null) {
174+
return -1;
175+
}
176+
s.remove(0);
177+
Deque<int[]> q = new ArrayDeque<>();
178+
q.offer(new int[]{0, 0});
179+
int ans = -1;
180+
while (!q.isEmpty()) {
181+
++ans;
182+
for (int n = q.size(); n > 0; --n) {
183+
int[] p = q.poll();
184+
int i = p[0], j = p[1];
185+
if (target[0] == i && target[1] == j) {
186+
return ans;
187+
}
188+
for (int k = 0; k < 4; ++k) {
189+
int x = i + dirs[k], y = j + dirs[k + 1];
190+
if (s.contains(x * N + y)) {
191+
s.remove(x * N + y);
192+
q.offer(new int[]{x, y});
193+
}
194+
}
195+
}
196+
}
197+
return -1;
198+
}
199+
200+
private void dfs(int i, int j, GridMaster master) {
201+
if (master.isTarget()) {
202+
target = new int[]{i, j};
203+
}
204+
for (int k = 0; k < 4; ++k) {
205+
char d = dir[k], nd = ndir[k];
206+
int x = i + dirs[k], y = j + dirs[k + 1];
207+
if (master.canMove(d) && !s.contains(x * N + y)) {
208+
s.add(x * N + y);
209+
master.move(d);
210+
dfs(x, y, master);
211+
master.move(nd);
212+
}
213+
}
214+
}
215+
}
101216
```
102217

103218
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* // This is the GridMaster's API interface.
3+
* // You should not implement it, or speculate about its implementation
4+
* class GridMaster {
5+
* boolean canMove(char direction);
6+
* void move(char direction);
7+
* boolean isTarget();
8+
* }
9+
*/
10+
11+
class Solution {
12+
private static final char[] dir = {'U', 'R', 'D', 'L'};
13+
private static final char[] ndir = {'D', 'L', 'U', 'R'};
14+
private static final int[] dirs = {-1, 0, 1, 0, -1};
15+
private static final int N = 1010;
16+
private Set<Integer> s;
17+
private int[] target;
18+
19+
public int findShortestPath(GridMaster master) {
20+
target = null;
21+
s = new HashSet<>();
22+
s.add(0);
23+
dfs(0, 0, master);
24+
if (target == null) {
25+
return -1;
26+
}
27+
s.remove(0);
28+
Deque<int[]> q = new ArrayDeque<>();
29+
q.offer(new int[]{0, 0});
30+
int ans = -1;
31+
while (!q.isEmpty()) {
32+
++ans;
33+
for (int n = q.size(); n > 0; --n) {
34+
int[] p = q.poll();
35+
int i = p[0], j = p[1];
36+
if (target[0] == i && target[1] == j) {
37+
return ans;
38+
}
39+
for (int k = 0; k < 4; ++k) {
40+
int x = i + dirs[k], y = j + dirs[k + 1];
41+
if (s.contains(x * N + y)) {
42+
s.remove(x * N + y);
43+
q.offer(new int[]{x, y});
44+
}
45+
}
46+
}
47+
}
48+
return -1;
49+
}
50+
51+
private void dfs(int i, int j, GridMaster master) {
52+
if (master.isTarget()) {
53+
target = new int[]{i, j};
54+
}
55+
for (int k = 0; k < 4; ++k) {
56+
char d = dir[k], nd = ndir[k];
57+
int x = i + dirs[k], y = j + dirs[k + 1];
58+
if (master.canMove(d) && !s.contains(x * N + y)) {
59+
s.add(x * N + y);
60+
master.move(d);
61+
dfs(x, y, master);
62+
master.move(nd);
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)