Bitaro’s Travel 2
시간 제한4초메모리 제한2048 MB
격자 위 산 높이와 점프 길이 L이 주어질 때, 두 칸 사이를 최소 몇 번의 하이 점프로 이동할 수 있는지 구하고 불가능하면 -1을 출력한다.
문제
The JOI Mountain Range consists of many mountains. It is represented as a grid with rows and columns, where the north-south direction is vertical, and the east-west direction is horizontal. The cell at the -th row from the north () and the -th column from the west () is denoted as . There is exactly one mountain in each cell. The height of the mountain at cell is .
Bitaro, the beaver, can move between the summits of the mountains using the procedure called high jump, which is described below. Here, is the parameter for his jumping skill.
- Bitaro floats straight up from the summit of the current mountain. When the altitude of the summit is , Bitaro will float up to the point of altitude .
- Bitaro then repeats moving to the adjacent cell in one of the four directions without changing the altitude. The height of the mountains at the visiting cells must be lower than the altitude at which he is floating.
- Bitaro finally lands at the summit of the current cell’s mountain.
Bitaro is planning for trips. In the -th trip (), he plans to move from the summit of the cell ’s mountain to the summit of the cell ’s mountain by only using high jumps. He wants to know if these trips are possible, and if so, he also wants to know the minimum number of high jumps needed, because high jumps require much energy.
The information on the mountains, Bitaro’s jumping skill, and his trip plans, are given. Write a program that, for each trip plan, determines whether it is possible, and calculates the minimum number of high jumps needed if the trip is possible.
입력
Read the following data from the standard input.
출력
Write lines to the standard output. In the -th line (), output the minimum number of high jumps needed in the -th trip if the trip is possible. If the trip is impossible, output -1.
제한
- .
- .
- .
- .
- (, ).
- .
- ().
- ().
- ().
- ().
- ().
- Given values are all integers.