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
Definition
- Class:
- TurnOffLights
- Method:
- fewestMoves
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int fewestMoves(String[] board)
- (be sure your method is public)
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).
Examples
{"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.
{"0010","0111","0110","0111"}
Returns: 6
{"1111","0010","0001","0100"}
Returns: 5
{"1001","0110","0110","1001"}
Returns: 4
{"1010","0111","1010","0110"}
Returns: 6
{"1101","1101","0100","0100"}
Returns: 4
{"0110","1100","0001","0111"}
Returns: 7
{"0011","1001","0110","0011"}
Returns: 4
{"1000","1111","0010","0011"}
Returns: 7
{"1100","0001","0111","0101"}
Returns: 4
{"0100","1111","1110","0101"}
Returns: 7
{"0110","0000","1100","0100"}
Returns: 4
{"0110","1100","0100","0001"}
Returns: 6
{"1110","0001","0000","0100"}
Returns: 4
{"0001","1010","0101","1101"}
Returns: 7
{"0101","0101","1001","0100"}
Returns: 6
{"0110","1001","1001","0110"}
Returns: 4
{"0000","0110","0110","0000"}
Returns: 6
{"0110","1111","1111","0110"}
Returns: 4
{"0110","0111","1111","1100"}
Returns: 7
{"1110","0011","1101","1101"}
Returns: 7
{"1010","1101","0110","1101"}
Returns: 9
{"0000","1111","1001","1111"}
Returns: 9
{"1011", "1101", "0100", "1010" }
Returns: 6
{"0101", "1101", "1011", "1001" }
Returns: 6
{"1111", "1111", "1111", "1111" }
Returns: 4
{"1011", "0100", "0101", "1010" }
Returns: 7
{"1111", "1100", "0011", "1111" }
Returns: 8
{"1010", "0101", "1010", "0101" }
Returns: 8
{"0101", "1010", "0101", "1010" }
Returns: 8
{"0100", "1000", "1101", "0110" }
Returns: 3
{"1001", "0000", "0000", "1001" }
Returns: 6
{"1011", "1010", "0000", "0000" }
Returns: 2
{"0001", "0101", "1111", "1011" }
Returns: 7
{"1111", "1011", "0110", "1101" }
Returns: 7
{"1010", "1100", "0101", "0011" }
Returns: 7
{"0100", "1010", "0100", "0000" }
Returns: 3
{"1111", "1011", "1101", "1111" }
Returns: 8
{"0110", "1001", "0110", "0000" }
Returns: 2
{"1111", "1101", "1011", "1111" }
Returns: 8
{"0100", "1000", "0100", "0000" }
Returns: 5