Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find where the ball lands in a grid box in Python
Suppose we are given an 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. A ball is dropped from each top cell, and we need to determine where each ball lands at the bottom of the box.
The grid is represented as a matrix where 1 means the diagonal board spans from top-left to bottom-right, and -1 means it spans from top-right to bottom-left. If a ball gets stuck or goes out of bounds, we return -1 for that position.
Problem Analysis
For each column, we simulate dropping a ball and track its path:
If current cell is
1(top-left to bottom-right), ball moves rightIf current cell is
-1(top-right to bottom-left), ball moves leftBall gets stuck if it hits a wall or incompatible adjacent board
Algorithm Steps
For each starting column, simulate the ball's path
At each row, check if the ball can continue to the next cell
If ball goes out of bounds or hits incompatible boards, return
-1If ball reaches the bottom, return its final column position
Implementation
def solve(mat):
rows, cols = len(mat), len(mat[0])
result = []
# Try dropping a ball from each column
for start_col in range(cols):
current_col = start_col
# Simulate ball falling through each row
for row in range(rows):
direction = mat[row][current_col]
next_col = current_col + direction
# Check if ball goes out of bounds
if next_col < 0 or next_col >= cols:
result.append(-1)
break
# Check if adjacent cell has compatible board
if mat[row][next_col] != direction:
result.append(-1)
break
current_col = next_col
else:
# Ball successfully reached the bottom
result.append(current_col)
return result
# Test with the given example
mat = [[1, 1, 1, -1],
[-1, 1, 1, -1],
[1, -1, -1, 1],
[1, -1, 1, -1]]
print(solve(mat))
[-1, -1, -1, -1]
How It Works
Let's trace the first ball (column 0):
Row 0: Cell [0,0] = 1, ball moves right to column 1
Row 1: Cell [1,1] = 1, ball moves right to column 2
Row 2: Cell [2,2] = -1, ball tries to move left to column 1
Check: Cell [2,1] = -1, compatible with direction, ball continues
Row 3: Cell [3,1] = -1, ball tries to move left to column 0
Check: Cell [3,0] = 1, incompatible! Ball gets stuck, return -1
Key Points
A ball moves in the direction indicated by the current cell's value
Both the current cell and next cell must have the same value for the ball to continue
If a ball goes out of bounds or hits incompatible boards, it returns -1
The algorithm simulates each ball's complete path from top to bottom
Conclusion
This solution simulates ball movement through a grid by checking board compatibility at each step. The algorithm efficiently determines where each ball lands or if it gets stuck during the fall.
