Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a4602e2

Browse files
committed
feat: add solutions to lc problem: No.1706
No.1706.Where Will the Ball Fall
1 parent 1379feb commit a4602e2

File tree

6 files changed

+354
-4
lines changed

6 files changed

+354
-4
lines changed

‎solution/1700-1799/1706.Where Will the Ball Fall/README.md‎

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,152 @@ b4 球开始放在第 4 列上,会卡在第 2、3 列和第 1 行之间的 "V"
6262
<li><code>grid[i][j]</code> 为 <code>1</code> 或 <code>-1</code></li>
6363
</ul>
6464

65-
6665
## 解法
6766

6867
<!-- 这里可写通用的实现逻辑 -->
6968

69+
球被卡住共有 4 种情况:
70+
71+
1. 球位于最左一列,并且球所在的单元格单元格挡板将球导向左侧
72+
1. 球位于最右一列,并且此单元格挡板将球导向右侧
73+
1. 球所在的单元格挡板将球导向右侧,并且球右侧相邻单元格挡板将球导向左侧
74+
1. 球所在的单元格挡板将球导向左侧,并且球左侧相邻单元格挡板将球导向右侧
75+
76+
DFS 搜索即可。
77+
7078
<!-- tabs:start -->
7179

7280
### **Python3**
7381

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

7684
```python
77-
85+
class Solution:
86+
def findBall(self, grid: List[List[int]]) -> List[int]:
87+
m, n = len(grid), len(grid[0])
88+
89+
def dfs(i, j):
90+
nonlocal m, n
91+
if i == m:
92+
return j
93+
if j == 0 and grid[i][j] == -1:
94+
return -1
95+
if j == n - 1 and grid[i][j] == 1:
96+
return -1
97+
if grid[i][j] == 1 and grid[i][j + 1] == -1:
98+
return -1
99+
if grid[i][j] == -1 and grid[i][j - 1] == 1:
100+
return -1
101+
return dfs(i + 1, j + 1) if grid[i][j] == 1 else dfs(i + 1, j - 1)
102+
103+
return [dfs(0, j) for j in range(n)]
78104
```
79105

80106
### **Java**
81107

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

84110
```java
111+
class Solution {
112+
private int m;
113+
private int n;
114+
private int[][] grid;
115+
116+
public int[] findBall(int[][] grid) {
117+
m = grid.length;
118+
n = grid[0].length;
119+
this.grid = grid;
120+
int[] ans = new int[n];
121+
for (int j = 0; j < n; ++j) {
122+
ans[j] = dfs(0, j);
123+
}
124+
return ans;
125+
}
126+
127+
private int dfs(int i, int j) {
128+
if (i == m) {
129+
return j;
130+
}
131+
if (j == 0 && grid[i][j] == -1) {
132+
return -1;
133+
}
134+
if (j == n - 1 && grid[i][j] == 1) {
135+
return -1;
136+
}
137+
if (grid[i][j] == 1 && grid[i][j + 1] == -1) {
138+
return -1;
139+
}
140+
if (grid[i][j] == -1 && grid[i][j - 1] == 1) {
141+
return -1;
142+
}
143+
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
144+
}
145+
}
146+
```
147+
148+
### **C++**
149+
150+
```cpp
151+
class Solution {
152+
public:
153+
int m, n;
154+
vector<vector<int>> grid;
155+
156+
vector<int> findBall(vector<vector<int>>& grid) {
157+
this->grid = grid;
158+
m = grid.size();
159+
n = grid[0].size();
160+
vector<int> ans(n);
161+
for (int j = 0; j < n; ++j) ans[j] = dfs(0, j);
162+
return ans;
163+
}
164+
165+
int dfs(int i, int j) {
166+
if (i == m) return j;
167+
if (j == 0 && grid[i][j] == -1) return -1;
168+
if (j == n - 1 && grid[i][j] == 1) return -1;
169+
if (grid[i][j] == 1 && grid[i][j + 1] == -1) return -1;
170+
if (grid[i][j] == -1 && grid[i][j - 1] == 1) return -1;
171+
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
172+
}
173+
};
174+
```
85175
176+
### **Go**
177+
178+
```go
179+
func findBall(grid [][]int) []int {
180+
m, n := len(grid), len(grid[0])
181+
182+
var dfs func(i, j int) int
183+
dfs = func(i, j int) int {
184+
if i == m {
185+
return j
186+
}
187+
if j == 0 && grid[i][j] == -1 {
188+
return -1
189+
}
190+
if j == n-1 && grid[i][j] == 1 {
191+
return -1
192+
}
193+
if grid[i][j] == 1 && grid[i][j+1] == -1 {
194+
return -1
195+
}
196+
if grid[i][j] == -1 && grid[i][j-1] == 1 {
197+
return -1
198+
}
199+
if grid[i][j] == 1 {
200+
return dfs(i+1, j+1)
201+
}
202+
return dfs(i+1, j-1)
203+
}
204+
205+
var ans []int
206+
for j := 0; j < n; j++ {
207+
ans = append(ans, dfs(0, j))
208+
}
209+
return ans
210+
}
86211
```
87212

