Cliff Walking
Time limit1sMemory limit128 MB
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 and , he can move only when the height difference 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 (meters) can be written with the time (hours) since high tide and a height (meters) fixed by those factors:
The walk starts at his house at and ends at his house. He has to be back before the tide returns, so the walk has to finish within . How far from home can Sanggeun get and still come back?
Input
The first line has a real number () and the time () it takes to cross one square. is given in seconds.
The second line has four integers , , , . (, , ) and are the width and the height of the map, and (, ) is the position of Sanggeun's house.
Each of the next lines has 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 changes by up to 0.1%.