#K40872. King's Journey on an Elevated Grid
King's Journey on an Elevated Grid
King's Journey on an Elevated Grid
You are given a square grid of size \(n \times n\) where each cell contains an integer representing its elevation. A king starts at the top-left corner of the grid and wants to reach the bottom-right corner. However, the king can only move to the right or down.
At every move from cell \((x,y)\) to cell \((nx,ny)\), the move is allowed if the elevation of the new cell satisfies the condition \[ grid[nx][ny] \leq grid[x][y] + h \] where \(h\) is a given non-negative integer.
Your task is to determine whether the king can reach the destination following these rules. Print YES
if the destination is reachable, and NO
otherwise.
Note: Movement is only allowed to the right and downward directions.
inputFormat
The input is read from standard input (stdin) and has the following format:
t n h row1 row2 ... rown ... (repeated for t test cases)
Here, t
is the number of test cases. For each test case, the first line contains two integers \(n\) and \(h\), where \(n\) is the size of the grid and \(h\) is the elevation tolerance. This is followed by \(n\) lines, each containing \(n\) integers representing the grid's rows.
outputFormat
For each test case, output a single line to standard output (stdout) containing either YES
if the king can reach the bottom-right cell following the allowed moves and elevation constraint, or NO
otherwise.
2
3 2
1 2 3
2 4 5
3 5 6
3 1
1 2 3
2 4 5
3 5 6
YES
NO
</p>