- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find where the ball lands in a grid box in Python

Suppose, we are given a m x n grid box, where each cell has a board that is positioned either from the top-right to bottom-left, or from the top-left to the bottom-right. Now from the top cells, a ball is put into the box and we have to check if that ball reaches the bottom of the box. The grid is given as a matrix. If a cell is marked 1 the diagonal board spans from the top-left to the bottom-right; if it is marked -1 it spans from the top-right to the bottom-left corner. If n balls are put into the box, we have to find out how many balls reach the bottom.

Example of a 3x3 grid box.

So, if the input is like mat =

1 | 1 | 1 | -1 |

-1 | 1 | 1 | -1 |

1 | -1 | -1 | 1 |

1 | -1 | 1 | -1 |

then the output will be [-1, -1, -1, -1]

To solve this, we will follow these steps −

i := number of rows in mat

j := number of columns in mat

res := a new list

for val in range 0 to j, do

x := val

for r in range 0 to i, do

s := mat[r, x]

x := x + mat[r, x]

if x < 0 or x >= j or mat[r, x] is not same as s, then

add element -1 at the end of res

come out from the loop

otherwise,

add x at the end of res

return res

## Example

Let us see the following implementation to get better understanding

def solve(mat): i, j = map(len, (mat, mat[0])) res = [] for val in range(j): x = val for r in range(i): s = mat[r][x] x += mat[r][x] if x < 0 or x >= j or mat[r][x] != s: res += [-1] break else: res += [x] return res print(solve([[1, 1, 1, -1], [-1, 1, 1, -1], [1, -1, -1, 1],[1, -1, 1, -1] ]))

## Input

[[1, 1, 1, -1], [-1, 1, 1, -1], [1, -1, -1, 1],[1, -1, 1, -1] ]

## Output

[-1, -1, -1, -1]

- Related Articles
- Find the position of box which occupies the given ball in Python
- Program to Find Out the Number of Squares in a Grid in Python
- Program to find out the position of a ball after n reversals in Python
- Program to find maximum number of balls in a box using Python
- Program to find minimum swaps to arrange a binary grid using Python
- Program to detect cycles in 2D grid in Python
- C++ Program to find out the number of border cells in a grid
- C++ Program to find out the number of illuminated cells in a grid
- Program to validate a sudoku grid is solvable or not in Python
- Set where to end the CSS grid item
- C++ Program to find out if there is a pattern in a grid
- Program to find a sublist where first and last values are same in Python
- Program to find island count by adding blocks into grid one by one in Python
- Program to find buildings from where sea can be visible in Python
- Program to find minimum number of operations to move all balls to each box in Python