#C10083. Grid Path Finder
Grid Path Finder
Grid Path Finder
You are given a grid of size \(n \times m\) composed of integers. Each cell in the grid is either open (represented by 0) or blocked (represented by 1). Your task is to determine if there exists a path from the top-left corner (cell (1,1)) to the bottom-right corner (cell (n,m)) moving only in the right or downward directions.
Note: A valid path cannot pass through blocked cells. Formally, if we denote the grid as \(G\), you need to check if there exists a sequence of cells \((i_1,j_1), (i_2,j_2), \dots, (i_k,j_k)\) such that:
- \((i_1,j_1) = (1,1)\) and \((i_k,j_k) = (n,m)\);
- For every \(1 \leq t \leq k\), \(G(i_t,j_t) = 0\);
- For every \(1 \leq t < k\), either \(i_{t+1} = i_t + 1 \text{ and } j_{t+1} = j_t\) or \(i_{t+1} = i_t \text{ and } j_{t+1} = j_t + 1\).
If such a path exists, output YES
; otherwise, output NO
.
inputFormat
The input is read from standard input. The first line contains two space-separated integers \(n\) and \(m\) representing the number of rows and columns, respectively. The next \(n\) lines each contain \(m\) space-separated integers (either 0 or 1), representing the grid.
For example:
3 3 0 0 1 1 0 1 1 0 0
outputFormat
Print a single line to standard output: YES
if there is a valid path from the top-left corner to the bottom-right corner, otherwise print NO
.
3 3
0 0 1
1 0 1
1 0 0
YES