#K36602. Path Through an Asteroid Field
Path Through an Asteroid Field
Path Through an Asteroid Field
You are given a grid represented as an n x m matrix, where each cell is either safe or contains an asteroid. A cell is safe when its value is 0
and contains an asteroid when its value is 1
. The top-left cell is at position (1,1) and the bottom-right cell is at (n,m). Your task is to determine if there exists a path from the top-left cell to the bottom-right cell, moving only right or down, that does not pass through any asteroids.
Formally, given the matrix \(G\) where \(G_{i,j}\) is defined as above, determine if there exists a sequence of moves beginning at \(G_{1,1}\) and ending at \(G_{n,m}\) such that each move is either to the right or down, and every visited cell \(G_{i,j}\) satisfies \(G_{i,j} = 0\).
If such a path exists, output YES
; otherwise, output NO
.
inputFormat
The first line contains two integers n and m (1 ≤ n, m ≤ 1000), representing the number of rows and columns of the grid, respectively.
Each of the next n lines contains m space-separated integers (each either 0
or 1
), representing the grid. A value of 0
means the cell is safe, and 1
means the cell contains an asteroid.
outputFormat
Output a single line with the string YES
if there exists a valid path from the top-left corner to the bottom-right corner, or NO
otherwise.
3 3
0 1 0
0 0 0
1 0 0
YES