#C3115. Patrol Route Validation

    ID: 46507 Type: Default 1000ms 256MiB

Patrol Route Validation

Patrol Route Validation

You are given a grid of size N × M where each cell represents the elevation of that point. Your task is to determine if there exists a valid path from the top-left cell to the bottom-right cell. You can only move either right or down at each step. A move from one cell to an adjacent cell is considered valid if the absolute elevation difference between the cells is at most \(2\), i.e., \(|grid[i][j] - grid[k][l]| \leq 2\).

For each test case, output "YES" if such a path exists, otherwise output "NO".

inputFormat

The input is read from the standard input and follows this format:

  • The first line contains an integer T, the number of test cases.
  • For each test case:
    • The first line contains two space-separated integers N and M representing the number of rows and columns.
    • This is followed by N lines, each containing M space-separated integers representing the elevation of each cell in the row.

outputFormat

For each test case, output a single line with either "YES" if a valid path exists from the top-left to the bottom-right, or "NO" otherwise. The output is printed to the standard output.

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

</p>