Maze With N doors and 1 Key

You are given an 'N * N' 'MAZE' where some cells have a door while some do not and a key that can be used only once to open a door.

You need to find if there exists a path from the top-left cell to the bottom right cell of the maze provided only downward and rightward movements are allowed.

Note:

1. You have only one key. And a key once used is exhausted and no more available with you during the journey through that path in a maze.

2. A cell with value 1, means the door or path is closed. And you have to spend a key to open the door/ reach that cell.

3. A cell with value 0, means that the cell is free to move / door is always open.

4. Top left cell in the maze and bottom-right cell in the maze may also have a door.

5. Downwards movement: From cell (i, j) to (i, j+1).

6. Rightwards movement: From cell (i, j) to (i+1, j).
Input Format:
The first line of the input contains an integer 'T' denoting the number of test cases. 
Then 'T' test cases follow.

The first line of each test case contains the side length 'N' of the square binary maze.

Then 'N' lines follow.
Each line contains 'N' space-separated integers 1 or 0 denoting whether the cell has a door or not.
Output Format:
For each test case, print in a separate line “YES” if the bottom right corner is reachable, else print “NO”.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= 'T' <= 100
1 <= 'N' <= 100
'MAZE[i][j]' = {0, 1}

Time Limit: 1 sec
CodingNinjas
author
2y
Recursion
  • This problem can be solved using Recursion.
  • If the current position in the maze (x, y) is set to 0, that means no key is required to open the current door. Check if it is destination return tr...read more
CodingNinjas
author
2y
Dynamic Programming
  • At any cell (x, y), to know if the bottom-right cell is reachable from this cell. We need two information: whether the path from (x+1, y) to the bottom-right cell and the path from ...read more
Help your peers!
Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter