Fewest Presses to Clear the Board
board is a small grid of lamps: 1 is on and 0 is off. A press on a cell flips the lamp of that cell and the lamps of the cells directly above, below, left and right of it, where they exist.
Return the fewest presses that switch every lamp off, or -1 if no presses can do that.
Example 1
board [[1,1,0],[1,0,0]]
returns 1
A press on the top left cell flips exactly the three lamps that are on.
Example 2
board [[1,0,0]]
returns 2
Press the middle cell and then the right one: 100 becomes 011 and then 000.
Example 3
board [[1,0]]
returns -1
Each of the two cells flips both lamps, so the lamps always match each other.
Constraints
1 <= len(board) * len(board[0]) <= 12, and every row has the same lengthboard[r][c]is 0 or 1
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.