/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は N \times M のグリッド状の迷宮に閉じ込められています。グリッドの各マスは通路(.)または壁(#)のいずれかです。マスは (r, c)(r 行目、c 列目)で表され、行・列はともに 1 から始まります。行番号は上から下へ、列番号は左から右へ増加するものとします。すなわち、マス (r, c) から見て「上」はマス (r-1, c)、「下」はマス (r+1, c)、「左」はマス (r, c-1)、「右」はマス (r, c+1) を指します。
高橋君は現在マス (S_r, S_c) におり、出口であるマス (G_r, G_c) を目指して脱出を試みています。
迷宮内には K 体の警備ロボットが巡回しており、i 番目の警備ロボットの初期位置はマス (P_i, Q_i) です。異なる警備ロボットが同じマスに初期配置されることもありえます。
各警備ロボットの移動は、後述する移動ルールに従って高橋君の位置のみに基づき決定的に定まります。高橋君は迷宮の構造および警備ロボットの移動ルールをすべて把握しています。
高橋君と警備ロボットは以下の手順でターンを繰り返します。最初のターンをターン 1 とし、以降ターン 2, 3, \ldots と続きます。ゲーム開始時点(ターン 1 の手順 1 の前)では捕獲判定は行いません。各ターンは次の順序で進行します:
- 高橋君の移動: 高橋君は、上下左右に隣接する通路のマスに 1 マス移動するか、現在のマスにとどまるかのいずれかを選びます。壁のマスやグリッドの外に移動することはできません。現在のマスにとどまることは常に選択可能です。
- 捕獲判定 1: 高橋君の移動後、高橋君がいずれかの警備ロボットと同じマスにいる場合、高橋君は捕まり脱出失敗となります。この場合、このターンの残りの手順は実行されません。
- 脱出判定: 捕獲判定 1 で捕まらなかった場合、高橋君が出口 (G_r, G_c) にいるならば脱出成功となります。この場合、このターンの残りの手順は実行されません。
- 警備ロボットの移動: 脱出成功も脱出失敗も発生していない場合、すべての警備ロボットが同時に 1 ステップ移動します(移動ルールは後述)。各ロボットは他のロボットの動きとは独立に移動先を決定します。複数のロボットが同じマスに存在することも許されます。
- 捕獲判定 2: 警備ロボットの移動後、高橋君がいずれかの警備ロボットと同じマスにいる場合、高橋君は捕まり脱出失敗となります。
脱出成功または脱出失敗が発生するまで、このターンを繰り返します。
警備ロボットの移動ルール
各警備ロボットは、上・下・左・右に隣接する通路のマスに 1 マス移動するか、現在のマスにとどまるかのいずれかを行います。壁のマスやグリッドの外には移動できません。
各警備ロボットの移動先は次のように決定されます。まず、移動先の候補を列挙します。候補とは、上・下・左・右への移動のうち移動先がグリッド内かつ通路であるもの、および現在のマスにとどまるもの(最大 5 通り)です。
各候補について、移動後のそのロボットの位置から、高橋君のそのターンでの移動後の位置(手順 1 の後の位置)までのマンハッタン距離を計算します。ここでマンハッタン距離とは、マス (r_1, c_1) とマス (r_2, c_2) の間の距離 |r_1 - r_2| + |c_1 - c_2| のことであり、間に壁があるかどうかは考慮しません。
候補の中から、このマンハッタン距離が最小となるものを選びます。マンハッタン距離が最小となる候補が複数ある場合は、上・下・左・右・とどまる の優先順位(この順に優先度が高い)で最も優先度が高いものを選択します。
出力すべきもの
高橋君が最適に行動するとき、脱出に成功できるかどうかを判定してください。ここで「最適に行動する」とは、脱出成功が可能な行動の選び方が存在する場合にそれを選び、さらにその中で脱出成功となるターン番号が最小となるように行動することを意味します。
脱出に成功できる場合は、脱出成功が確定する最小のターン番号を出力してください。
高橋君がどのように行動しても脱出成功に至らない場合(出口に到達する前に必ず捕まってしまう場合や、捕まることは回避できるが出口に永遠に到達できない場合など)は -1 を出力してください。
制約
- 1 \leq N \leq 8
- 1 \leq M \leq 8
- 0 \leq K \leq 2
- 1 \leq S_r \leq N
- 1 \leq S_c \leq M
- 1 \leq G_r \leq N
- 1 \leq G_c \leq M
- 1 \leq P_i \leq N (1 \leq i \leq K)
- 1 \leq Q_i \leq M (1 \leq i \leq K)
- (S_r, S_c) \neq (G_r, G_c)
- 高橋君の初期位置、出口、各警備ロボットの初期位置はすべて通路(
.)である。 - 高橋君の初期位置はどの警備ロボットの初期位置とも異なる。
- 入力はすべて整数である(グリッドの文字列を除く)。
入力
N M K S_r S_c G_r G_c P_1 Q_1 P_2 Q_2 \vdots P_K Q_K C_1 C_2 \vdots C_N
- 1 行目には、グリッドの行数 N、列数 M、警備ロボットの数 K がスペース区切りで与えられる。
- 2 行目には、高橋君の初期位置の行 S_r と列 S_c がスペース区切りで与えられる。
- 3 行目には、出口の行 G_r と列 G_c がスペース区切りで与えられる。
- 続く K 行にわたって、警備ロボットの初期位置が与えられる。K = 0 の場合、この部分は 0 行となり存在しない。
- i 番目 (1 \leq i \leq K) の警備ロボットについて、初期位置の行 P_i と列 Q_i がスペース区切りで与えられる。
- 続く N 行には、グリッドの各行を表す長さ M の文字列 C_i が与えられる。各文字は
.(通路)または#(壁)である。 - C_i は上から i 行目 (1 \leq i \leq N) のグリッドの情報を表す。C_i の j 文字目がマス (i, j) の状態に対応する。
出力
高橋君が最適に行動したとき脱出に成功できる場合は、脱出成功となる最小のターン番号を 1 行で出力してください。成功できない場合は -1 を出力してください。
入力例 1
3 3 0 1 1 1 3 ... ... ...
出力例 1
2
入力例 2
3 1 1 1 1 3 1 2 1 . . .
出力例 2
-1
入力例 3
5 5 1 1 1 5 5 3 3 ..... .###. .#... .#.#. .....
出力例 3
16
入力例 4
8 8 2 1 1 8 8 1 8 8 1 ........ .######. .#....#. .#.##.#. .#.##.#. .#....#. .######. ........
出力例 4
-1
入力例 5
1 2 0 1 1 1 2 ..
出力例 5
1
Score : 400 pts
Problem Statement
Takahashi is trapped in an N \times M grid-shaped labyrinth. Each cell of the grid is either a passage (.) or a wall (#). A cell is denoted by (r, c) (row r, column c), where both rows and columns are numbered starting from 1. Row numbers increase from top to bottom, and column numbers increase from left to right. That is, from cell (r, c), "up" refers to cell (r-1, c), "down" refers to cell (r+1, c), "left" refers to cell (r, c-1), and "right" refers to cell (r, c+1).
Takahashi is currently at cell (S_r, S_c) and is trying to escape by reaching the exit at cell (G_r, G_c).
There are K guard robots patrolling the labyrinth, and the i-th guard robot's initial position is cell (P_i, Q_i). Different guard robots may be initially placed on the same cell.
Each guard robot's movement is deterministically determined based solely on Takahashi's position, according to the movement rules described below. Takahashi knows the entire structure of the labyrinth and all the movement rules of the guard robots.
Takahashi and the guard robots repeat turns as follows. The first turn is turn 1, followed by turns 2, 3, \ldots. No capture check is performed at the start of the game (before step 1 of turn 1). Each turn proceeds in the following order:
- Takahashi's movement: Takahashi chooses to either move 1 cell to an adjacent passage cell in one of the four directions (up, down, left, right), or stay on his current cell. He cannot move to a wall cell or outside the grid. Staying on the current cell is always an available choice.
- Capture check 1: After Takahashi's movement, if Takahashi is on the same cell as any guard robot, Takahashi is caught and escape fails. In this case, the remaining steps of this turn are not executed.
- Escape check: If Takahashi was not caught in capture check 1, and Takahashi is at the exit (G_r, G_c), then escape succeeds. In this case, the remaining steps of this turn are not executed.
- Guard robots' movement: If neither escape success nor escape failure has occurred, all guard robots simultaneously move 1 step (movement rules described below). Each robot determines its destination independently of the other robots. Multiple robots are allowed to occupy the same cell.
- Capture check 2: After the guard robots' movement, if Takahashi is on the same cell as any guard robot, Takahashi is caught and escape fails.
These turns repeat until either escape success or escape failure occurs.
Guard Robot Movement Rules
Each guard robot either moves 1 cell to an adjacent passage cell in one of the four directions (up, down, left, right), or stays on its current cell. It cannot move to a wall cell or outside the grid.
Each guard robot's destination is determined as follows. First, enumerate the movement candidates. The candidates are: moving up, down, left, or right where the destination is within the grid and is a passage, as well as staying on the current cell (at most 5 candidates).
For each candidate, compute the Manhattan distance from the robot's position after the move to Takahashi's position after his movement in that turn (his position after step 1). Here, the Manhattan distance between cells (r_1, c_1) and (r_2, c_2) is |r_1 - r_2| + |c_1 - c_2|, regardless of whether there are walls in between.
Among the candidates, the one that minimizes this Manhattan distance is selected. If there are multiple candidates with the minimum Manhattan distance, the one with the highest priority is selected according to the priority order: up, down, left, right, stay (listed from highest to lowest priority).
What to Output
Determine whether Takahashi can successfully escape when he acts optimally. Here, "acting optimally" means that if there exists a sequence of actions that leads to escape success, he chooses such a sequence, and among those, he acts to minimize the turn number at which escape succeeds.
If escape is possible, output the minimum turn number at which escape succeeds.
If Takahashi cannot escape no matter how he acts (e.g., he is inevitably caught before reaching the exit, or he can avoid being caught but can never reach the exit), output -1.
Constraints
- 1 \leq N \leq 8
- 1 \leq M \leq 8
- 0 \leq K \leq 2
- 1 \leq S_r \leq N
- 1 \leq S_c \leq M
- 1 \leq G_r \leq N
- 1 \leq G_c \leq M
- 1 \leq P_i \leq N (1 \leq i \leq K)
- 1 \leq Q_i \leq M (1 \leq i \leq K)
- (S_r, S_c) \neq (G_r, G_c)
- Takahashi's initial position, the exit, and each guard robot's initial position are all passages (
.). - Takahashi's initial position is different from the initial position of every guard robot.
- All input values are integers (except for the grid strings).
Input
N M K S_r S_c G_r G_c P_1 Q_1 P_2 Q_2 \vdots P_K Q_K C_1 C_2 \vdots C_N
- The first line contains the number of rows N, the number of columns M, and the number of guard robots K, separated by spaces.
- The second line contains Takahashi's initial row S_r and column S_c, separated by a space.
- The third line contains the exit's row G_r and column G_c, separated by a space.
- The following K lines give the initial positions of the guard robots. If K = 0, this part consists of 0 lines and does not exist.
- For the i-th (1 \leq i \leq K) guard robot, its initial row P_i and column Q_i are given, separated by a space.
- The following N lines contain strings C_i of length M representing each row of the grid. Each character is either
.(passage) or#(wall). - C_i represents the information of the i-th row (1 \leq i \leq N) from the top. The j-th character of C_i corresponds to the state of cell (i, j).
Output
If Takahashi can successfully escape when acting optimally, output the minimum turn number at which escape succeeds, on a single line. If he cannot succeed, output -1.
Sample Input 1
3 3 0 1 1 1 3 ... ... ...
Sample Output 1
2
Sample Input 2
3 1 1 1 1 3 1 2 1 . . .
Sample Output 2
-1
Sample Input 3
5 5 1 1 1 5 5 3 3 ..... .###. .#... .#.#. .....
Sample Output 3
16
Sample Input 4
8 8 2 1 1 8 8 1 8 8 1 ........ .######. .#....#. .#.##.#. .#.##.#. .#....#. .######. ........
Sample Output 4
-1
Sample Input 5
1 2 0 1 1 1 2 ..
Sample Output 5
1