1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// @Title: 球会落何处 (Where Will the Ball Fall)
// @Author: 15816537946@163.com
// @Date: 2022-02-24 23:04:12
// @Runtime: 16 ms
// @Memory: 6.5 MB
func findBall(grid [][]int) []int {
    n := len(grid[0])
    ans := make([]int, n)
    for j := range ans {
        col := j // 球的初始列
        for _, row := range grid {
            dir := row[col]
            col += dir // 移动球
            if col < 0 || col == n || row[col] != dir { // 到达侧边或 V 形
                col = -1
                break
            }
        }
        ans[j] = col // col >= 0 为成功到达底部
    }
    return ans
}