問題一覧 > 通常問題

No.1572 XI

レベル : / AI推定難易度 : 1325 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 36
作問者 : maguro / テスター : KoD blackyuki PCTprobability
お気に入りにしたユーザー ProblemId : 6283 / 自分の提出
問題文最終更新日: 2021-06-27 13:39:00
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
灘校文化祭コンテスト 2021 day2 (順位表) の他の問題:

Problem Statement

There is a grid divided into squares, with $H$ rows and $W$ columns. The square in the $i$-th row from the top and the $j$-th column from the left is denoted by $(i,j)$.

If $A_{i,j}$ is ., there is nothing on $(i,j)$; if it is #, a cubic block is placed on $(i,j)$.

Now, a die is placed on $(s_y,s_x)$ with the $1$ face on top. The die can be rolled in the $4$ directions (up, down, left, and right), but it cannot be rolled onto a square with a block or outside the grid.

Is it possible to roll the die so that it is on $(g_y,g_x)$ with the $1$ face on top? If it is possible, find the minimum number of rolls.

Input

$H\ W$
$s_y\ s_x$
$g_y\ g_x$
$A_{1,1}\ A_{1,2}\ \cdots \ A_{1,W}$
$A_{2,1}\ A_{2,2}\ \cdots \ A_{2,W}$
$\vdots$
$A_{H,1}\ A_{H,2}\ \cdots \ A_{H,W}$

  • $2 \leq H,W \leq 1000$
  • $1 \leq s_y,g_y \leq H$
  • $1 \leq s_x,g_x \leq W$
  • $(s_y,s_x) \neq (g_y,g_x)$
  • $A_{i,j}$ is either . or #
  • $A_{s_y,s_x},\ A_{g_y,g_x}$ are .

Output

If it is impossible to roll the die so that it is on $(g_y,g_x)$ with the $1$ face on top, print -1.

If it is possible, print the minimum number of rolls needed to get the die on $(g_y,g_x)$ with the $1$ face on top.

Print a newline at the end.

Sample

Sample 1
Input
4 4
1 1
4 4
....
..##
#...
#...
Output
6

It is optimal to roll the die in the order $(1,1),(2,1),(2,2),(3,2),(3,3),(4,3),(4,4)$.

Sample 2
Input
3 2
1 1
3 2
..
..
..
Output
7

It is optimal to roll the die in the order $(1,1),(1,2),(2,2),(3,2),(3,1),(2,1),(2,2),(3,2)$.

Sample 3
Input
4 4
1 1
4 4
....
.#..
...#
.#..
Output
-1

No matter how you roll the die, you cannot get it on $(g_y,g_x)$ with the $1$ face on top.

Sample 4
Input
4 4
1 1
4 4
....
.#..
..##
.#..
Output
-1

The die cannot be rolled to $(g_y,g_x)$ in the first place.

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