#P1301. 魔鬼之城
魔鬼之城
Description
In a rectangular “Devil City” divided into square rooms, an explorer must follow the rules below to move by jumping. He must enter at and exit at . On the wall of every room there is a magic number, which is a natural number from to inclusive. The explorer can imagine any of the directions (horizontal, vertical, or diagonal), and then make a spatial jump passing through consecutive rooms in that direction, where is the magic number of the room he is currently in. However, if the number of rooms available in that direction is less than , he does not jump and must imagine another direction. In addition, the explorer cannot make two consecutive jumps in the same direction.

For example, in the Devil City shown above, if the explorer is currently at , then by making successive spatial jumps he can reach one of the following rooms: , , , , or . Moreover, if he wants to reach from in two jumps, he cannot first jump to (because then the direction of his second jump would be the same as the first, which is not allowed). Therefore, he must first jump to .
Please write a program that, for a given map, computes the minimum number of jumps the explorer needs to escape the Devil City.
Input Format
The first line contains two integers .
Then follow lines, each containing natural numbers, representing the magic numbers in the corresponding rooms.
Output Format
Output the minimum number of jumps. If the explorer cannot escape the Devil City, output NEVER.
5 4
3 3 6 7 11
3 2 1 1 3
3 2 2 1 1
2 1 2 2 1
4
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号