Skip to content

Commit 1ea57a8

Browse files
committed
feat: add solutions to lcp problem: No.63
1 parent 7895a5f commit 1ea57a8

File tree

5 files changed

+356
-0
lines changed

5 files changed

+356
-0
lines changed

lcp/LCP 63. 弹珠游戏/README.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,208 @@
6464

6565
<!-- 这里可写通用的实现逻辑 -->
6666

67+
**方法一:模拟**
68+
69+
我们注意到,从不同的位置打入弹珠,弹珠的前进路线不会重叠。因此,我们可以枚举所有可能的打入位置,然后模拟弹珠的前进过程,判断是否能够入洞,是则将该位置加入答案。
70+
71+
在实现上,我们定义一个方向数组 $dirs=[0,1,0,-1,0]$,对于 $d \in [0,..3]$,其中 $(dirs[d], dirs[d + 1])$ 表示弹珠的前进方向,分别对应 “右、下、左、上”四个方向。如果弹珠遇到 “W” 转向器,则 $d=(d+3) \bmod 4$,如果遇到 “E” 转向器,则 $d=(d+1) \bmod 4$。
72+
73+
时间复杂度 $O(m \times n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为弹珠盘的行数和列数。
74+
6775
<!-- tabs:start -->
6876

6977
### **Python3**
7078

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

7381
```python
82+
class Solution:
83+
def ballGame(self, num: int, plate: List[str]) -> List[List[int]]:
84+
def check(i, j, d):
85+
k = num
86+
while plate[i][j] != 'O':
87+
if k == 0:
88+
return False
89+
if plate[i][j] == 'W':
90+
d = (d + 3) % 4
91+
elif plate[i][j] == 'E':
92+
d = (d + 1) % 4
93+
i, j = i + dirs[d], j + dirs[d + 1]
94+
if not (0 <= i < m and 0 <= j < n):
95+
return False
96+
k -= 1
97+
return True
7498

99+
dirs = (0, 1, 0, -1, 0)
100+
m, n = len(plate), len(plate[0])
101+
ans = []
102+
for i in range(1, m - 1):
103+
if plate[i][0] == '.' and check(i, 0, 0):
104+
ans.append([i, 0])
105+
if plate[i][n - 1] == '.' and check(i, n - 1, 2):
106+
ans.append([i, n - 1])
107+
for j in range(1, n - 1):
108+
if plate[0][j] == '.' and check(0, j, 1):
109+
ans.append([0, j])
110+
if plate[m - 1][j] == '.' and check(m - 1, j, 3):
111+
ans.append([m - 1, j])
112+
return ans
75113
```
76114

77115
### **Java**
78116

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

81119
```java
120+
class Solution {
121+
private String[] plate;
122+
private int num;
123+
private int m;
124+
private int n;
125+
private final int[] dirs = {0, 1, 0, -1, 0};
126+
127+
public int[][] ballGame(int num, String[] plate) {
128+
this.num = num;
129+
this.plate = plate;
130+
this.m = plate.length;
131+
this.n = plate[0].length();
132+
List<int[]> ans = new ArrayList<>();
133+
for (int i = 1; i < m - 1; ++i) {
134+
if (plate[i].charAt(0) == '.' && check(i, 0, 0)) {
135+
ans.add(new int[]{i, 0});
136+
}
137+
if (plate[i].charAt(n - 1) == '.' && check(i, n - 1, 2)) {
138+
ans.add(new int[]{i, n - 1});
139+
}
140+
}
141+
for (int j = 1; j < n - 1; ++j) {
142+
if (plate[0].charAt(j) == '.' && check(0, j, 1)) {
143+
ans.add(new int[]{0, j});
144+
}
145+
if (plate[m - 1].charAt(j) == '.' && check(m - 1, j, 3)) {
146+
ans.add(new int[]{m - 1, j});
147+
}
148+
}
149+
return ans.toArray(new int[0][]);
150+
}
151+
152+
private boolean check(int i, int j, int d) {
153+
int k = num;
154+
while (plate[i].charAt(j) != 'O') {
155+
if (k == 0) {
156+
return false;
157+
}
158+
if (plate[i].charAt(j) == 'W') {
159+
d = (d + 3) % 4;
160+
} else if (plate[i].charAt(j) == 'E') {
161+
d = (d + 1) % 4;
162+
}
163+
i = i + dirs[d];
164+
j = j + dirs[d + 1];
165+
if (i < 0 || i >= m || j < 0 || j >= n) {
166+
return false;
167+
}
168+
--k;
169+
}
170+
return true;
171+
}
172+
}
173+
```
174+
175+
### **C++**
176+
177+
```cpp
178+
class Solution {
179+
public:
180+
vector<vector<int>> ballGame(int num, vector<string>& plate) {
181+
int m = plate.size(), n = plate[0].size();
182+
vector<vector<int>> ans;
183+
int dirs[5] = {0, 1, 0, -1, 0};
184+
auto check = [&](int i, int j, int d) -> bool {
185+
int k = num;
186+
while (plate[i][j] != 'O') {
187+
if (k == 0) {
188+
return false;
189+
}
190+
if (plate[i][j] == 'W') {
191+
d = (d + 3) % 4;
192+
} else if (plate[i][j] == 'E') {
193+
d = (d + 1) % 4;
194+
}
195+
i += dirs[d];
196+
j += dirs[d + 1];
197+
if (i < 0 || i >= m || j < 0 || j >= n) {
198+
return false;
199+
}
200+
--k;
201+
}
202+
return true;
203+
};
204+
for (int i = 1; i < m - 1; ++i) {
205+
if (plate[i][0] == '.' && check(i, 0, 0)) {
206+
ans.push_back({i, 0});
207+
}
208+
if (plate[i][n - 1] == '.' && check(i, n - 1, 2)) {
209+
ans.push_back({i, n - 1});
210+
}
211+
}
212+
for (int j = 1; j < n - 1; ++j) {
213+
if (plate[0][j] == '.' && check(0, j, 1)) {
214+
ans.push_back({0, j});
215+
}
216+
if (plate[m - 1][j] == '.' && check(m - 1, j, 3)) {
217+
ans.push_back({m - 1, j});
218+
}
219+
}
220+
return ans;
221+
}
222+
};
223+
```
224+
225+
### **Go**
82226
227+
```go
228+
func ballGame(num int, plate []string) (ans [][]int) {
229+
dirs := [5]int{0, 1, 0, -1, 0}
230+
m, n := len(plate), len(plate[0])
231+
check := func(i, j, d int) bool {
232+
k := num
233+
for plate[i][j] != 'O' {
234+
if k == 0 {
235+
return false
236+
}
237+
if plate[i][j] == 'W' {
238+
d = (d + 3) % 4
239+
} else if plate[i][j] == 'E' {
240+
d = (d + 1) % 4
241+
}
242+
i += dirs[d]
243+
j += dirs[d+1]
244+
if i < 0 || i >= m || j < 0 || j >= n {
245+
return false
246+
}
247+
k--
248+
}
249+
return true
250+
}
251+
for i := 1; i < m-1; i++ {
252+
if plate[i][0] == '.' && check(i, 0, 0) {
253+
ans = append(ans, []int{i, 0})
254+
}
255+
if plate[i][n-1] == '.' && check(i, n-1, 2) {
256+
ans = append(ans, []int{i, n - 1})
257+
}
258+
}
259+
for j := 1; j < n-1; j++ {
260+
if plate[0][j] == '.' && check(0, j, 1) {
261+
ans = append(ans, []int{0, j})
262+
}
263+
if plate[m-1][j] == '.' && check(m-1, j, 3) {
264+
ans = append(ans, []int{m - 1, j})
265+
}
266+
}
267+
return
268+
}
83269
```
84270

85271
### **...**

lcp/LCP 63. 弹珠游戏/Solution.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> ballGame(int num, vector<string>& plate) {
4+
int m = plate.size(), n = plate[0].size();
5+
vector<vector<int>> ans;
6+
int dirs[5] = {0, 1, 0, -1, 0};
7+
auto check = [&](int i, int j, int d) -> bool {
8+
int k = num;
9+
while (plate[i][j] != 'O') {
10+
if (k == 0) {
11+
return false;
12+
}
13+
if (plate[i][j] == 'W') {
14+
d = (d + 3) % 4;
15+
} else if (plate[i][j] == 'E') {
16+
d = (d + 1) % 4;
17+
}
18+
i += dirs[d];
19+
j += dirs[d + 1];
20+
if (i < 0 || i >= m || j < 0 || j >= n) {
21+
return false;
22+
}
23+
--k;
24+
}
25+
return true;
26+
};
27+
for (int i = 1; i < m - 1; ++i) {
28+
if (plate[i][0] == '.' && check(i, 0, 0)) {
29+
ans.push_back({i, 0});
30+
}
31+
if (plate[i][n - 1] == '.' && check(i, n - 1, 2)) {
32+
ans.push_back({i, n - 1});
33+
}
34+
}
35+
for (int j = 1; j < n - 1; ++j) {
36+
if (plate[0][j] == '.' && check(0, j, 1)) {
37+
ans.push_back({0, j});
38+
}
39+
if (plate[m - 1][j] == '.' && check(m - 1, j, 3)) {
40+
ans.push_back({m - 1, j});
41+
}
42+
}
43+
return ans;
44+
}
45+
};

