#P6331. Peg Solitaire Maximum Moves
Peg Solitaire Maximum Moves
Peg Solitaire Maximum Moves
Given a \(7\times7\) matrix representing a peg solitaire board, determine the maximum number of moves that can be performed. The board is represented by 7 lines of 7 characters each. In the first two rows and the last two rows, the first two columns are always blank (i.e. a space character). In the remaining positions, the character o
indicates the presence of a peg, and .
indicates an empty (playable) cell.
A move is defined as follows: if there is a peg at a cell and there is an adjacent peg in one of the four directions (up, down, left, or right), and the cell immediately beyond that peg in the same direction is empty, then the peg can jump over the adjacent peg into the empty cell. The peg that is jumped over is removed from the board. This constitutes a single move.
Your task is to compute the maximum number of moves that can be executed sequentially starting from the given configuration.
In mathematical terms, let the board be represented by a matrix \(B\) of size \(7\times7\). For a peg at \(B_{i,j}\) (o
), a move in direction \((dx,dy)\) is allowed if:
[ \begin{cases} B_{i+dx,j+dy} = o,\ B_{i+2dx,j+2dy} = . \end{cases} ]
After a move, update the board by setting \(B_{i,j}\) and \(B_{i+dx,j+dy}\) to .
and \(B_{i+2dx,j+2dy}\) to o
. Determine the maximum number of such moves that can be made.
inputFormat
The input consists of 7 lines, each containing 7 characters. In the first two and last two rows, the first two columns are spaces. The other characters are either o
(representing a peg) or .
(representing an empty cell).
outputFormat
Output a single integer: the maximum number of moves that can be performed.
sample
.....
.....
.......
...o...
.......
.....
.....
0