問題一覧 > 通常問題

No.1323 Ushirazu Swap

レベル : / AI推定難易度 : 2450 / 実行時間制限 : 1ケース 3.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 25
作問者 : ei1333333 / テスター : Luzhiled
お気に入りにしたユーザー ProblemId : 4176 / 自分の提出
問題文最終更新日: 2020-12-20 01:08:02
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2020 (順位表) の他の問題:

Problem Statement

There is a grid with $H$ rows and $W$ columns. The cell in the $r \ (1 \leq r \leq H)$-th row from the top and the $c\ (1 \leq c \leq W)$-th column from the left is denoted by $(r, c)$.

The state of the grid is represented by a two-dimensional array $S$. If $S_{r,c}$ is #, there is an obstacle in cell $(r, c)$, and if it is ., there is no obstacle. It is guaranteed that the outer boundary of the grid is surrounded by obstacles #.

Ushi-san and Hikari-chan, who are on cells of the grid, are trying to swap their positions.

Initially, Ushi-san is in cell $(r_a, c_a)$ and Hikari-chan is in cell $(r_b, c_b)$. Each of the $2$ people can move only to a cell that has no obstacle and no one in it, among the $4$ cells sharing an edge with the cell they are currently in. The $2$ people may move in any order, but the $2$ of them cannot move to different cells at the same time. In the end, Ushi-san should be in cell $(r_b, c_b)$ and Hikari-chan should be in cell $(r_a, c_a)$.

Determine whether the positions can be swapped, and if they can, find the minimum total number of moves made by the $2$ people to swap their positions.

Input

$H$ $W$ $r_a$ $c_a$ $r_b$ $c_b$
$S_{1,1} S_{1,2} \cdots S_{1,W}$
$S_{2,1} S_{2,2} \cdots S_{2,W}$
$:$
$S_{H,1} S_{H,2} \cdots S_{H,W}$
  • $4 \leq H, W \leq 1333$
  • $1 \lt r_a, r_b \lt H$
  • $1 \lt c_a, c_b \lt W$
  • $(r_a, c_a) \neq (r_b, c_b)$
  • $S_{r,c}$ $(1 \leq r \leq H, 1 \leq c \leq W)$ is # or .
  • $S_{r_a, c_a}, S_{r_b, c_b}$ are .
  • The outer boundary of the grid (row $1$, row $H$, column $1$, column $W$) is #
  • $H, W, r_a, c_a, r_b, c_b$ are integers

Output

Output, on $1$ line, the minimum total number of moves made by the $2$ people to swap their positions. However, if the positions cannot be swapped, output $-1$.

Sample

Sample 1
Input
4 5 2 2 3 4
#####
#...#
##..#
#####
Output
6

For example, they can move as follows.

  1. Ushi-san moves: $(2, 2) \Rightarrow (2, 3)$
  2. Ushi-san moves: $(2, 3) \Rightarrow (2, 4)$
  3. Hikari-chan moves: $(3, 4) \Rightarrow (3, 3)$
  4. Ushi-san moves: $(2, 4) \Rightarrow (3, 4)$
  5. Hikari-chan moves: $(3, 3) \Rightarrow (2, 3)$
  6. Hikari-chan moves: $(2, 3) \Rightarrow (2, 2)$
Sample 2
Input
4 5 2 2 2 4
#####
#.#.#
#...#
#####
Output
-1

The two of them cannot stay in the same cell at the same time.

Sample 3
Input
5 4 4 3 2 3
####
#..#
#..#
#..#
####
Output
6

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。