#K61352. Maze Runner: Path Finder
Maze Runner: Path Finder
Maze Runner: Path Finder
You are given one or more mazes. Each maze is represented as a grid with 0
and 1
where 0
indicates an open cell and 1
indicates a wall. Your task is to determine whether there exists a valid path from the top-left corner to the bottom-right corner. You can only move in four directions: up, down, left, and right. Diagonal moves are not allowed.
Important: The starting cell (top-left) and the destination cell (bottom-right) must both be open (0
) for a path to exist. Use an appropriate graph traversal algorithm such as Breadth-First Search (BFS) to solve the problem.
The input and output are handled via standard input and standard output.
inputFormat
The input begins with an integer T denoting the number of test cases. Each test case starts with two integers N and M which represent the number of rows and columns in the maze respectively. This is followed by N lines each containing M space-separated integers (either 0 or 1), representing the maze grid.
Input Format:
T N M row1 row2 ... rowN
outputFormat
For each test case, output a single line containing either YES
if there exists a path from the top-left corner to the bottom-right corner, or NO
if there is no such path.
3
4 4
0 1 0 0
0 1 1 0
0 0 0 1
1 1 0 0
3 3
0 0 1
1 0 1
1 0 0
2 2
0 1
1 0
YES
YES
NO
</p>