Chisam's Stepping Stone Crossing
Time limit1sMemory limit1024 MB
Water spreads from given sources one cell per day; find the earliest day Chisam can walk over watered stones from (1,1) to (N,N).
- Level
Medium7 of 10
- Topics
- BFS, Binary search, Graph, Array
- Solved
- No attempts yet
Problem
Chisam wants to cross a square valley that is wide and tall. But the tremendous heat has dried up all the water!
The valley consists of ground and stones. The whole ground is made of cells, and a cell may contain a stone. The ground and stones in the valley are too hot to walk on. However, a cell with a stone becomes cool enough to walk on once water flows into it. Fortunately, kind students dug into the ground to create water sources for Chisam, and Chisam decided to cross the valley by stepping on stones. A water source may lie on either ground or a stone. Water spreads once a day from each cell that contains water to cells adjacent to it. Diagonal cells are not considered adjacent.
Chisam wants to travel from the starting point to the destination . Stones always lie at the starting point and the destination , and Chisam can walk on them without meeting water. Chisam's movement takes no time. Chisam can move up, down, left, right, and diagonally. Find the number of days it takes for Chisam to reach the destination as fast as possible.
Input
The first line gives the size of the ground and the number of water sources .
Lines 2 through give the position of each water source (row), (column) .
From line , lines give the map of the stream. 1 marks a cell with a stone, and 0 marks a cell with ground.
, , , and are all positive integers.
Output
Print the number of days it takes to reach the destination as fast as possible. If Chisam cannot reach the destination , print -1.