Skip to content
Onsite

Maze Validator

MediumP3Coding

A maze is a rectangular grid where 0 is an open cell and 1 is a wall. An opening is an open cell on the outer edge of the grid (first or last row, first or last column); a corner cell is one cell.

The maze is valid if and only if both hold:

  • There are exactly two openings: one entry and one exit (it does not matter which is which).
  • There is a path of open cells between the two openings, moving up, down, left or right.

Return true if the maze is valid, otherwise false.

Example 1

Input
grid = [[1,0,1],[1,0,1],[1,0,1]]
Output
true

Example 2

Input
grid = [[1,0,1],[1,1,1],[1,0,1]]
Output
false

Example 3

Input
grid = [[0,0,0],[1,1,1],[1,1,1]]
Output
false

Constraints

  • 1 <= rows, cols <= 100
  • grid[i][j] is 0 or 1.
  • All rows have the same length.

Hints

Run checks the 3 visible tests, Submit all 13.