This page is still under construction.

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

Cliff Walking

Time limit1sMemory limit128 MB

Summary
Find the farthest square reachable on a 12-hour round trip over a tidal grid, stepping only between squares within 1m of height after each dried for an hour.
Level

Medium7 of 10

Topics
Shortest path, Graph, Intervals, Math
Solved
No attempts yet

Problem

Sanggeun lives on the Atlantic coast. One day he looked up and saw a full moon. A full moon makes the gap between high tide and low tide larger. Sanggeun wants to pick a day without rain and walk along the shoreline.

If the tide comes in while he is out there, the water traps him. So he has to study the tide before he plans the walk.

Walking at low tide is safe. The trouble is that this shore is covered with stones. Slipping on a stone hurts, so Sanggeun can step onto a square only once it has been out of the water for one hour.

The beach is mostly sand with stones lying on it. Every square is either dry or under water. A square goes under the moment the water level rises above the height of that square, and the heights of the neighboring squares do not matter.

The beach splits into squares of 10 × 10m, and the height of every square is known. To enter a square he has to come from one of the four squares next to it. If the two squares have heights z1z_1 and z2z_2, he can move only when the height difference ∣z1−z2∣|z_1 - z_2| is at most 1m.

Moving from one square to the next always takes the same amount of time, and both squares have to stay dry for the whole move.

The sea level depends on several factors. Sanggeun found that the water height vv (meters) can be written with the time tt (hours) since high tide and a height aa (meters) fixed by those factors:

v=0.5a(cos⁡(t2π12)+1)v = 0.5a\left(\cos\left(t\frac{2\pi}{12}\right) + 1\right)

The walk starts at his house at t=0t = 0 and ends at his house. He has to be back before the tide returns, so the walk has to finish within 0.0≤t≤12.00.0 \le t \le 12.0. How far from home can Sanggeun get and still come back?

Input

The first line has a real number aa (0.0<a<15.00.0 < a < 15.0) and the time mm (0.1≤m≤60.00.1 \le m \le 60.0) it takes to cross one square. mm is given in seconds.

The second line has four integers WW, HH, XX, YY. (1≤W,H≤2001 \le W, H \le 200, 0≤X<W0 \le X < W, 0≤Y<H0 \le Y < H) WW and HH are the width and the height of the map, and (XX, YY) is the position of Sanggeun's house.

Each of the next HH lines has WW integers separated by spaces. Each integer is the height of one 10 × 10m square, measured from the lowest sea level, in millimeters. Every height is at least 0 and at most 20,000. The first number of the first line is the square at (0, 0), and Sanggeun's house is always dry.

Output

Among the squares he can walk to and still return home from, take the one farthest from his house and print the square of the Euclidean distance to it. The distance between two squares is measured between their centers, and distances are in meters, so the printed value is in square meters. Centers sit 10m apart, so the answer is always an integer. Print 0 if he cannot leave his house.

The test data is built so that the answer stays the same when the walking time mm changes by up to 0.1%.

Examples2

  1. Example 1

    Input
    2.0 10.0
    3 3 0 0
    2001 1000 100
    1001 10000 200
    100 0 0
    
    Expected output
    400
    
  2. Example 2

    Input
    4.0 30.0
    6 2 2 0
    73 1001 4001 1001 76 70
    70 2001 3001 2001 72 71
    
    Expected output
    500