서로 겹치지 않는 직사각형 장애물이 있는 평면에서 공이 축에 평행하게만 움직일 수 있을 때, 시작점에서 도착점까지 필요한 최소 타수를 구한다.
어려움8그래프BFS기하구현아직 제출이 없습니다시간 제한5초메모리 제한1024 MBJOI-kun is a boy practicing golf in a special golf course.
The course is a plane with xy-coordinates. There are N obstacles on the course. The i-th obstacle (1 ≤ i ≤ N) occupies the rectangular region whose x-coordinates are greater than or equal to Ai and less than or equal to Bi, and y-coordinates are greater than or equal to Ci and less than or equal to Di. Two obstacles (including their boundaries) do not intersect with each other.
In this course, the start point is (S, T), and the end point is (U, V). These points are different, and not contained in any obstacles and their boundaries. In the beginning, the golf ball is placed at the start point.
JOI-kun can hit the golf ball, and move it for any distance toward any of four directions parallel to one of the coordinates of the plane. But, the track of the ball should not touch the interior of any obstacles. The ball can pass through the boundaries of the obstacles. It can stop on the boundary of an obstacle. Then, it can change the direction to move by hitting it toward the direction in which there is no obstacle.
JOI-kun wants to know the minimum number of hits needed to finish the course. Because you are JOI-kun’s golf friend, he asks you to calculate it.
What is the minimum number of hits needed to move the ball from the start point to the end point?
Given the information of the golf course, write a program which calculates the minimum number of hits needed to move the ball from the start point to the end point.
Read the following data from the standard input.
Write one line to the standard output. The output contains the minimum number of hits needed to move the golf ball from the start point to the end point.
All input data satisfy the following conditions.