Java | Where Will the Ball Fall | Simulation BFS
\nclass Solution {\n    \n    \n    class BallInfo {\n        int col; // from which column.\n        int x,y; // determines currentPosition.\n        \n       
```\nclass Solution {\n    \n    \n    class BallInfo {\n        int col; // from which column.\n        int x,y; // determines currentPosition.\n        \n        public BallInfo(int col,int x,int y) {\n            this.col = col;\n            this.x = x;\n            this.y = y;\n        }\n    }\n    \n    \n    public int[] findBall(int[][] grid) {\n        int n = grid.length;\n        int m = grid[0].length;\n        Queue<BallInfo> q = new LinkedList<>();\n        int ans[] = new int[m];\n        int idx=0;\n        \n        for(int i=0;i<m;i++) {\n            q.offer(new BallInfo(i,0,i));         \n        }\n        \n        while(!q.isEmpty()) {\n            \n            BallInfo cb = q.poll();\n            // reached end of row.\n            if(cb.x>=n) {\n                ans[cb.col] = cb.y;\n                continue;\n            }\n\t\t\tif(cb.x<0 || cb.y>=m || cb.y<0) { // current ball is outOFBounds already.\n                continue;\n            }\n            \n            int nextX = cb.x;\n            int nextY = (grid[cb.x][cb.y]==1?cb.y+1:cb.y-1);\n            \n            // struck condition.\n            if(nextX<0 || nextY>=m || nextY<0 || grid[nextX][nextY]+grid[cb.x][cb.y]==0) {\n                ans[cb.col] = -1;\n                continue;\n            }\n            \n            if(grid[cb.x][cb.y]==1) {\n                q.offer(new BallInfo(cb.col,cb.x+1,cb.y+1));\n            } else {\n                q.offer(new BallInfo(cb.col,cb.x+1,cb.y-1));\n            }\n        }\n        \n        return ans;\n    }\n}\n```\n\n*Please Upvote if you found my solution useful. Thanks :)*\n
['Breadth-First Search', 'Simulation', 'Java']