#K36627. Traverse the City
Traverse the City
Traverse the City
You are given an (N \times N) grid where each cell contains an integer representing the height at that position in a city. Starting from the top-left cell, determine if there exists a path to the bottom-right cell by moving only up, down, left, or right. You can only move from one cell to an adjacent cell if the absolute difference in heights does not exceed (k), i.e. ( |a - b| \le k ).
The task is to check if such a route exists. If it exists, output "YES"; otherwise, output "NO".
inputFormat
The input is given via standard input (stdin).
The first line contains two integers, (N) (the grid size) and (k) (the maximum allowed height difference).
Each of the next (N) lines contains (N) space-separated integers representing the heights in the grid.
outputFormat
Output a single line to standard output (stdout) containing either "YES" if a valid path exists, or "NO" if it does not.## sample
4 3
1 4 2 3
2 3 3 4
3 5 4 2
4 2 3 2
YES