You are given a matrix 'MAT' with ‘N’ rows and ‘N’ columns, and an ant is initially sitting on a coordinate given to you. The ant moves in an interesting way. If the ith row and jth column value are 1, it rotates 90 degrees in the right direction and moves 1 step forward. If the value at the ith row and jth column are 0, it rotates 90 degrees in the left direction and moves 1 step forward.
Your task is to find the final coordinates of the ant.
Note:
The ant initially faces toward the east side. Every time an ant moves from a block, it inverts it, i.e., changes 0 to 1 and 1 to 0.
If the ant exits the matrix just return -1,-1.
For example,
If ‘N’ = 2
mat[2][2] = {{1, 1},
{0, 0}}
startingRow = 0 , startingColumn = 0
moves = 1
The ant is initially facing the east side, it will take a right turn and move 1 stop in the south.
The output will be 1 0.
Input format :
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case contains four space-separated integers N, sr, and sc and moves. Where ‘N’ is the number of rows and columns in the ‘MAT’ matrix, ‘sr’ is the starting row and ‘sc’ is the starting column of the ant in the matrix, and moves are the number of moves an ant will make on the grid.
The description of the next ‘N’ lines is as follows-
Each line contains ‘N’ space-separated integers representing the elements of the matrix ‘MAT’.
Output format :
For each test case, print a single line containing two space-separated integers denoting the final position of an ant[0-based indexing].
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 100
0 <= MAT[ i ][ j ] <= 1
Where ‘T’ is the total number of test cases, and 'N’ is the number of rows and columns in the ‘MAT’ matrix, and MAT[ i ][ j ] is the value of pairs (i, j).
Time limit: 1sec.
2
2 0 0 1
1 1
0 0
2 0 0 2
1 1
0 1
1 0
1 1
For the first test case:
The ant is initially facing the east side, will take a right turn, and move 1 stop in the south. Therefore output will be 1 0.
For the second test case:
The ant first takes a right turn and lands at (1,0). The ant takes a right turn and moves forward, coming to 1,1. Therefore the output is 1 1
2
3 1 1 2
1 0 1
0 1 0
1 0 1
3 1 1 3
1 0 1
0 1 0
1 0 1
2 2
-1 -1
Can we use recursion?
The idea is to use recursion and maintain a variable that tells us what direction the ant is facing. We have four different direction options up, down, left, right.
At each call, the direction we move forward will be determined by the value of the block. If the block is 0, then we take a left turn, else a right turn. Till the number of moves, we have to make after that return the final position of the ant (row, col)[0 based indexing]. If the ant ever leaves the board just return -1,-1
O(M), where ‘M’ is the number of moves.
Since we are moving only ‘M’ times hence time complexity is O(M).
O(M), where ‘M’ is the number of moves.
Since we are using recursion, hence ‘M’ call stacks will be made.