TurnOffLights
SRM 288 · 2006-02-08 · by erinn
Problem Statement
You are given a 4x4 game board consisting of buttons which are either lit or unlit. The buttons are numbered 1-16, like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Pressing a button changes the state of that button, along with the states of the buttons to the immediate left, right, top and bottom. Pressing and holding a button, which counts as two moves, changes only the state of that individual light.
The goal of the game is to turn off all of the lights. You are given a
Constraints
- board will contain exactly 4 elements.
- Each element of board will contain exactly 4 characters.
- Each character of each element of board will be '0' (zero) or '1' (one).
{"1100",
"1000",
"0000",
"0000"}
Returns: 1
Press only the button in the upper left corner.
{"0100",
"1110",
"0100",
"0000"}
Returns: 1
Again, a single button press suffices.
{"1000",
"0000",
"0000",
"0000"}
Returns: 2
We need to press and hold the lit button, which costs us two moves.
{"1100",
"1000",
"0000",
"0001"}
Returns: 3
Press the upper left button (costs 1). Press and hold the lower right button (costs 2).
{"1011",
"1010",
"0000",
"0000"}
Returns: 2
Here, we push buttons 1 and 3. Notice that button 2 lights up after the first button push, but ends up unlit at the end.
Submissions are judged against all 45 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class TurnOffLights with a public method int fewestMoves(vector<string> board) · 45 test cases · 2 s / 256 MB per case