#abc246e. [abc246_e]Bishop 2
[abc246_e]Bishop 2
Problem Statement
We have an chessboard. Let denote the square at the -th row from the top and -th column from the left of this board.
The board is described by strings .
The -th character of the string , , means the following.
- If
.
, the square is empty. - If
#
, the square is occupied by a white pawn, which cannot be moved or removed.
We have put a white bishop on the square .
Find the minimum number of moves needed to move this bishop from to according to the rules of chess (see Notes).
If it cannot be moved to , report -1
instead.
Notes
A white bishop on the square can go to the following positions in one move.
-
For each positive integer , it can go to if all of the conditions are satisfied.
- The square exists in the board.
- For every positive integer , is not occupied by a white pawn.
-
For each positive integer , it can go to if all of the conditions are satisfied.
- The square exists in the board.
- For every positive integer , is not occupied by a white pawn.
-
For each positive integer , it can go to if all of the conditions are satisfied.
- The square exists in the board.
- For every positive integer , is not occupied by a white pawn.
-
For each positive integer , it can go to if all of the conditions are satisfied.
- The square exists in the board.
- For every positive integer , is not occupied by a white pawn.
Constraints
- is a string of length consisting of
.
and#
. -
.
-
.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
5
1 3
3 5
....#
...#.
.....
.#...
#....
Sample Output 1
3
We can move the bishop from to in three moves as follows, but not in two or fewer moves.
- $(1,3) \\rightarrow (2,2) \\rightarrow (4,4) \\rightarrow (3,5)$
Sample Input 2
4
3 2
4 2
....
....
....
....
Sample Output 2
-1
There is no way to move the bishop from to .
Sample Input 3
18
18 1
1 18
..................
.####.............
.#..#..####.......
.####..#..#..####.
.#..#..###...#....
.#..#..#..#..#....
.......####..#....
.............####.
..................
..................
.####.............
....#..#..#.......
.####..#..#..####.
.#.....####..#....
.####.....#..####.
..........#..#..#.
.............####.
..................
Sample Output 3
9