# Solution For Where Will The Ball Fall

Problem Statement:

Given a two-dimensional matrix representing the height of each cell, and a ball that is dropped at any cell of the matrix, the ball moves in the direction of the steepest descent until it hits a V-shaped barrier between two cells, and then bounces off the barrier in the opposite direction of the incoming direction. The gravity effect makes the ball move down the matrix cells towards the V-shaped barrier. The goal is to find the cell where the ball will stop.

Solution:

To solve this problem, we need to simulate the movement of the ball down the matrix cells. The key to simulating the ball movement is to find the direction of the steepest descent from the current cell and move the ball in that direction. We also need to check if the ball hits a V-shaped barrier, and if it hits, we need to bounce it off in the opposite direction.

We can simulate the ball movement using a while loop. We start with the cell where the ball is dropped, and we keep moving the ball in the direction of the steepest descent until it hits a V-shaped barrier.

To find the direction of the steepest descent, we need to check the neighboring cells of the current cell and find the cell with the minimum height. We can use the following code to find the minimum height cell:

`int[] dir = {-1, 0, 1, 0, -1}; int min_row = row, min_col = col, min_height = heights[row][col]; for (int k = 0; k < 4; k++) { int new_row = row + dir[k], new_col = col + dir[k+1]; if (new_row < 0 || new_row >= m || new_col < 0 || new_col >= n) continue; if (heights[new_row][new_col] < min_height) { min_row = new_row; min_col = new_col; min_height = heights[new_row][new_col]; } }`
The `dir` array represents the four neighboring cells of the current cell. We iterate over the neighbors and check if the height of the neighbor is less than the current minimum height. If it is, we update the minimum height cell and continue the loop.

Once we find the minimum height cell, we need to check if the ball will hit a V-shaped barrier if we move it to the minimum height cell. A V-shaped barrier is a situation where the two cells adjacent to the ball are higher than the ball’s current position. We can use the following code to check if the ball will hit a V-shaped barrier:

`if (heights[min_row][min_col] >= heights[row][col] && ((min_row == row - 1 && heights[min_row][min_col] >= heights[row][col-1]) || (min_row == row + 1 && heights[min_row][min_col] >= heights[row][col+1]) || (min_col == col - 1 && heights[min_row][min_col] >= heights[row-1][col]) || (min_col == col + 1 && heights[min_row][min_col] >= heights[row+1][col]))) { // hit a V-shaped barrier } else { row = min_row; col = min_col; }`
First, we check if the minimum height cell is higher than or equal to the current cell. If it is, we need to check if it is a V-shaped barrier. We check if the minimum height cell is adjacent to the current cell in one of the four directions. If it is, we need to check if the two cells adjacent to the ball are higher than the current position. If they are, we have hit a V-shaped barrier.

If we hit a V-shaped barrier, we need to bounce the ball off in the opposite direction. We can use the following code to do this:

`if (min_row == row - 1 && heights[min_row][min_col] >= heights[row][col-1]) { row = row - 1; col = col - 1; } else if (min_row == row + 1 && heights[min_row][min_col] >= heights[row][col+1]) { row = row + 1; col = col + 1; } else if (min_col == col - 1 && heights[min_row][min_col] >= heights[row-1][col]) { row = row - 1; col = col + 1; } else if (min_col == col + 1 && heights[min_row][min_col] >= heights[row+1][col]) { row = row + 1; col = col - 1; }`
We check which direction the ball was moving before hitting the V-shaped barrier, and we move it in the opposite direction.

We continue this process of finding the steepest descent direction, checking for V-shaped barriers, and bouncing off the ball until we find the cell where the ball will stop.

The complete solution for this problem can be found below:

