Banner Home Page Web Contests Problems Ranklist Status Statistics
if you finished the problem set: log("You're breathtaking!") else: log("Let's play Cyberpunk!")

Walk

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 0   Accepted Submission(s) : 0
Problem Description
Biaoge is planning to walk to amusement park. The city he lives can be abstracted as a 2D plane. Biaoge is at (x1, y1) and the amusement park is at (x2, y2). There are also some rectangle buildings. Biaoge can only walk parallel to the coordinate axis. Of course Biaoge can¡¯t walk across the buildings.
What¡¯s the minimum number of turns Biaoge need to make?

As the figure above shows, there are 4 buildings and Biaoge need to make at least 3 turns to reach the amusement park(Before walking he can chose a direction freely). It is guaranteed that all the buildings are parallel to the coordination axis. Buildings may contact but overlapping is impossible. The amusement park and Biaoge¡¯s initial positions will not contact or inside any building.
 

Input
There are multiple test case. Each test case contains several lines. The first line contains 4 integers x1, y1, x2, y2 indicating the coordinate of Biaoge and amusement park. The second line contains one integer N(0¡ÜN¡Ü50), indicating the number of buildings. Then N lines follows, each contains 4 integer x1, y1, x2, y2, indicating the coordinates of two opposite vertices of the building. Input ends with 0 0 0 0, you should not process it. All numbers in the input range from -10[sup]8[/sup] to 10[sup]8[/sup].
 

Output
For each test case, output the number of least turns in a single line. If Biaoge can¡¯t reach the amusement park, output -1 instead.
 

Sample Input
0 0 0 10 1 0 5 5 8 0 0 0 10 2 0 5 5 8 -2 1 0 5 0 0 0 0
 

Sample Output
0 2 [hint] In the first case, Biaoge can walk along the side of building, and no turn needed. In the second case, two buildings block the direct way and Biaoge need to make 2 turns at least. [/hint]
 

Source
2012 Asia JinHua Regional Contest
 

Statistic | Submit | Back