#P2537. [AHOI2005] 穿越磁场

    ID: 1552 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2005各省省选离散化安徽广度优先搜索,BFS最短路

[AHOI2005] 穿越磁场

Description

An exploration robot is searching for a peculiar ore on planet Samuel, but it has become trapped in a mysterious magnetic field region and cannot move.

The exploration station immediately scanned the area and drew a planar map of the magnetic field distribution. There are NN magnetic fields in this region. Each magnetic field is a square whose sides are parallel to the coordinate axes.

For example, in the figure below, there are 33 magnetic fields. The white dot is the robot’s position, and the black dot is the ore’s position:

After analyzing the map, scientists further found that these magnetic fields are squares of various sizes. They may intersect or even overlap, but their edges never coincide, and their vertices never coincide.

For example, the following two cases will not occur:

Scientists activated a magnetic shield for the exploration robot so that it can move freely through the magnetic fields.

Initially, neither the robot nor any ore lies on any magnetic field edge. Due to technical limitations, during traversal the robot can only move horizontally or vertically, and it cannot move along the edges of magnetic fields.

Because the magnetic shield’s energy is limited, the scientists hope the robot will cross as few magnetic field edges as possible to collect the ore. In the example above, the robot needs to cross edges at least twice.

Now, please write a program to help the scientists design the robot’s route and compute the minimum number of magnetic field edges the robot must cross.

Input Format

The first line contains an integer NN, meaning there are NN magnetic fields (1<N<1001 < N < 100). Then follow NN lines, each containing three integers X,Y,CX, Y, C (0<X,Y,C<100000 < X, Y, C < 10000), indicating a magnetic field whose lower-left corner is (X,Y)(X, Y) and whose side length is CC. The next line contains four integers SX,SY,TX,TYSX, SY, TX, TY, indicating that the robot’s initial coordinate is (SX,SY)(SX, SY) and the ore’s coordinate is (TX,TY)(TX, TY) (where 1<SX,SY,TX,TY<100001 < SX, SY, TX, TY < 10000).

Output Format

Output a single integer on one line, the minimum number of magnetic field edges the robot needs to cross.

2
1 3 3 
2 1 4
0 0 3 4
2

Hint

Translated by ChatGPT 5