# Pass the ball game: NxN matrix in python

Carvia Tech | May 18, 2019 | 3 min read | 836 views |

Pass the Ball Game

One of the game in Raj’s summer camp is passing the ball. All the participants in the game are seated on ground which are arranged in R rows and C columns. Raj is sitting at position X, Y, where X is the row number and Y is the column. The game starts when the coach gives the ball to a participant sitting in any of the corner (1,1) or (1,C) or (R,1) or (R,C) . Participant should pass the ball to the next participant sitting to left, right, front or back.

Participant should pass the ball with below priority:

1. If there is a participant in the Right and was not passed the ball earlier, pass it to right.

2. If there is a participant in the Left and was not passed the ball earlier, pass it to left.

3. If there is a participant in the Front and was not passed the ball then pass it front.

4. If there is a participant in the Back and was not passed the ball earlier then pass it to back.

5. Participant should say "Over", if all the four (left, right, front, back) have passed the ball.

Raj is very curious and does not want to wait till the ball comes to him to find out which direction he has to pass the ball.

Write a program to find out where Raj has to pass the ball or should he say "Over"

## Input

First line consists of a single integer, T, denoting the number of test cases.

Each test case consists of three lines.

First line is two integers denoting R and C.

Second line is two integers SX and SY, denoting the starting corner of distribution

Third line is two integers X and Y deoniting where Raj sits.

## Output

For each case, print the output as the direction in which he needs to pass the ball. It could be (Left, Right, Front, Back or "All Done")

Sample Input 1
``````1
3 3
1 1
3 3``````

Sample Output 1

Over

Sample Input 2
``````2
2 3
1 3
2 2
3 3
1 3
2 3``````

## Solution

The below program in python will calculate the next move based on logic given above.

``````import numpy as np

def move_next(empty_matrix, current_position, size):
if (current_position + 1 < size) and (empty_matrix[current_position, current_position + 1] == 0): ## Move Right
current_position = current_position + 1
empty_matrix[current_position, current_position] = 1
return "Move Right"
if (current_position - 1 >= 0) and (empty_matrix[current_position, current_position - 1] == 0): ## Move Left
current_position = current_position - 1
empty_matrix[current_position, current_position] = 1
return "Move Left"
if (current_position + 1 < size) and (empty_matrix[current_position + 1, current_position] == 0):  ## Move front
current_position = current_position + 1
empty_matrix[current_position, current_position] = 1
return "Move Front"
if (current_position - 1 > 0) and (empty_matrix[current_position - 1, current_position] == 0):    ## Move Back
current_position = current_position - 1
empty_matrix[current_position, current_position] = 1
return "Move Back"
return "Over"

if __name__ == "__main__":
# t = int(input())
for _ in range(int(input())):
size = [int(i) for i in input().split()]
starting_corner = [int(i) for i in input().split()]
raj_post = [int(i) for i in input().split()]
empty_matrix = np.zeros(tuple(size))
print(_, size, starting_corner, raj_post)
# print(empty_matrix)
raj_post = [raj_post-1, raj_post-1]
current_position = [starting_corner-1, starting_corner-1]
empty_matrix[current_position, current_position] = 1
print(empty_matrix)

move = move_next(empty_matrix, current_position, size)
lastMove = False
while move != "Over":
print(empty_matrix)
if current_position == raj_post:
lastMove = True
move = move_next(empty_matrix, current_position, size)
if lastMove:
print(move)``````

###### Python Coding Problems:
See all articles in Python Coding Problems

###### Find more on this topic: ##### Machine Learning

Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more

Last updated 1 week ago

###### Recommended books for interview preparation:
Book you may be interested in..
Book you may be interested in..
Book you may be interested in..