Skip to content

Commit c5a217c

Browse files
committed
feat: add solutions to lc problem: No.1810
No.1810.Minimum Path Cost in a Hidden Grid
1 parent 3d11db3 commit c5a217c

File tree

4 files changed

+359
-4
lines changed

4 files changed

+359
-4
lines changed

solution/1800-1899/1810.Minimum Path Cost in a Hidden Grid/README.md

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,141 @@
9595

9696
<!-- 这里可写通用的实现逻辑 -->
9797

98+
**方法一:DFS 建图 + 堆优化版 Dijkstra 算法**
99+
98100
<!-- tabs:start -->
99101

100102
### **Python3**
101103

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

104106
```python
105-
107+
# """
108+
# This is GridMaster's API interface.
109+
# You should not implement it, or speculate about its implementation
110+
# """
111+
# class GridMaster(object):
112+
# def canMove(self, direction: str) -> bool:
113+
#
114+
#
115+
# def move(self, direction: str) -> int:
116+
#
117+
#
118+
# def isTarget(self) -> None:
119+
#
120+
#
121+
122+
class Solution(object):
123+
def findShortestPath(self, master: 'GridMaster') -> int:
124+
def dfs(i, j):
125+
nonlocal target
126+
if master.isTarget():
127+
target = (i, j)
128+
for dir, (a, b, ndir) in dirs.items():
129+
x, y = i + a, j + b
130+
if 0 <= x < N and 0 <= y < N and master.canMove(dir) and g[x][y] == -1:
131+
g[x][y] = master.move(dir)
132+
dfs(x, y)
133+
master.move(ndir)
134+
135+
target = (-1, -1)
136+
N = 200
137+
INF = 0x3f3f3f3f
138+
g = [[-1] * N for _ in range(N)]
139+
dirs = {
140+
'U': (-1, 0, 'D'),
141+
'D': (1, 0, 'U'),
142+
'L': (0, -1, 'R'),
143+
'R': (0, 1, 'L')
144+
}
145+
dfs(100, 100)
146+
if target == (-1, -1):
147+
return -1
148+
q = [(0, 100, 100)]
149+
dist = [[INF] * N for _ in range(N)]
150+
dist[100][100] = 0
151+
while q:
152+
w, i, j = heappop(q)
153+
if (i, j) == target:
154+
return w
155+
for a, b, _ in dirs.values():
156+
x, y = i + a, j + b
157+
if 0 <= x < N and 0 <= y < N and g[x][y] != -1 and dist[x][y] > w + g[x][y]:
158+
dist[x][y] = w + g[x][y]
159+
heappush(q, (dist[x][y], x, y))
160+
return 0
106161
```
107162

108163
### **Java**
109164

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

112167
```java
113-
168+
/**
169+
* // This is the GridMaster's API interface.
170+
* // You should not implement it, or speculate about its implementation
171+
* class GridMaster {
172+
* boolean canMove(char direction);
173+
* int move(char direction);
174+
* boolean isTarget();
175+
* }
176+
*/
177+
178+
class Solution {
179+
private static final char[] dir = {'U', 'R', 'D', 'L'};
180+
private static final char[] ndir = {'D', 'L', 'U', 'R'};
181+
private static final int[] dirs = {-1, 0, 1, 0, -1};
182+
private static final int N = 200;
183+
private static final int INF = 0x3f3f3f3f;
184+
private static int[][] g = new int[N][N];
185+
private static int[][] dist = new int[N][N];
186+
private int[] target;
187+
188+
public int findShortestPath(GridMaster master) {
189+
target = new int[]{-1, -1};
190+
for (int i = 0; i < N; ++i) {
191+
Arrays.fill(g[i], -1);
192+
Arrays.fill(dist[i], INF);
193+
}
194+
dfs(100, 100, master);
195+
if (target[0] == -1 && target[1] == -1) {
196+
return -1;
197+
}
198+
PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
199+
q.offer(new int[]{0, 100, 100});
200+
dist[100][100] = 0;
201+
while (!q.isEmpty()) {
202+
int[] p = q.poll();
203+
int w = p[0], i = p[1], j = p[2];
204+
if (i == target[0] && j == target[1]) {
205+
return w;
206+
}
207+
for (int k = 0; k < 4; ++k) {
208+
int x = i + dirs[k], y = j + dirs[k + 1];
209+
if (x >= 0 && x < N && y >= 0 && y < N && g[x][y] != -1 && dist[x][y] > w + g[x][y]) {
210+
dist[x][y] = w + g[x][y];
211+
q.offer(new int[]{dist[x][y], x, y});
212+
}
213+
}
214+
}
215+
return 0;
216+
}
217+
218+
private void dfs(int i, int j, GridMaster master) {
219+
if (master.isTarget()) {
220+
target = new int[]{i, j};
221+
}
222+
for (int k = 0; k < 4; ++k) {
223+
char d = dir[k], nd = ndir[k];
224+
int x = i + dirs[k], y = j + dirs[k + 1];
225+
if (x >= 0 && x < N && y >= 0 && y < N && master.canMove(d) && g[x][y] == -1) {
226+
g[x][y] = master.move(d);
227+
dfs(x, y, master);
228+
master.move(nd);
229+
}
230+
}
231+
}
232+
}
114233
```
115234

