#P9169. Optimal Chessboard Piece Game

    ID: 22325 Type: Default 1000ms 256MiB

Optimal Chessboard Piece Game

Optimal Chessboard Piece Game

There is an n×m chessboard. Each cell is denoted by its row and column indices \((i,j)\). Some cells have obstacles, and on the board there is one black piece and two red pieces.

The rules of the game are as follows:

  • The red side moves first and then the black side; the two sides alternate turns.
  • On each turn, the red player can choose one of the red pieces and move it one step into an adjacent cell (up, down, left, or right). Formally, if a red piece is at \((i,j)\), it may move to one of \((i-1,j)\), \((i+1,j)\), \((i,j-1)\), or \((i,j+1)\), provided that the destination cell is within the board, does not contain an obstacle, and does not already contain the other red piece.
  • On its turn, the black player can move its piece one step in one of three directions: up, left, or right. Formally, if the black piece is at \((i,j)\), it may move to \((i-1,j)\), \((i,j-1)\), or \((i,j+1)\), as long as the destination cell is inside the board and does not have an obstacle.

Before a player takes an action, if any one of the following conditions holds, the game terminates immediately and the outcome is decided in the order given below:

  1. If the black piece is located in the first row (i.e. \(i=1\) in 1–indexed notation or \(i=0\) in 0–indexed notation), the black side wins.
  2. If the black piece and one of the red pieces occupy the same cell, then the player who made the previous move (i.e. the mover who just finished his turn) wins.
  3. If the current player has no legal moves, then the opponent wins.

Both players play optimally. In particular, when choosing a move they obey the following tie‑breaking rules:

  • If there exists a winning strategy, the player chooses among all winning moves the one in which the maximum number of moves needed (in the worst‐case continuation) to eventually win is minimized.
  • If no winning strategy exists but there is a strategy that guarantees at least a draw (i.e. non‑losing), the player will choose any such move.
  • If no non‑losing strategy exists, the player will choose among all moves the one that maximizes the minimum number of moves needed for the opponent to win (i.e. delaying defeat as long as possible).

If after \(100^{100^{100}}\) moves no side has won, the game is declared a draw.

The task is to determine the total number of moves made by both players when the game terminates, or to decide that the game ends in a draw.

The board configuration is given as described below.

inputFormat

The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10\)) — the number of rows and columns of the chessboard.

Then follow \(n\) lines, each containing a string of length \(m\). Each character is one of the following:

  • . representing an empty cell;
  • # representing an obstacle;
  • B representing the black piece (exactly one on the board);
  • R representing a red piece (exactly two on the board).

outputFormat

If the game terminates with a win for one side, output a single integer representing the total number of moves made by both players until the game ended. If the game is declared a draw, output the word draw (without quotes).

sample

3 3
.B.
R#R
...
0