#abc246e. E - Bishop 2

E - Bishop 2

Score : 500500 points

问题描述

我们有一个 N×NN \times N 的棋盘。令 (i,j)(i, j) 表示该棋盘从上到下第 ii 行、从左到右第 jj 列的格子。

棋盘由 NN 个字符串 SiS_i 描述。字符串 SiS_i 的第 jj 个字符 Si,jS_{i,j} 表示如下含义:

  • Si,j=S_{i,j}= .,表示格子 (i,j)(i, j) 为空。
  • Si,j=S_{i,j}= #,表示格子 (i,j)(i, j) 被一个白色兵占据,且不能移动或移除。

我们在格子 (Ax,Ay)(A_x, A_y) 上放置了一个白色象(即主教)。
根据国际象棋规则(见注释),求从 (Ax,Ay)(A_x, A_y) 移动这个象到 (Bx,By)(B_x, B_y) 所需的最小步数。
如果无法移动到 (Bx,By)(B_x, B_y),则输出 -1

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have an N×NN \times N chessboard. Let (i,j)(i, j) denote the square at the ii-th row from the top and jj-th column from the left of this board.
The board is described by NN strings SiS_i.
The jj-th character of the string SiS_i, Si,jS_{i,j}, means the following.

  • If Si,j=S_{i,j}= ., the square (i,j)(i, j) is empty.
  • If Si,j=S_{i,j}= #, the square (i,j)(i, j) is occupied by a white pawn, which cannot be moved or removed.

We have put a white bishop on the square (Ax,Ay)(A_x, A_y).
Find the minimum number of moves needed to move this bishop from (Ax,Ay)(A_x, A_y) to (Bx,By)(B_x, B_y) according to the rules of chess (see Notes).
If it cannot be moved to (Bx,By)(B_x, B_y), report -1 instead.

Notes

A white bishop on the square (i,j)(i, j) can go to the following positions in one move.

  • For each positive integer dd, it can go to (i+d,j+d)(i+d,j+d) if all of the conditions are satisfied.

    • The square (i+d,j+d)(i+d,j+d) exists in the board.
    • For every positive integer ldl \le d, (i+l,j+l)(i+l,j+l) is not occupied by a white pawn.
  • For each positive integer dd, it can go to (i+d,jd)(i+d,j-d) if all of the conditions are satisfied.

    • The square (i+d,jd)(i+d,j-d) exists in the board.
    • For every positive integer ldl \le d, (i+l,jl)(i+l,j-l) is not occupied by a white pawn.
  • For each positive integer dd, it can go to (id,j+d)(i-d,j+d) if all of the conditions are satisfied.

    • The square (id,j+d)(i-d,j+d) exists in the board.
    • For every positive integer ldl \le d, (il,j+l)(i-l,j+l) is not occupied by a white pawn.
  • For each positive integer dd, it can go to (id,jd)(i-d,j-d) if all of the conditions are satisfied.

    • The square (id,jd)(i-d,j-d) exists in the board.
    • For every positive integer ldl \le d, (il,jl)(i-l,j-l) is not occupied by a white pawn.

Constraints

  • 2N15002 \le N \le 1500
  • 1Ax,AyN1 \le A_x,A_y \le N
  • 1Bx,ByN1 \le B_x,B_y \le N
  • (Ax,Ay)(Bx,By)(A_x,A_y) \neq (B_x,B_y)
  • SiS_i is a string of length NN consisting of . and #.
  • SAx,Ay=S_{A_x,A_y}= .
  • SBx,By=S_{B_x,B_y}= .

Input

Input is given from Standard Input in the following format:

NN

AxA_x AyA_y

BxB_x ByB_y

S1S_1

S2S_2

\vdots

SNS_N

Output

Print the answer.

Sample Input 1

5
1 3
3 5
....#
...#.
.....
.#...
#....

Sample Output 1

3

We can move the bishop from (1,3)(1,3) to (3,5)(3,5) 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 (3,2)(3,2) to (4,2)(4,2).

Sample Input 3

18
18 1
1 18
..................
.####.............
.#..#..####.......
.####..#..#..####.
.#..#..###...#....
.#..#..#..#..#....
.......####..#....
.............####.
..................
..................
.####.............
....#..#..#.......
.####..#..#..####.
.#.....####..#....
.####.....#..####.
..........#..#..#.
.............####.
..................

Sample Output 3

9

update @ 2024/3/10 10:33:42