116235
### **...**

solution/1800-1899/1810.Minimum Path Cost in a Hidden Grid/README_EN.md

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,130 @@ We now know that the target is the cell (1, 0), and the minimum total cost to re
9494
### **Python3**
9595

9696
```python
97-
97+
# """
98+
# This is GridMaster's API interface.
99+
# You should not implement it, or speculate about its implementation
100+
# """
101+
# class GridMaster(object):
102+
# def canMove(self, direction: str) -> bool:
103+
#
104+
#
105+
# def move(self, direction: str) -> int:
106+
#
107+
#
108+
# def isTarget(self) -> None:
109+
#
110+
#
111+
112+
class Solution(object):
113+
def findShortestPath(self, master: 'GridMaster') -> int:
114+
def dfs(i, j):
115+
nonlocal target
116+
if master.isTarget():
117+
target = (i, j)
118+
for dir, (a, b, ndir) in dirs.items():
119+
x, y = i + a, j + b
120+
if 0 <= x < N and 0 <= y < N and master.canMove(dir) and g[x][y] == -1:
121+
g[x][y] = master.move(dir)
122+
dfs(x, y)
123+
master.move(ndir)
124+
125+
target = (-1, -1)
126+
N = 200
127+
INF = 0x3f3f3f3f
128+
g = [[-1] * N for _ in range(N)]
129+
dirs = {
130+
'U': (-1, 0, 'D'),
131+
'D': (1, 0, 'U'),
132+
'L': (0, -1, 'R'),
133+
'R': (0, 1, 'L')
134+
}
135+
dfs(100, 100)
136+
if target == (-1, -1):
137+
return -1
138+
q = [(0, 100, 100)]
139+
dist = [[INF] * N for _ in range(N)]
140+
dist[100][100] = 0
141+
while q:
142+
w, i, j = heappop(q)
143+
if (i, j) == target:
144+
return w
145+
for a, b, _ in dirs.values():
146+
x, y = i + a, j + b
147+
if 0 <= x < N and 0 <= y < N and g[x][y] != -1 and dist[x][y] > w + g[x][y]:
148+
dist[x][y] = w + g[x][y]
149+
heappush(q, (dist[x][y], x, y))
150+
return 0
98151
```
99152

100153
### **Java**
101154

102155
```java
103-
156+
/**
157+
* // This is the GridMaster's API interface.
158+
* // You should not implement it, or speculate about its implementation
159+
* class GridMaster {
160+
* boolean canMove(char direction);
161+
* int move(char direction);
162+
* boolean isTarget();
163+
* }
164+
*/
165+
166+
class Solution {
167+
private static final char[] dir = {'U', 'R', 'D', 'L'};
168+
private static final char[] ndir = {'D', 'L', 'U', 'R'};
169+
private static final int[] dirs = {-1, 0, 1, 0, -1};
170+
private static final int N = 200;
171+
private static final int INF = 0x3f3f3f3f;
172+
private static int[][] g = new int[N][N];
173+
private static int[][] dist = new int[N][N];
174+
private int[] target;
175+
176+
public int findShortestPath(GridMaster master) {
177+
target = new int[]{-1, -1};
178+
for (int i = 0; i < N; ++i) {
179+
Arrays.fill(g[i], -1);
180+
Arrays.fill(dist[i], INF);
181+
}
182+
dfs(100, 100, master);
183+
if (target[0] == -1 && target[1] == -1) {
184+
return -1;
185+
}
186+
PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
187+
q.offer(new int[]{0, 100, 100});
188+
dist[100][100] = 0;
189+
while (!q.isEmpty()) {
190+
int[] p = q.poll();
191+
int w = p[0], i = p[1], j = p[2];
192+
if (i == target[0] && j == target[1]) {
193+
return w;
194+
}
195+
for (int k = 0; k < 4; ++k) {
196+
int x = i + dirs[k], y = j + dirs[k + 1];
197+
if (x >= 0 && x < N && y >= 0 && y < N && g[x][y] != -1 && dist[x][y] > w + g[x][y]) {
198+
dist[x][y] = w + g[x][y];
199+
q.offer(new int[]{dist[x][y], x, y});
200+
}
201+
}
202+
}
203+
return 0;
204+
}
205+
206+
private void dfs(int i, int j, GridMaster master) {
207+
if (master.isTarget()) {
208+
target = new int[]{i, j};
209+
}
210+
for (int k = 0; k < 4; ++k) {
211+
char d = dir[k], nd = ndir[k];
212+
int x = i + dirs[k], y = j + dirs[k + 1];
213+
if (x >= 0 && x < N && y >= 0 && y < N && master.canMove(d) && g[x][y] == -1) {
214+
g[x][y] = master.move(d);
215+
dfs(x, y, master);
216+
master.move(nd);
217+
}
218+
}
219+
}
220+
}
104221
```
105222

