This page is still under construction.

Parts of this page are still being built. What you see may change.

Cows on Ice

Time limit1sMemory limit128 MB

Summary
Bessie slides on ice until a rock stops her; find the minimum number of pushes to move from her start cell to the goal cell.
Level

Medium7 of 10

Topics
BFS, Graph, Hash map, Simulation
Solved
No attempts yet

Problem

Bessie is ice skating on a large frozen lake modeled as a 2D grid whose coordinates range from −109-10^9 to 10910^9 on both axes. NN (1≤N≤200001 \le N \le 20000) of the grid cells contain rocks, numbered 11 through NN; every other cell is slippery ice.

Bessie is a poor skater, so she can only move by pushing off from her current cell (which always sits next to a rock) and then sliding in a straight line until she slams into another rock, coming to rest in the cell immediately before that rock. She can push herself only straight north, east, south, or west, and she cannot push through a rock, so she usually has at most three useful directions.

A slide only works if some rock lies ahead of her in that direction to stop her; with no rock ahead she would slide forever, so every push must be aimed carefully.

For example, Bessie (B) wants to reach the goal (G) at (x=5,y=1)(x = 5, y = 1), directly east of her (. = ice, * = rock, B = Bessie, G = goal). Sliding straight east would carry her past the goal, because she can only stop by hitting a rock. One way to reach (5,1)(5, 1) is:

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

In situation (a) she could try north, east, or south, but only the northward slide has a rock to stop her. In situation (b) only the eastward slide has a stopping rock.

Rock ii sits at (Xi,Yi)(X_i, Y_i) with each coordinate between −109-10^9 and 10910^9, and no two rocks share a cell. Bessie starts at (Bx,By)(B_x, B_y), always next to a rock, and her goal is (Gx,Gy)(G_x, G_y); every coordinate lies in the same range, and the goal is always reachable.

Sliding costs Bessie nothing, but pushing off a rock tires her out. Determine the minimum number of pushes she needs to reach the goal.

Input

  • Line 1: five space-separated integers NN, BxB_x, ByB_y, GxG_x, GyG_y.
  • Lines 2 through N+1N + 1: line i+1i + 1 contains two space-separated integers XiX_i and YiY_i, the location of rock ii.

Output

  • Line 1: a single integer, the minimum number of pushes Bessie needs to reach her goal.

Examples3

  1. Example 1

    Input
    6 2 1 5 1
    5 4
    2 3
    1 1
    6 2
    5 0
    0 0
    
    Expected output
    3
    
  2. Example 2

    Input
    2 1 0 4 0
    0 0
    5 0
    
    Expected output
    1
    
  3. Example 3

    Input
    3 0 1 4 4
    0 0
    0 5
    5 4
    
    Expected output
    2