#K44897. Grid Path with Height Constraints

    ID: 27633 Type: Default 1000ms 256MiB

Grid Path with Height Constraints

Grid Path with Height Constraints

You are given a grid with \( H \) rows and \( W \) columns. Each cell of the grid contains an integer representing its height. Starting at the top left cell, your task is to determine whether you can reach the bottom right cell by moving only to adjacent cells (up, down, left, and right) under the following constraint:

\( grid[i'][j'] \geq grid[i][j] \) when moving from cell \( (i, j) \) to cell \( (i', j') \).

This problem requires you to check if there is a valid path following the above rule. It combines grid traversal and simple constraints that can be solved using Depth First Search (DFS) or Breadth First Search (BFS).

inputFormat

The first line contains two integers \( H \) and \( W \) separated by a space.

The following \( H \) lines each contain \( W \) space-separated integers representing the height of each cell in the grid.

outputFormat

Output a single line: "YES" if it is possible to reach the bottom right cell from the top left cell under the given constraints, otherwise output "NO".

## sample
3 3
1 2 3
2 3 3
1 3 4
YES