Ninja and the Maze
Ninja went to an amusement park and visited a maze. Now, he is stuck in the maze. He can go in any direction(Up, Down, Left, or Right) from this point, but he cannot change his direction of motion until he comes across a wall in its path. Once he stops, he can choose his new direction.
Now, you are given Ninja’s starting point, the destination he wants to reach, and the maze in the form of a 2D matrix. You need to find out if Ninja can reach the destination from the starting point or not.
The maze is represented by a 2D array. ‘1’ means Ninja came across a wall and he needs to stop. ‘0’ means that it is an empty space and he can keep moving.
The coordinates of starting point and destination are represented by row and column numbers.
For Example
Given maze: {[0, 0, 1], [1, 0, 0], [1, 0, 0]}
Starting point: [2, 2]
Destination: [0, 0]
For the above example maze will look like this:
So, we can see there are 2 ways for Ninja to reach destination(D), from the starting point(SP). They are: [left -> up -> left] and [up -> left -> up -> left].
So, you need to print true.
Input Format:
The first line contains an integer ‘T’ which denotes the number of test cases.
The first line of each test case contains two space-separated integers ‘M’ and ‘N’ denoting the dimensions of the maze.
The next ‘M’ lines contain ‘N’ space-separated integers each denoting the maze.
The next line of each test case contains two space-separated integers ‘startX’ and ‘startY’ denoting the coordinates of the starting point.
The last line of each test case contains two space-separated integers ‘destX’ and ‘destY’ denoting the coordinates of the destination.
Output Format
For each test case, print a single line containing a single string containing ‘True’ if it is possible to reach the destination from the starting point and ‘False’ if it is not possible.
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 <= 50
1 <= M,N <= 100
0 <= MAZE[ i ] [ j ] <= 1
Where MAZE[ i ][ j ] is the value of the maze. The starting point and destination will always lie in the maze and will be in empty spaces.
Time limit: 1 sec.
CodingNinjas
author
2y
Depth First Search
- Ninja can move in at most four directions i.e left, right, up, and down.
- We will take a 2-D array ‘directions’ of size 4 x 2 in which the first column denotes the shift in the horizon...read more
Help your peers!
Add answer anonymously...
Top Bounteous x Accolite Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Top HR questions asked in Bounteous x Accolite Software Developer Intern
>
Bounteous x Accolite Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app