#C9422. Path Existence in a Grid

    ID: 53514 Type: Default 1000ms 256MiB

Path Existence in a Grid

Path Existence in a Grid

You are given a 2D grid represented as an \(m \times n\) matrix where each cell contains either a 0 or a 1. A cell with a 0 represents an empty space, while a cell with a 1 represents an obstacle. Your task is to determine if there exists a path from the top-left cell (i.e. \( (0,0) \)) to the bottom-right cell (i.e. \( (m-1,n-1) \)). You can only move in four directions: up, down, left, and right. Note that you cannot step into a cell with a value of 1.

Input Format: The first line of input contains two integers \(m\) and \(n\) representing the number of rows and columns in the grid. The following \(m\) lines each contain \(n\) space-separated integers (each either 0 or 1) representing the grid.

Output Format: Print YES if there exists a valid path from the top-left to the bottom-right cell; otherwise, print NO.

Example:

Input:
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 1 0
0 1 1 1 0
0 0 0 0 0

Output:
YES

inputFormat

The input begins with two integers \(m\) and \(n\) on a single line, separated by space, indicating the number of rows and columns respectively. The next \(m\) lines each contain \(n\) integers separated by spaces representing the grid. Each integer is either 0 (empty) or 1 (obstacle).

outputFormat

Print a single line with the string YES if a path exists from the top-left to the bottom-right of the grid, otherwise print NO.

## sample
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 1 0
0 1 1 1 0
0 0 0 0 0
YES