solve_board: Solve a Lights Out board

Description Usage Arguments Details Value See Also Examples

View source: R/solve.R

Description

Given a Lights Out board, find the set of lights that need to be pressed in order to solve the board. If no solution is possible, an error is thrown.

Usage

1
solve_board(board)

Arguments

board

A lightsout board object.

Details

There are a few algorithms for solving Lights Out puzzles. This function implements the Gaussian Elimination technique, which does not guarantee the minimum number of steps. Therefore, some steps in the given solution may be redundant.

If you are interested, there are many resources online outlining the exact details of how this technique works, and what the other solving strategies are.

Value

A matrix with the same dimensions as the input board, with a 1 in every position that requires a press to solve to the board. Note that the order of the light presses does not matter.

See Also

new_board random_board play is_solvable is_solved

Examples

1
2
3
4
5
6
7
8
# Create an empty 5x5 board, press two lights, and then see that the solution
# tells us to press the same lights in order to solve the board.
board <- empty_board(5) %>% play(3, 2) %>% play(4, 1)
board
solution <- solve_board(board)
solution
board <- play(board, matrix = solution)
is_solved(board)

Example output

Lights Out 5x5 board
Game mode: classic 

	0 0 0 0 0
	0 1 0 0 0
	0 1 1 0 0
	1 0 0 0 0
	1 0 0 0 0
	
	0 0 0 0 0
	0 0 0 0 0
	0 1 0 0 0
	1 0 0 0 0
	0 0 0 0 0
	Good job, you won!
[1] TRUE

lightsout documentation built on May 2, 2019, 1:10 p.m.