Bitaro’s Travel 2

시간 제한4초메모리 제한2048 MB

요약
격자 위 산 높이와 점프 길이 L이 주어질 때, 두 칸 사이를 최소 몇 번의 하이 점프로 이동할 수 있는지 구하고 불가능하면 -1을 출력한다.
난이도

어려움10점 중 9점

유형
그래프, BFS, 최단 경로, 이분 탐색
정답자
아직 제출이 없습니다

문제

The JOI Mountain Range consists of many mountains. It is represented as a grid with HH rows and WW columns, where the north-south direction is vertical, and the east-west direction is horizontal. The cell at the ii-th row from the north (1≤i≤H1 ≤ i ≤ H) and the jj-th column from the west (1≤j≤W1 ≤ j ≤ W) is denoted as (i,j)(i, j). There is exactly one mountain in each cell. The height of the mountain at cell (i,j)(i, j) is T_i,jT\_{i, j}.

Bitaro, the beaver, can move between the summits of the mountains using the procedure called high jump, which is described below. Here, LL is the parameter for his jumping skill.

  1. Bitaro floats straight up from the summit of the current mountain. When the altitude of the summit is xx, Bitaro will float up to the point of altitude x+L+0.5x + L + 0.5.
  2. 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.
  3. Bitaro finally lands at the summit of the current cell’s mountain.

Bitaro is planning for QQ trips. In the kk-th trip (1≤k≤Q1 ≤ k ≤ Q), he plans to move from the summit of the cell (A_k,B_k)(A\_k, B\_k)’s mountain to the summit of the cell (C_k,D_k)(C\_k, D\_k)’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.

HH WW LL

T_1,1T\_{1,1} T_1,2T\_{1,2} ⋯\cdots T_1,WT\_{1,W}

T_2,1T\_{2,1} T_2,2T\_{2,2} ⋯\cdots T_2,WT\_{2,W}

⋮\vdots

T_H,1T\_{H,1} T_H,2T\_{H,2} ⋯\cdots T_H,WT\_{H,W}

QQ

A_1A\_1 B_1B\_1 C_1C\_1 D_1D\_1

A_2A\_2 B_2B\_2 C_2C\_2 D_2D\_2

A_QA\_Q B_QB\_Q C_QC\_Q D_QD\_Q

출력

Write QQ lines to the standard output. In the kk-th line (1≤k≤Q1 ≤ k ≤ Q), output the minimum number of high jumps needed in the kk-th trip if the trip is possible. If the trip is impossible, output -1.

제한

  • 1≤H1 ≤ H.
  • 1≤W1 ≤ W.
  • 2≤H×W≤300,0002 ≤ H \times W ≤ 300\\, 000.
  • 1≤L≤1091 ≤ L ≤ 10^9.
  • 1≤T_i,j≤1091 ≤ T\_{i, j} ≤ 10^9 (1≤i≤H1 ≤ i ≤ H, 1≤j≤W1 ≤ j ≤ W).
  • 1≤Q≤300,0001 ≤ Q ≤ 300\\, 000.
  • 1≤A_k≤H1 ≤ A\_k ≤ H (1≤k≤Q1 ≤ k ≤ Q).
  • 1≤B_k≤W1 ≤ B\_k ≤ W (1≤k≤Q1 ≤ k ≤ Q).
  • 1≤C_k≤H1 ≤ C\_k ≤ H (1≤k≤Q1 ≤ k ≤ Q).
  • 1≤D_k≤W1 ≤ D\_k ≤ W (1≤k≤Q1 ≤ k ≤ Q).
  • (A_k,B_k)≠(C_k,D_k)(A\_k, B\_k) \ne (C\_k, D\_k) (1≤k≤Q1 ≤ k ≤ Q).
  • Given values are all integers.

예제3

  1. 예제 1

    입력
    2 4 5
    1 3 22 1
    8 13 6 16
    6
    1 1 2 2
    1 1 1 3
    1 1 2 3
    1 1 2 4
    1 1 1 4
    1 1 1 2
    
    예상 출력
    3
    -1
    3
    4
    4
    1
    
  2. 예제 2

    입력
    6 5 11
    175 100 110 117 158
    144 133 123 150 191
    167 252 219 181 346
    231 241 280 201 209
    261 332 325 225 338
    269 298 315 291 308
    12
    1 1 4 2
    1 1 1 5
    1 1 5 1
    1 1 5 4
    1 1 3 4
    1 1 6 4
    1 1 2 5
    1 1 3 1
    1 1 4 4
    1 1 5 5
    1 1 6 2
    1 1 6 1
    
    예상 출력
    8
    1
    10
    6
    1
    13
    2
    1
    3
    19
    14
    11
    
  3. 예제 3

    입력
    4 4 5
    53 55 51 49
    56 60 89 45
    54 57 92 43
    96 99 95 92
    9
    1 4 2 3
    4 1 3 2
    2 4 2 3
    2 1 4 1
    1 2 1 1
    2 4 1 1
    4 1 2 3
    3 4 1 1
    1 3 1 4
    
    예상 출력
    -1
    1
    -1
    -1
    1
    3
    1
    4
    1