atcoder#ABC246E. [ABC246E] Bishop 2
[ABC246E] Bishop 2
Score : points
During the time of the contest, the memory limit was set to 2GB. However, due to a change in the judging environment, the memory limit has now been set to 1GB. Please note that it has been confirmed that solutions can still achieve an Acceptable Completion (AC) within this memory limit.
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.
5
1 3
3 5
....#
...#.
.....
.#...
#....
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)$
4
3 2
4 2
....
....
....
....
-1
There is no way to move the bishop from to .
18
18 1
1 18
..................
.####.............
.#..#..####.......
.####..#..#..####.
.#..#..###...#....
.#..#..#..#..#....
.......####..#....
.............####.
..................
..................
.####.............
....#..#..#.......
.####..#..#..####.
.#.....####..#....
.####.....#..####.
..........#..#..#.
.............####.
..................
9