`class Solution { public int[] findBall(int[][] grid) { int m = grid.length, n = grid.length; int[] ans = new int[n]; for (int i = 0; i < n; i++) { int row = 0, col = i; while (row < m) { int[] dir = {-1, 0, 1, 0, -1}; int min_row = row, min_col = col, min_height = grid[row][col]; for (int k = 0; k < 4; k++) { int new_row = row + dir[k], new_col = col + dir[k+1]; if (new_row < 0 || new_row >= m || new_col < 0 || new_col >= n) continue; if (grid[new_row][new_col] < min_height) { min_row = new_row; min_col = new_col; min_height = grid[new_row][new_col]; } } if (grid[min_row][min_col] >= grid[row][col] && ((min_row == row - 1 && grid[min_row][min_col] >= grid[row][col-1]) || (min_row == row + 1 && grid[min_row][min_col] >= grid[row][col+1]) || (min_col == col - 1 && grid[min_row][min_col] >= grid[row-1][col]) || (min_col == col + 1 && grid[min_row][min_col] >= grid[row+1][col]))) { // hit a V-shaped barrier ans[i] = -1; break; } else { row = min_row; col = min_col; } if (row == m) { ans[i] = col; } } } return ans; } }`
In this solution, we iterate over each column of the matrix and simulate the ball movement starting from each cell in the first row. If we find a cell where the ball stops, we store the column index in the `ans` array, and if the ball hits a V-shaped barrier, we store `-1` in the `ans` array.

## Step by Step Implementation For Where Will The Ball Fall

```Assuming we have a 2D array representing the board, we can keep track of the row and column of the ball's position. Every time the ball moves, we can update the row and column variables accordingly. If the ball reaches the edge of the board, we can simply print out the row and column variables.

row = 0;
column = 0;

while (true) {
// check if ball is at edge of board
if (row == 0 || row == board.length-1 || column == 0 || column == board.length-1) {
System.out.println("Row: " + row);
System.out.println("Column: " + column);
break;
}

// update row and column variables
if (board[row][column] == 'U') {
row--;
}
else if (board[row][column] == 'D') {
row++;
}
else if (board[row][column] == 'L') {
column--;
}
else if (board[row][column] == 'R') {
column++;
}
}```
```def ball_fall(l, h, p):

# l is the length of the tube
# h is the height of the tube
# p is the position of the hole

# if the ball falls to the left of the hole
if p-l/2 < 0:
return "The ball will fall to the left of the hole."

# if the ball falls to the right of the hole
elif p-l/2 > 0:
return "The ball will fall to the right of the hole."

# if the ball falls directly into the hole
else:
return "The ball will fall directly into the hole."```
```var findBallFallPath = function(board) {
let rows = board.length;
let cols = board.length;

let visited = new Array(rows);
for(let i = 0; i < rows; i++) {
visited[i] = new Array(cols).fill(false);
}

let queue = [];
queue.push([0, 0]);
visited = true;

let dirs = [[1, 0], [0, 1], [-1, 0], [0, -1]];

while(queue.length > 0) {
let curr = queue.shift();
let r = curr;
let c = curr;

if(r === rows-1 && c === cols-1) {
// we have reached the end
return true;
}

for(let i = 0; i < 4; i++) {
let newR = r + dirs[i];
let newC = c + dirs[i];

if(newR >= 0 && newR < rows && newC >= 0 && newC < cols && !visited[newR][newC]) {
// the new position is valid
queue.push([newR, newC]);
visited[newR][newC] = true;
}
}
}

return false;
};```
`The ball will fall off the edge of the table.`
```using System;

class GFG {

// Function to calculate the
// position of ball
static int position(int v0, int theta,
int x1, int h1, int h2)
{

// convert the angle into radian
double radian = (Math.PI * theta) / 180;

// y coordinate of ball
double y = (x1 * Math.Tan(radian)) -
((9.81 * x1 * x1) /
(2 * v0 * v0 * Math.Cos(radian) *

// if y coordinate is less than height
// of platform then ball will fall
// on the platform
if (y < h1 + 1)
return 1;

// if y coordinate is greater than height
// of platform but less than height of hole
// then ball will pass through the platform
else if (y < h2)
return 0;

// if y coordinate is greater than the
// height of hole then ball will not
// reach the hole
else
return -1;
}

// Driver code
public static void Main()
{

// initial velocity of ball
int v0 = 20;

// with respect to ground
int theta = 60;

// coordinate of platform
int x1 = 120;

// height of platform
int h1 = 40;

// height of hole
int h2 = 120;

// calling function
int result = position(v0, theta, x1, h1, h2);

// if result is 1 then ball will fall
// on the platform
if (result == 1)
Console.Write("Ball will fall on"
+ " the platform");

// if result is 0 then ball will pass
// through the platform
else if (result == 0)
Console.Write("Ball will pass"
+ " through the platform");

// if result is -1 then ball will not
// reach the hole
else
Console.Write("Ball will not"
+ " reach the hole");
}
}```

## Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]