lcp/LCP 63. 弹珠游戏/Solution.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
func ballGame(num int, plate []string) (ans [][]int) {
2+
dirs := [5]int{0, 1, 0, -1, 0}
3+
m, n := len(plate), len(plate[0])
4+
check := func(i, j, d int) bool {
5+
k := num
6+
for plate[i][j] != 'O' {
7+
if k == 0 {
8+
return false
9+
}
10+
if plate[i][j] == 'W' {
11+
d = (d + 3) % 4
12+
} else if plate[i][j] == 'E' {
13+
d = (d + 1) % 4
14+
}
15+
i += dirs[d]
16+
j += dirs[d+1]
17+
if i < 0 || i >= m || j < 0 || j >= n {
18+
return false
19+
}
20+
k--
21+
}
22+
return true
23+
}
24+
for i := 1; i < m-1; i++ {
25+
if plate[i][0] == '.' && check(i, 0, 0) {
26+
ans = append(ans, []int{i, 0})
27+
}
28+
if plate[i][n-1] == '.' && check(i, n-1, 2) {
29+
ans = append(ans, []int{i, n - 1})
30+
}
31+
}
32+
for j := 1; j < n-1; j++ {
33+
if plate[0][j] == '.' && check(0, j, 1) {
34+
ans = append(ans, []int{0, j})
35+
}
36+
if plate[m-1][j] == '.' && check(m-1, j, 3) {
37+
ans = append(ans, []int{m - 1, j})
38+
}
39+
}
40+
return
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution {
2+
private String[] plate;
3+
private int num;
4+
private int m;
5+
private int n;
6+
private final int[] dirs = {0, 1, 0, -1, 0};
7+
8+
public int[][] ballGame(int num, String[] plate) {
9+
this.num = num;
10+
this.plate = plate;
11+
this.m = plate.length;
12+
this.n = plate[0].length();
13+
List<int[]> ans = new ArrayList<>();
14+
for (int i = 1; i < m - 1; ++i) {
15+
if (plate[i].charAt(0) == '.' && check(i, 0, 0)) {
16+
ans.add(new int[]{i, 0});
17+
}
18+
if (plate[i].charAt(n - 1) == '.' && check(i, n - 1, 2)) {
19+
ans.add(new int[]{i, n - 1});
20+
}
21+
}
22+
for (int j = 1; j < n - 1; ++j) {
23+
if (plate[0].charAt(j) == '.' && check(0, j, 1)) {
24+
ans.add(new int[]{0, j});
25+
}
26+
if (plate[m - 1].charAt(j) == '.' && check(m - 1, j, 3)) {
27+
ans.add(new int[]{m - 1, j});
28+
}
29+
}
30+
return ans.toArray(new int[0][]);
31+
}
32+
33+
private boolean check(int i, int j, int d) {
34+
int k = num;
35+
while (plate[i].charAt(j) != 'O') {
36+
if (k == 0) {
37+
return false;
38+
}
39+
if (plate[i].charAt(j) == 'W') {
40+
d = (d + 3) % 4;
41+
} else if (plate[i].charAt(j) == 'E') {
42+
d = (d + 1) % 4;
43+
}
44+
i = i + dirs[d];
45+
j = j + dirs[d + 1];
46+
if (i < 0 || i >= m || j < 0 || j >= n) {
47+
return false;
48+
}
49+
--k;
50+
}
51+
return true;
52+
}
53+
}

lcp/LCP 63. 弹珠游戏/Solution.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def ballGame(self, num: int, plate: List[str]) -> List[List[int]]:
3+
def check(i, j, d):
4+
k = num
5+
while plate[i][j] != 'O':
6+
if k == 0:
7+
return False
8+
if plate[i][j] == 'W':
9+
d = (d + 3) % 4
10+
elif plate[i][j] == 'E':
11+
d = (d + 1) % 4
12+
i, j = i + dirs[d], j + dirs[d + 1]
13+
if not (0 <= i < m and 0 <= j < n):
14+
return False
15+
k -= 1
16+
return True
17+
18+
dirs = (0, 1, 0, -1, 0)
19+
m, n = len(plate), len(plate[0])
20+
ans = []
21+
for i in range(1, m - 1):
22+
if plate[i][0] == '.' and check(i, 0, 0):
23+
ans.append([i, 0])
24+
if plate[i][n - 1] == '.' and check(i, n - 1, 2):
25+
ans.append([i, n - 1])
26+
for j in range(1, n - 1):
27+
if plate[0][j] == '.' and check(0, j, 1):
28+
ans.append([0, j])
29+
if plate[m - 1][j] == '.' and check(m - 1, j, 3):
30+
ans.append([m - 1, j])
31+
return ans

0 commit comments

Comments
 (0)