luogu#P4771. 八百标兵奔北坡

八百标兵奔北坡

Background

baingbaboom is running north!!!

Problem Description

Now on an NMN*M map, there are KK babingbabooms!!! For each point on the map, there is a hi,jh_{i,j} representing the height of that location. Now these babingbabooms all want to run to a mountain slope in the north. Find the nearest mountain to the north for each babingbaboom.

Additional definitions:

Mountain: a point whose surrounding area has no place higher than it (4-connected).

In the north: Let the babingbaboom's coordinate be A(a,b)A(a,b) and the mountain's coordinate be B(x,y)B(x,y). The mountain is to the north of the babingbaboom if and only if disA,B=axdis_{A,B}=a-x.

Chebyshev distance:

$A(x_1,y_1) B(x_2,y_2):dis_{A,B}=\max(|x_1 - x_2|, |y_1 - y_2|)$

Input Format

Line 11 contains three positive integers N,M,KN,M,K.

Lines 22 to N+1N+1 each contain MM positive integers hi,jh_{i,j}.

Lines N+2N+2 to N+K+1N+K+1 each contain two positive integers Xi,YiX_i,Y_i, indicating the coordinate of each babingbaboom.

Output Format

Output KK lines in total. If for a babingbaboom there exists such a nearest mountain (by Chebyshev distance), output the Chebyshev distance from this babingbaboom to that mountain. Otherwise, output "Pool Babingbaboom!".

5 5 5
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
1 2
2 3
3 4
4 5
5 1

Pool Babingbaboom!
Pool Babingbaboom!
1
2
0

Hint

Constraints: 1N,M1031 \leqslant N,M \leqslant 10^3, 1K1051 \leqslant K \leqslant 10^5, 1hi,j1091 \leqslant h_{i,j} \leqslant 10^9.

The testdata has gradients.

Sample image (stars represent babingbabooms, red represents mountains):

(The vertical axis is xx, and the horizontal axis is yy. I did not pay attention when drawing it, sorry.)

Translated by ChatGPT 5