#K93167. Valid Path in a Grid
Valid Path in a Grid
Valid Path in a Grid
You are given an n x m grid where each cell contains either a 0 or a 1. A cell with 0 represents an open space, and a cell with 1 represents an obstacle. Your task is to determine whether there exists a valid path from the top-left corner (0, 0) to the bottom-right corner (n-1, m-1). You can move up, down, left, or right from a cell as long as you remain within the grid and do not step onto an obstacle.
The movement can be understood mathematically: a move from cell (i, j) to cell (i + dx, j + dy) is valid if
[ 0 \leq i + dx < n, \quad 0 \leq j + dy < m, \quad \text{and} \quad grid[i+dx][j+dy] = 0. ]
Determine if such a path exists. For example, consider the grid below:
3 3 0 0 1 0 1 0 0 0 0
The answer is True
because there is a path from the top-left corner to the bottom-right corner.
inputFormat
The input is given via stdin in the following format:
n m row1 row2 ... row n
Here, n
and m
are the number of rows and columns respectively. Each of the next n
lines contains m
space-separated integers (either 0 or 1) representing a row of the grid.
outputFormat
Output a single line via stdout containing either True
or False
. True
indicates that there exists a valid path from the top-left corner to the bottom-right corner, and False
otherwise.
3 3
0 0 1
0 1 0
0 0 0
True