100 atcoder#ABC176D. [ABC176D] Wizard in Maze
[ABC176D] Wizard in Maze
Score : points
Problem Statement
A maze is composed of a grid of squares - vertical, horizontal.
The square at the -th row from the top and the -th column from the left - - is a wall if is #
and a road if is .
.
There is a magician in . He can do the following two kinds of moves:
- Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.
- Move B: Use magic to warp himself to a road square in the area centered at the square he is currently in.
In either case, he cannot go out of the maze.
At least how many times does he need to use the magic to reach ?
Constraints
- is
#
or.
. - and are
.
.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of times the magician needs to use the magic. If he cannot reach , print -1
instead.
4 4
1 1
4 4
..#.
..#.
.#..
.#..
1
For example, by walking to and then using the magic to travel to , just one use of magic is enough.
Note that he cannot walk diagonally.
4 4
1 4
4 1
.##.
####
####
.##.
-1
He cannot move from there.
4 4
2 2
3 3
....
....
....
....
0
No use of magic is needed.
4 5
1 2
2 5
#.###
####.
#..##
#..##
2