---
# System prepended metadata

title: 1706. Where Will the Ball Fall
tags: [Leetcode, Simulation, Medium]

---

# 1706. Where Will the Ball Fall
###### tags: `Leetcode` `Medium` `Simulation`
Link: https://leetcode.com/problems/where-will-the-ball-fall/description/
## 思路
if the ball wants to move from column ```i1``` to column ```i2```,
we must have the board direction ```grid[j][i1]==grid[j][i2]```
## Code
```python=
class Solution:
    def findBall(self, grid: List[List[int]]) -> List[int]:
        m, n = len(grid), len(grid[0])
        ans = []
        for i in range(n):
            i1 = i
            for j in range(m):
                i2 = i1 + grid[j][i1]
                if i2==-1 or i2==n or grid[j][i2]!=grid[j][i1]:
                    i1 = -1
                    break
                i1 = i2
            ans.append(i1)
        return ans
```