#P3716. [CTSC2000] 冰原探险

    ID: 2693 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2000WC/CTSC/集训队枚举,暴力广度优先搜索,BFS最短路

[CTSC2000] 冰原探险

Description

Legend says there is a vast icefield in Antarctica, under which lie ruins of a prehistoric civilization. The entire icefield is divided into many equal-sized grid cells. On this icefield there are NN rectangular icebergs of different sizes, each as ancient as Antarctica itself.

Each rectangular iceberg occupies at least one cell and always fully occupies cells. Icebergs neither overlap nor touch each other at edges or points. The following two cases cannot occur:

After years of preparation, the ACM\text{ACM} exploration team decided to search for the ruins on this icefield. According to their information, there is a one-cell-deep pit on the icefield that hides a switch made by prehistoric humans. The only thing that can activate this switch is a movable small ice block that is almost one cell in size. Obviously, such a small independent ice block could not naturally exist in Antarctica, so it must also be a product of the prehistoric civilization. They want to push this block into the pit to open a passage to the bottom of the icefield and excavate the secrets of the prehistoric civilization. Neither the starting position of the ice block nor the position of the deep pit is adjacent to any iceberg.

Both the ice surface and the icebergs are perfectly smooth. A gentle push makes the ice block slide forward until it hits an iceberg and stops next to its side. The ice block can slide through any area of the ice surface without icebergs and can pass between two icebergs (see the figure below). The ice block can only be pushed along the grid directions.

Please help them push the ice block into the deep pit with the minimum number of pushes.

Input Format

The first line contains the number of icebergs NN.
The second line contains the coordinates of the starting cell of the ice block, X1,Y1X_{1}, Y_{1}.
The third line contains the coordinates of the deep pit, X2,Y2X_{2}, Y_{2}.
Each of the next NN lines contains four integers, the coordinates of the upper-left and lower-right cells of an iceberg, Xi1,Yi1,Xi2,Yi2X_{i_{1}}, Y_{i_{1}}, X_{i_{2}}, Y_{i_{2}}.

Output Format

Output a single integer: the minimum number of pushes. If it is impossible to push the ice block into the deep pit, output 0.

2
1 1
5 5
1 3 3 3
6 2 8 4
3

Hint

1N40001 \leq N \leq 4000.

Sample explanation: The moving plan is shown in the figure.

Translated by ChatGPT 5