Swift simulation + dynamic programming
\n\nclass Solution {\n    func findBall(_ grid: [[Int]]) -> [Int] {\n        guard !grid.isEmpty else { return [] }\n        let columns = grid[0].count\n      
```\n\nclass Solution {\n    func findBall(_ grid: [[Int]]) -> [Int] {\n        guard !grid.isEmpty else { return [] }\n        let columns = grid[0].count\n        var result: [Int] = .init(repeating: -1, count: columns)\n        \n        ballsLoop: for startColumn in stride(from: 0, to: columns, by: 1) {\n            var column = startColumn\n            for row in stride(from: 0, to: grid.count, by: 1) {\n                guard let element = grid[row][safe: column],\n                let adjacentElement = grid[row][safe: column + element] else {\n                    continue ballsLoop \n                }\n                if element + adjacentElement == 0 {\n                    continue ballsLoop\n                }\n                column += element\n            }\n            result[startColumn] = column\n        }\n        return result\n    }\n}\n\nextension Collection {\n    subscript(safe index: Index) -> Element? {\n        indices.contains(index) ? self[index] : nil\n    }\n}\n```\n\nDP approach:\n```\n\nclass Solution {\n    func findBall(_ grid: [[Int]]) -> [Int] {\n        guard !grid.isEmpty else { return [] }\n        let columns = grid[0].count\n        var result: [Int] = []\n        var dp: [[Int]] = .init(repeating: .init(repeating: -1, count: columns), count: grid.count + 1)\n        \n        for row in stride(from: dp.count - 1, to: -1, by: -1) {\n            for column in stride(from: 0, to: columns, by: 1) {\n                if row == dp.count - 1 {\n                    dp[row][column] = column\n                    continue\n                }\n                \n                let nextColumn = column + grid[row][column]\n                if let value = dp[row + 1][safe: nextColumn], grid[row][safe: nextColumn] == grid[row][column] {\n                    dp[row][column] = value\n                }\n                if row == 0 {\n                    result.append(dp[row][column])\n                }\n            }\n        }\n        return result\n    }\n}\n\nextension Collection {\n    subscript(safe index: Index) -> Element? {\n        indices.contains(index) ? self[index] : nil\n    }\n}\n```
['Swift']