Gamified challenge · Bitmask search
grid[r][c] is 1 if the light at row r, column c is on and 0 if it's off. Pressing a cell toggles that light and its up to four neighbours (up, down, left, right); lights off the edge are ignored. Grids are at most 5 × 5, and every puzzle can be solved.
Write presses(grid) that returns a list of presses [row, col] which, applied in order, switch every light off. For [[0,1,0],[1,1,1],[0,1,0]], [[1, 1]] works. Pressing a cell twice cancels out, and order doesn't matter, so any set of presses that ends with all lights off passes. A dark grid needs [].
Hint: once you've decided the top row, every other press is forced.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google