Surrounded
Time limit1sMemory limit1024 MB
Mirko moves his piece on a grid to wall in Slavko's hidden piece before it reaches the border, and the task asks for the fewest wall cells needed.
- Level
Hard8 of 10
- Topics
- BFS, Graph, Implementation
- Solved
- No attempts yet
Problem
Mirko and Slavko play a new game on an board. The board has two pieces: Mirko's piece and Slavko's hidden piece. Mirko's piece starts in the upper left corner. Mirko does not know where Slavko's piece starts, but he knows every possible starting position.
Mirko moves first. On his turn, he can make up to 10 steps. One step moves a piece to an adjacent cell. Two cells are adjacent if they share a side. After Mirko, Slavko moves his piece one step or leaves it where it is. Slavko's piece is hidden, so Mirko cannot see where it is or which moves Slavko makes during the game. The two pieces may occupy the same cell. Mirko may step onto a cell he has already visited only in the last step of the whole game. Slavko may visit any cell as many times as he wants. The players take turns until one of them wins.
Slavko wins by moving his piece to the first or last row, or to the first or last column, of the board. Mirko wins by using his piece to enclose the area around Slavko's piece. When Mirko steps onto a cell he already visited in the last step of the game, a wall forms on every cell he occupied between the first and second visits to that cell, including that cell itself. Slavko's piece is enclosed if it lies strictly inside Mirko's wall.
Input
The first line contains two natural numbers and (, ), the number of rows and columns of the board.
Each of the next lines contains characters that describe the board. A dot ('.') is an empty cell. A lowercase 'x' marks a possible starting position of Slavko's piece.
Output
Print the smallest number of cells on which walls must be built so that Mirko is sure to enclose Slavko's piece, regardless of Slavko's starting position and moves. If Mirko cannot guarantee this, print -1.
Hint
Explanation of the first sample: On his first turn, Mirko visits the cells in the order shown in the figure:
01...
.234.
.9x5.
.876.
.....
At the 10th step of his first turn, Mirko steps back onto the cell marked 2. When he steps onto the cell marked 2 again, walls form on 8 cells (the cells marked 2 to 9).
Explanation of the second sample: Mirko cannot enclose Slavko's piece. If Slavko places his piece on the starting position in the second-to-last row, then after Mirko's 10 steps, Slavko moves his piece to the last row and wins.
Explanation of the third sample: One way for Mirko to enclose Slavko's piece with 30 walls is to move in the order shown:
0 1 2 3 4 . . .
29 . . . 5 6 7 .
28 . . . . . 8 9
27 . . x . . . 10
26 . . . x . . 11
25 . . x . . . 12
24 . . . . . . 13
23 22 . . . . . 14
. 21 20 19 18 17 16 15
Mirko reaches cell 0 on his 30th step. Walking those 30 cells takes Mirko 3 turns, so Slavko gets 2 moves before the wall is finished. Slavko is certainly inside the enclosed area within those 2 moves.