Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Ninja Ant
MEDIUM
30 mins
1 upvotes
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Ninja Ant

Contributed by
Ankush Gupta
Medium
yellow-spark
0/80
Avg time to solve 30 mins
Success Rate 70 %
Share
1 upvotes

Problem Statement

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.
Detailed explanation ( Input/output format, Notes, Constraints, Images )

Sample Input 1 :

2
2 0 0 1 
1 1
0 0
2 0 0 2
1 1
0 1

Sample Output 1 :

1 0
1 1

Explanation of the Sample Input 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   

Sample Input 2 :

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

Sample Output 2 :

2 2
-1 -1
Reset Code
Full screen
Auto
copy-code
Console