Poisonous Swamp
InterviewTime limit8sMemory limit512 MB
On a 100x100 grid with one non-poisonous rectangle, find the minimum total damage (entries into swamp cells) to visit N destinations in order.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, BFS, Implementation
- Solved
- No attempts yet
Problem
You are playing a retro role-playing game. The field of this game is a grid with 100 rows and 100 columns. The cell in column x from the left and row y from the top is denoted (x, y). The character you control is on one of the cells of the field, and can move one cell at a time up, down, left, or right within the field.
The character you control is now at (X0, Y0) and will visit N destinations in order. However, when you move the character, you must pay attention to the types of cells in the field. Each cell is either a poisonous swamp or non-poisonous land. If the destination cell of a move is a poisonous swamp, the character takes damage; if the destination cell is non-poisonous land, the character takes no damage. To reduce the damage to the character, you want to choose a route that minimizes the number of times the character takes damage. Note that whether damage occurs is determined by the type of the destination cell of the move. For example, if the source cell of a move is a poisonous swamp and the destination cell is non-poisonous land, the character takes no damage.
According to your analysis, the cells inside the rectangle whose top-left corner is (A, B) and whose bottom-right corner is (C, D) are non-poisonous land, and all other cells are poisonous swamp. Find the number of times the character you control takes damage when you visit the N destinations in order so as to minimize the number of times the character takes damage from poisonous swamp.
Input
The input consists of at most 50 datasets. Each dataset is given in the following format.
N
A B C D
X0 Y0
X1 Y1
X2 Y2
...
XN YN
A dataset consists of N+3 lines.
The first line is the integer N (1 ≤ N ≤ 100), the number of destinations.
The second line contains the integers A, B, C, D representing the rectangular region that is non-poisonous land, satisfying 1 ≤ A ≤ C ≤ 100 and 1 ≤ B ≤ D ≤ 100. When the destination cell (x, y) of a move satisfies A ≤ x ≤ C and B ≤ y ≤ D, and only then, the character takes no damage.
The third line contains the integers (X0, Y0) representing the coordinates of the cell where the character you control starts, satisfying 1 ≤ X0, Y0 ≤ 100. The following N lines from the fourth line give the coordinates of the N destinations. The (3+i)-th line contains the integers (Xi, Yi) representing the coordinates of the i-th destination cell, satisfying 1 ≤ Xi, Yi ≤ 100. The coordinates of the starting cell and the destination cells are all distinct, that is, (Xj, Yj) ≠ (Xk, Yk) (0 ≤ j < k ≤ N).
The end of the input is indicated by a line containing a single zero.
Output
For each dataset, output in one line the number of times the character you control takes damage.
Hint
The sample input is shown in the figure below. From (3, 3) to (5, 3) is non-poisonous land, but (6, 3) and (7, 3) are poisonous swamp, so the character takes damage 2 times before reaching the first destination. If you move from the first destination to the second destination downward 4 times along the shortest path, every cell is poisonous swamp, so the character takes damage 4 times. If you take a detour through non-poisonous land, the character enters the poisonous swamp at (6, 3), (6, 7), and (7, 7), so it takes damage 3 times. When you choose a movement that minimizes the number of times damage is taken, the number of times damage is taken is 5.
