#C3625. Grid Path: Reach the Bottom-Right

    ID: 47073 Type: Default 1000ms 256MiB

Grid Path: Reach the Bottom-Right

Grid Path: Reach the Bottom-Right

You are given an N×N grid where each cell represents the height of a building. Starting at the top-left corner, you need to determine if it is possible to reach the bottom-right corner by moving only either to the right or downward. You can only move to a cell if its height is either equal to the current cell or exactly one level higher. Formally, if you are at cell ( (i, j) ) with height ( h ), you can move to cell ( (i, j+1) ) or ( (i+1, j) ) if the cell's height ( h' ) satisfies: [ h \leq h' \leq h+1 ]

This problem tests your ability to implement a pathfinding algorithm under specific constraints.

inputFormat

The input is given via standard input (stdin). The first line contains a single integer ( T ), denoting the number of test cases. For each test case, the first line contains an integer ( N ) (the size of the grid). The next ( N ) lines each contain ( N ) space-separated integers representing the grid.

outputFormat

For each test case, output a single line (via standard output, stdout) containing either 'YES' if it is possible to reach the bottom-right corner, or 'NO' if it is not.## sample

3
3
1 2 4
2 3 5
6 5 4
4
1 1 1 1
1 2 2 1
1 2 2 1
1 1 1 1
2
3 1
2 2
NO

YES NO

</p>