#P2981. [USACO10FEB] Cows on Ice G

[USACO10FEB] Cows on Ice G

Description

Bessie is ice skating on a large frozen lake modelled as a 2-dimensional grid with coordinates in the range 109109-10^9 \cdots 10^9. N(1N20,000)N(1 \le N \le 20,000) of the lake's grid cells contain rocks(conveniently numbered 1N1 \cdots N). The other cells contain slippery ice.

Since Bessie is not a very good skater, she traverses the lake's cells by pushing herself away from her current position near a rock and sliding continuously in the same direction until she hits another rock (stopping in the square before she hits the rock, of course). Never good with complicated angles, Bessie can push herself only straight north, east, south, or west. She can't push herself through the rock, of course, and thus generally has only three possible directions to move.

Sliding is not without risks. Bessie must hit a rock or might end up sliding for a very long time. She must aim her pushes carefully.

Consider the situation depicted below; Bessie wants to get to location (5,1)(5, 1), which is east of her current location (. = ice, * = rock, B = Bessie, G = goal). If she slides directly to the east, she will slide past the location since she can stop only by encountering a rock head on. One way to get to (5,1)(5, 1) is the following:

    (a)             (b)             (c)             (d)
4 .....*.         .....*.         .....*.         .....*.
3 ..*....  slide  ..*....  slide  ..*....  slide  ..*....
2 ......*  north  ..B...*  east   .....B*  south  ......*
1 .*B..G. ------> .*...G. ------> .*...G. ------> .*...B.
0 *....*.         *....*.         *....*.         *....*.
  0123456

Bessie could slide north, east, or south in situation (a), but only north has a rock for stopping. For situation (b), she can slide only east for the same reason.

For the input, rock i is located at cell $(X_i,Y_i)(-10^9 \le X_i \le 10^9,-10^9 \le Y_i \le 10^9)$, and no two rocks occupy the same position. Bessie starts at BxB_x,ByB_y (which is next to a rock) (109Bx109,109By109)(-10^9 \le Bx \le 10^9,-10^9 \le By \le 10^9). Bessie's goal is GxG_x,GyG_y (10Gx109,109Gy109)(-10 \le Gx \le 10^9,-10^9 \le Gy \le 10^9). Bessie can always reach the goal one way or another.

Bessie doesn't mind sliding. However, pushing herself away from a rock is very tiring. To prepare her, FJ would like to know the minimum number of pushes Bessie needs to do.

Input Format

  • Line 1: Five space separated integers: NN, BxB_x, ByB_y, GxG_x, and GyG_y.

  • Lines 2N+12 \cdots N+1: Line i+1i+1 describes a rock location with space separated integers: XiX_i and YiY_i.

Output Format

Line 1: A single integer that is the minimum number of pushes for Bessie to get to her goal.

6 2 1 5 1 
5 4 
2 3 
1 1 
6 2 
5 0 
0 0 

3