#K75037. Find a Valid Path in a Grid

    ID: 34330 Type: Default 1000ms 256MiB

Find a Valid Path in a Grid

Find a Valid Path in a Grid

You are given a grid of size \(m \times n\) where each cell is either accessible (0) or blocked (1). A robot starts at the top-left cell at (0,0) and needs to reach the bottom-right cell at (m-1, n-1). The robot can move in four directions: up, down, left, or right.

The grid is represented as a 2D matrix. A cell with value \(1\) is an obstacle and cannot be traversed, while a cell with \(0\) is open for movement. Your task is to determine whether there exists a path from the start to the destination that avoids obstacles.

Note: The movement is allowed in four directions. If the starting or ending cell is blocked, the answer is automatically "NO".

inputFormat

The input is given via standard input. The first line contains two space-separated integers \(m\) and \(n\) indicating the number of rows and columns respectively.

Each of the next \(m\) lines contains \(n\) space-separated integers (either 0 or 1) representing the grid.

outputFormat

Print "YES" if there is a path from the top-left corner to the bottom-right corner avoiding obstacles; otherwise, print "NO". The output should be written to standard output.## sample

3 3
0 0 1
1 0 1
1 0 0
YES