atcoder#ABC170F. [ABC170F] Pond Skater
[ABC170F] Pond Skater
Score : points
Problem Statement
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with east-west rows and north-south columns. Let be the square at the -th row from the north and -th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered.
The square has a lotus leaf on it if is @
, and it does not if is .
.
In one stroke, Snuke can move between and squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square to . If the travel from to is impossible, point out that fact.
Constraints
- or .
- is
.
or@
. -
.
-
.
- All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of strokes Snuke takes to travel from the square to , or print -1
if the travel is impossible.
3 5 2
3 2 3 4
.....
.@..@
..@..
5
Initially, Snuke is at the square . He can reach the square by making five strokes as follows:
- From , go west one square to .
- From , go north two squares to .
- From , go east two squares to .
- From , go east one square to .
- From , go south two squares to .
1 6 4
1 1 1 6
......
2
3 3 1
2 1 2 3
.@.
.@.
.@.
-1