-
Notifications
You must be signed in to change notification settings - Fork 4
/
WhereWillTheBallFall.py
28 lines (21 loc) · 1013 Bytes
/
WhereWillTheBallFall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from typing import List
def findBall(grid: List[List[int]]) -> List[int]:
row_length, col_length = len(grid), len(grid[0])
def dfs(row, col, flag, visited):
if row * col_length + col in visited or col < 0 or col >= col_length:
return -1
if row == row_length:
return col
visited.append(row * col_length + col)
if (flag == "up" or flag == "right") and grid[row][col] == 1:
return dfs(row, col + 1, "left", visited)
if (grid[row][col] == 1 and flag == "left") or (grid[row][col] == -1 and flag == "right"):
return dfs(row + 1, col, "up", visited)
if (flag == "up" or flag == "left") and grid[row][col] == -1:
return dfs(row, col - 1, "right", visited)
res = []
for i in range(col_length):
res.append(dfs(0, i, "up", []))
return res
if __name__ == '__main__':
print(findBall([[1, 1, 1, 1, 1, 1], [-1, -1, -1, -1, -1, -1], [1, 1, 1, 1, 1, 1], [-1, -1, -1, -1, -1, -1]]))