#K38417. Path Finding in a Maze

    ID: 26194 Type: Default 1000ms 256MiB

Path Finding in a Maze

Path Finding in a Maze

You are given a square maze represented as an n x n grid where each cell is either open (represented by 0) or blocked (represented by 1). Your task is to determine whether there exists a path from the top-left corner (cell (1,1)) to the bottom-right corner (cell (n,n)). You can move to adjacent cells in four directions: up, down, left, and right.

The challenge is to implement a function that reads multiple test cases from standard input. For each test case, the first line contains the size of the grid n, followed by n lines with n space-separated integers. For each maze, print YES if a path exists or NO otherwise.

Note: There is always at least one test case. The grid is always square. Use depth-first search (DFS) or any other graph traversal algorithm to solve the problem.

inputFormat

The input is given via standard input (stdin) and has the following format:

T
n
row1
row2
...
rown
n
row1
row2
...
rown
...

Where T is the number of test cases. For each test case, the first integer n represents the size of the grid. The next n lines each contain n space-separated integers (either 0 or 1) representing the maze cells.

outputFormat

For each test case, output a single line containing either YES if there exists a path from the top-left to the bottom-right corner, or NO if no such path exists. The output should be written to standard output (stdout).

## sample
2
3
0 1 0
0 0 1
1 0 0
2
0 1
1 0
YES

NO

</p>