88213
### **...**

‎solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md‎

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,137 @@ Ball b4 is dropped at column 4 and will get stuck on the box between column 2 an
5858
<li><code>grid[i][j]</code> is <code>1</code> or <code>-1</code>.</li>
5959
</ul>
6060

61-
6261
## Solutions
6362

6463
<!-- tabs:start -->
6564

6665
### **Python3**
6766

6867
```python
69-
68+
class Solution:
69+
def findBall(self, grid: List[List[int]]) -> List[int]:
70+
m, n = len(grid), len(grid[0])
71+
72+
def dfs(i, j):
73+
nonlocal m, n
74+
if i == m:
75+
return j
76+
if j == 0 and grid[i][j] == -1:
77+
return -1
78+
if j == n - 1 and grid[i][j] == 1:
79+
return -1
80+
if grid[i][j] == 1 and grid[i][j + 1] == -1:
81+
return -1
82+
if grid[i][j] == -1 and grid[i][j - 1] == 1:
83+
return -1
84+
return dfs(i + 1, j + 1) if grid[i][j] == 1 else dfs(i + 1, j - 1)
85+
86+
return [dfs(0, j) for j in range(n)]
7087
```
7188

7289
### **Java**
7390

7491
```java
92+
class Solution {
93+
private int m;
94+
private int n;
95+
private int[][] grid;
96+
97+
public int[] findBall(int[][] grid) {
98+
m = grid.length;
99+
n = grid[0].length;
100+
this.grid = grid;
101+
int[] ans = new int[n];
102+
for (int j = 0; j < n; ++j) {
103+
ans[j] = dfs(0, j);
104+
}
105+
return ans;
106+
}
107+
108+
private int dfs(int i, int j) {
109+
if (i == m) {
110+
return j;
111+
}
112+
if (j == 0 && grid[i][j] == -1) {
113+
return -1;
114+
}
115+
if (j == n - 1 && grid[i][j] == 1) {
116+
return -1;
117+
}
118+
if (grid[i][j] == 1 && grid[i][j + 1] == -1) {
119+
return -1;
120+
}
121+
if (grid[i][j] == -1 && grid[i][j - 1] == 1) {
122+
return -1;
123+
}
124+
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
int m, n;
135+
vector<vector<int>> grid;
136+
137+
vector<int> findBall(vector<vector<int>>& grid) {
138+
this->grid = grid;
139+
m = grid.size();
140+
n = grid[0].size();
141+
vector<int> ans(n);
142+
for (int j = 0; j < n; ++j) ans[j] = dfs(0, j);
143+
return ans;
144+
}
145+
146+
int dfs(int i, int j) {
147+
if (i == m) return j;
148+
if (j == 0 && grid[i][j] == -1) return -1;
149+
if (j == n - 1 && grid[i][j] == 1) return -1;
150+
if (grid[i][j] == 1 && grid[i][j + 1] == -1) return -1;
151+
if (grid[i][j] == -1 && grid[i][j - 1] == 1) return -1;
152+
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
153+
}
154+
};
155+
```
75156
157+
### **Go**
158+
159+
```go
160+
func findBall(grid [][]int) []int {
161+
m, n := len(grid), len(grid[0])
162+
163+
var dfs func(i, j int) int
164+
dfs = func(i, j int) int {
165+
if i == m {
166+
return j
167+
}
168+
if j == 0 && grid[i][j] == -1 {
169+
return -1
170+
}
171+
if j == n-1 && grid[i][j] == 1 {
172+
return -1
173+
}
174+
if grid[i][j] == 1 && grid[i][j+1] == -1 {
175+
return -1
176+
}
177+
if grid[i][j] == -1 && grid[i][j-1] == 1 {
178+
return -1
179+
}
180+
if grid[i][j] == 1 {
181+
return dfs(i+1, j+1)
182+
}
183+
return dfs(i+1, j-1)
184+
}
185+
186+
var ans []int
187+
for j := 0; j < n; j++ {
188+
ans = append(ans, dfs(0, j))
189+
}
190+
return ans
191+
}
76192
```
77193

