#K68107. Pathfinding on a Grid
Pathfinding on a Grid
Pathfinding on a Grid
You are given an n x m grid where each cell contains either 0
(an open cell) or 1
(a blocked cell). Your task is to determine if there exists a valid path from the top-left corner (cell (1,1)) to the bottom-right corner (cell (n,m)). You can only move in two directions: down or right.
The start and end cells must be open (i.e. contain the value 0
). Formally, given a grid \( G \) of dimension \( n \times m \), you need to decide whether there is a path from \( (0,0) \) to \( (n-1, m-1) \) such that:
[ \begin{aligned} & G[0][0] = 0, \quad G[n-1][m-1] = 0, \ & \text{and for any two consecutive cells } (x,y) \text{ and } (x',y') \text{ in the path}, \ & \quad (x'-x, , y'-y) \in {(1,0), (0,1)}. \end{aligned} ]
Output YES
if such a path exists and NO
otherwise.
inputFormat
The input is given via stdin and has the following format:
n m row1 row2 ... row_n
Where:
n
andm
are two integers representing the number of rows and columns in the grid.- Each of the next
n
lines containsm
integers (either0
or1
) separated by spaces, representing the cells of the grid.
outputFormat
Output a single line to stdout containing either YES
if a valid path exists, or NO
if there is no valid path.
2 2
0 0
0 0
YES