#K60207. Path Finding on a Grid with Height Constraint

    ID: 31035 Type: Default 1000ms 256MiB

Path Finding on a Grid with Height Constraint

Path Finding on a Grid with Height Constraint

You are given a grid of integers and an integer t. Your task is to determine whether there exists a path from the top-left cell to the bottom-right cell such that for every move between two adjacent cells the absolute difference of their values satisfies the condition $$|h_i - h_j| \leq t$$. You may move only in the four cardinal directions: up, down, left, or right.

This problem simulates a height-map where moving from one cell to another is only possible if the difference in height is within an acceptable range. A correct solution should use a breadth-first search (BFS) or depth-first search (DFS) algorithm to verify connectivity under the height constraint.

inputFormat

The first line contains two space-separated integers m and n, representing the number of rows and columns of the grid respectively. The following m lines each contain n space-separated integers, representing the grid values.

The last line contains a single integer t, the maximum allowed height difference between neighboring cells.

outputFormat

Output a single line containing True if there exists a path from the top-left cell to the bottom-right cell where each move satisfies the condition, or False otherwise.

## sample
3 3
1 3 5
4 2 3
7 1 2
3
True