78194
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int m, n;
4+
vector<vector<int>> grid;
5+
6+
vector<int> findBall(vector<vector<int>>& grid) {
7+
this->grid = grid;
8+
m = grid.size();
9+
n = grid[0].size();
10+
vector<int> ans(n);
11+
for (int j = 0; j < n; ++j) ans[j] = dfs(0, j);
12+
return ans;
13+
}
14+
15+
int dfs(int i, int j) {
16+
if (i == m) return j;
17+
if (j == 0 && grid[i][j] == -1) return -1;
18+
if (j == n - 1 && grid[i][j] == 1) return -1;
19+
if (grid[i][j] == 1 && grid[i][j + 1] == -1) return -1;
20+
if (grid[i][j] == -1 && grid[i][j - 1] == 1) return -1;
21+
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
22+
}
23+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func findBall(grid [][]int) []int {
2+
m, n := len(grid), len(grid[0])
3+
4+
var dfs func(i, j int) int
5+
dfs = func(i, j int) int {
6+
if i == m {
7+
return j
8+
}
9+
if j == 0 && grid[i][j] == -1 {
10+
return -1
11+
}
12+
if j == n-1 && grid[i][j] == 1 {
13+
return -1
14+
}
15+
if grid[i][j] == 1 && grid[i][j+1] == -1 {
16+
return -1
17+
}
18+
if grid[i][j] == -1 && grid[i][j-1] == 1 {
19+
return -1
20+
}
21+
if grid[i][j] == 1 {
22+
return dfs(i+1, j+1)
23+
}
24+
return dfs(i+1, j-1)
25+
}
26+
27+
var ans []int
28+
for j := 0; j < n; j++ {
29+
ans = append(ans, dfs(0, j))
30+
}
31+
return ans
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
private int m;
3+
private int n;
4+
private int[][] grid;
5+
6+
public int[] findBall(int[][] grid) {
7+
m = grid.length;
8+
n = grid[0].length;
9+
this.grid = grid;
10+
int[] ans = new int[n];
11+
for (int j = 0; j < n; ++j) {
12+
ans[j] = dfs(0, j);
13+
}
14+
return ans;
15+
}
16+
17+
private int dfs(int i, int j) {
18+
if (i == m) {
19+
return j;
20+
}
21+
if (j == 0 && grid[i][j] == -1) {
22+
return -1;
23+
}
24+
if (j == n - 1 && grid[i][j] == 1) {
25+
return -1;
26+
}
27+
if (grid[i][j] == 1 && grid[i][j + 1] == -1) {
28+
return -1;
29+
}
30+
if (grid[i][j] == -1 && grid[i][j - 1] == 1) {
31+
return -1;
32+
}
33+
return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1);
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /