#P2199. 最后的迷宫

    ID: 1169 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>搜索枚举,暴力广度优先搜索,BFS队列

最后的迷宫

Description

Harry has excellent eyesight: he can look in a straight line from one side of the maze to the other (but only in eight directions — NE, E, SE, S, SW, W, NW, N). He also runs very fast: taking one step (moving one cell up, down, left, or right) costs only 1s1\text{s}. However, the maze walls are opaque, and burning them down is not easy, so Harry decides to run to a place where he can see the trophy. Now he wants you to determine the minimum time needed for him to obtain the trophy.

Input Format

The first line contains 22 numbers N,MN, M indicating the size of the maze (NN is height, MM is width).

Next comes the N×MN \times M maze: O\texttt{O} denotes open ground, and X\texttt{X} denotes a wall.

Finally, there are multiple pairs of data, each giving the coordinates of the trophy and Harry (obviously not on walls), one pair per line. A single 00 marks the end.

Output Format

For each pair of data, output the minimum time for Harry to obtain the trophy, one per line. If the Ministry of Magic makes things difficult by surrounding the trophy with walls, output Poor Harry\texttt{Poor Harry}.

3 4
OXXO
XXOO
XOOO
3 2 2 4
3 3 1 1
0 0 0 0

1
Poor Harry

Hint

For 30%30\% of the testdata, N×M100N \times M \le 100.

For 60%60\% of the testdata, N×M1600N \times M \le 1600.

For 100%100\% of the testdata, N×M16384N \times M \le 16384.

The number of query pairs does not exceed 512512.

Translated by ChatGPT 5