Rocky Mountain Road Trip
시간 제한2초메모리 제한2048 MB
연속된 고도 변화가 오르기와 내리기를 번갈아 가야 하는 격자에서 왕처럼 이동하는 최단 경로의 길이를 구한다.
문제
Adeline and Byron are on a road trip through the mountains. Interestingly, the roads through the mountains can be modeled as a grid of size , where each cell has an integer altitude. The grid follows standard Cartesian coordinates, with the top-left corner being and the bottom-right corner being .
Adeline, an adrenaline junkie, loves the ups and downs of the mountains, while Byron gets motion sick easily. After some debate, they agreed on a compromise: they must find a route from their starting position to their destination that minimizes the total distance traveled, but to make it fun for Adeline, they must alternate between gaining and losing altitude with every move. If they take a longer path than necessary, Byron will get sick.
Adeline and Byron's car can move in any of the 8 cardinal directions: North, Northeast, East, Southeast, South, Southwest, West, and Northwest. Each movement to an adjacent cell counts as a distance of 1, regardless of direction.
Help Adeline and Byron determine the minimum number of moves required to reach their destination.
입력
The first line contains two integers and —the dimensions of the grid.
The next lines each contain integers , representing the altitude of each cell in the grid.
The next line contains four integers —the starting and destination positions.
It is guaranteed that the starting and destination positions are distinct.
출력
Print a single integer—the minimum number of moves required to reach from , or print if it is impossible to reach the destination.