106223
### **...**
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
* int 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 = 200;
16+
private static final int INF = 0x3f3f3f3f;
17+
private static int[][] g = new int[N][N];
18+
private static int[][] dist = new int[N][N];
19+
private int[] target;
20+
21+
public int findShortestPath(GridMaster master) {
22+
target = new int[]{-1, -1};
23+
for (int i = 0; i < N; ++i) {
24+
Arrays.fill(g[i], -1);
25+
Arrays.fill(dist[i], INF);
26+
}
27+
dfs(100, 100, master);
28+
if (target[0] == -1 && target[1] == -1) {
29+
return -1;
30+
}
31+
PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
32+
q.offer(new int[]{0, 100, 100});
33+
dist[100][100] = 0;
34+
while (!q.isEmpty()) {
35+
int[] p = q.poll();
36+
int w = p[0], i = p[1], j = p[2];
37+
if (i == target[0] && j == target[1]) {
38+
return w;
39+
}
40+
for (int k = 0; k < 4; ++k) {
41+
int x = i + dirs[k], y = j + dirs[k + 1];
42+
if (x >= 0 && x < N && y >= 0 && y < N && g[x][y] != -1 && dist[x][y] > w + g[x][y]) {
43+
dist[x][y] = w + g[x][y];
44+
q.offer(new int[]{dist[x][y], x, y});
45+
}
46+
}
47+
}
48+
return 0;
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 (x >= 0 && x < N && y >= 0 && y < N && master.canMove(d) && g[x][y] == -1) {
59+
g[x][y] = master.move(d);
60+
dfs(x, y, master);
61+
master.move(nd);
62+
}
63+
}
64+
}
65+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# """
2+
# This is GridMaster's API interface.
3+
# You should not implement it, or speculate about its implementation
4+
# """
5+
# class GridMaster(object):
6+
# def canMove(self, direction: str) -> bool:
7+
#
8+
#
9+
# def move(self, direction: str) -> int:
10+
#
11+
#
12+
# def isTarget(self) -> None:
13+
#
14+
#
15+
16+
class Solution(object):
17+
def findShortestPath(self, master: 'GridMaster') -> int:
18+
def dfs(i, j):
19+
nonlocal target
20+
if master.isTarget():
21+
target = (i, j)
22+
for dir, (a, b, ndir) in dirs.items():
23+
x, y = i + a, j + b
24+
if 0 <= x < N and 0 <= y < N and master.canMove(dir) and g[x][y] == -1:
25+
g[x][y] = master.move(dir)
26+
dfs(x, y)
27+
master.move(ndir)
28+
29+
target = (-1, -1)
30+
N = 200
31+
INF = 0x3f3f3f3f
32+
g = [[-1] * N for _ in range(N)]
33+
dirs = {
34+
'U': (-1, 0, 'D'),
35+
'D': (1, 0, 'U'),
36+
'L': (0, -1, 'R'),
37+
'R': (0, 1, 'L')
38+
}
39+
dfs(100, 100)
40+
if target == (-1, -1):
41+
return -1
42+
q = [(0, 100, 100)]
43+
dist = [[INF] * N for _ in range(N)]
44+
dist[100][100] = 0
45+
while q:
46+
w, i, j = heappop(q)
47+
if (i, j) == target:
48+
return w
49+
for a, b, _ in dirs.values():
50+
x, y = i + a, j + b
51+
if 0 <= x < N and 0 <= y < N and g[x][y] != -1 and dist[x][y] > w + g[x][y]:
52+
dist[x][y] = w + g[x][y]
53+
heappush(q, (dist[x][y], x, y))
54+
return 0

0 commit comments

Comments
 (0)