Junan's Rampage
InterviewTime limit2sMemory limit512 MB
Each jump clears all friends in the four straight rays from Junan's cell until the first friend in each direction; find the fewest jumps to clear the culprit's cell.
- Level
Medium6 of 10
- Topics
- BFS, Graph, Simulation, Implementation
- Solved
- No attempts yet
Problem
Junan is furious. The chocolate bar he had hidden in his desk drawer to eat in secret has disappeared. Junan has started going wild. In fact, he has literally started jumping.
'Thud... thud...'
Junan wants to knock down (?) every friend around him with the shock wave of his jumps and find whoever stole the chocolate bar. He starts jumping somewhere in an classroom. His wave spreads in the four directions (up, down, left, right) until it reaches a friend, knocks that friend down (?), and stops there. In other words, one jump knocks down one layer of friends. A cell whose friend has been knocked down becomes empty space. Consider the following example.
1 # 1 0 1 1 1
1 1 0 1 0 0 1
0 0 1 * 1 1 1
1 1 0 1 1 1 1
0 0 1 1 0 0 1
Junan, shown as *, is at , and the culprit holding the chocolate bar, shown as #, is at . 0 is empty space with no obstacle, and 1 is a cell where a friend is standing. The following grids show how the surviving (?) students change after each of Junan's jumps.
1 # 1 0 1 1 1
1 1 0 0 0 0 1
0 0 0 * 0 1 1
1 1 0 0 1 1 1
0 0 1 1 0 0 1
1 # 0 0 0 0 1
0 0 0 0 0 0 0
0 0 0 * 0 0 1
0 0 0 0 0 1 1
0 0 0 0 0 0 1
0 X 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 * 0 0 0
0 0 0 0 0 0 1
0 0 0 0 0 0 0
In this example Junan finds the thief after 3 jumps. The culprit is knocked down by the wave exactly like a friend, and the culprit is caught at the moment he falls.
Junan must be stopped quickly to keep the classroom at peace. Protect the classroom by telling Junan the minimum number of jumps.
Input
The first line contains the size of the classroom, and .
The second line contains Junan's position , and the culprit's position , . , Positions are given as (row, column), counted from 1.
Each of the next lines describes one row of the classroom as characters with no spaces. 0 is empty space, 1 is a friend, * is Junan, and # is the culprit.
Output
Print the minimum number of jumps Junan needs to catch the culprit.
Hint
Like the ripples from a stone dropped into a lake, the wave spreads in the four directions (up, down, left, right) and keeps spreading until it meets an obstacle (a friend).
# 0 0 0 0
1 1 1 1 1
0 0 0 0 *
After the first jump, the classroom above becomes the following.
# 0 0 0 0
0 0 0 0 0
0 0 0 0 *
One more jump then lets the wave reach #, and the culprit is caught.