1813. Peg Jump Puzzle
A solitaire board is a grid board of characters: '#' is a wall, 'o' is a cell holding a peg and '.' is an empty hole. A move picks a peg and jumps it over an orthogonally adjacent peg, landing in the hole exactly two cells away in the same direction; the jumped peg is removed. Walls can never be jumped over or landed on, and jumps never leave the board. You may make any number of moves, in any order, or none at all.
Return the smallest number of pegs that can remain on the board after some sequence of moves. The board has at most 16 cells that are not walls, so every position of the pegs fits in a bitmask of those cells. Search the positions reachable from the start, remembering each visited position so it is expanded only once. Time is O(2^m * m) and space O(2^m), where m is the number of non-wall cells.
Example 1
- Input:
- board = [["o","o","."]]
- Output:
- 1
- Explanation:
The left peg jumps over its neighbour into the hole, removing it, so one peg remains.
Example 2
- Input:
- board = [["o",".","o"]]
- Output:
- 2
- Explanation:
Neither peg has an adjacent peg to jump over, so both stay.
Example 3
- Input:
- board = [["o","o","o"],["o","o","o"],["o","o","."]]
- Output:
- 4
- Explanation:
With one corner hole and eight pegs, the best sequence of jumps leaves 4 pegs on the board.
Constraints
1 ≤ rows, cols ≤ 7
board[i][j] is one of '#', 'o', '.'
At most 16 cells are not '#'; at least one cell is 'o'
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 6,000 msC++ 1,500 msJava 3,000 msJavaScript 3,000 msTypeScript 3,000 ms
Expected complexity
- Time
- O(2^m * m)
- Space
- O(2^m)