Statistics

Problem Statement for "TurnOffLights"

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 String[] board consisting of four elements, each containing four characters. Each character will be a '0' or '1', indicating whether each light is off or on, respectively. You are to return an int indicating the least number of moves necessary to turn off all of the lights.

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

  1. {"1100", "1000", "0000", "0000"}

    Returns: 1

    Press only the button in the upper left corner.

  2. {"0100", "1110", "0100", "0000"}

    Returns: 1

    Again, a single button press suffices.

  3. {"1000", "0000", "0000", "0000"}

    Returns: 2

    We need to press and hold the lit button, which costs us two moves.

  4. {"1100", "1000", "0000", "0001"}

    Returns: 3

    Press the upper left button (costs 1). Press and hold the lower right button (costs 2).

  5. {"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.

  6. {"0010","0111","0110","0111"}

    Returns: 6

  7. {"1111","0010","0001","0100"}

    Returns: 5

  8. {"1001","0110","0110","1001"}

    Returns: 4

  9. {"1010","0111","1010","0110"}

    Returns: 6

  10. {"1101","1101","0100","0100"}

    Returns: 4

  11. {"0110","1100","0001","0111"}

    Returns: 7

  12. {"0011","1001","0110","0011"}

    Returns: 4

  13. {"1000","1111","0010","0011"}

    Returns: 7

  14. {"1100","0001","0111","0101"}

    Returns: 4

  15. {"0100","1111","1110","0101"}

    Returns: 7

  16. {"0110","0000","1100","0100"}

    Returns: 4

  17. {"0110","1100","0100","0001"}

    Returns: 6

  18. {"1110","0001","0000","0100"}

    Returns: 4

  19. {"0001","1010","0101","1101"}

    Returns: 7

  20. {"0101","0101","1001","0100"}

    Returns: 6

  21. {"0110","1001","1001","0110"}

    Returns: 4

  22. {"0000","0110","0110","0000"}

    Returns: 6

  23. {"0110","1111","1111","0110"}

    Returns: 4

  24. {"0110","0111","1111","1100"}

    Returns: 7

  25. {"1110","0011","1101","1101"}

    Returns: 7

  26. {"1010","1101","0110","1101"}

    Returns: 9

  27. {"0000","1111","1001","1111"}

    Returns: 9

  28. {"1011", "1101", "0100", "1010" }

    Returns: 6

  29. {"0101", "1101", "1011", "1001" }

    Returns: 6

  30. {"1111", "1111", "1111", "1111" }

    Returns: 4

  31. {"1011", "0100", "0101", "1010" }

    Returns: 7

  32. {"1111", "1100", "0011", "1111" }

    Returns: 8

  33. {"1010", "0101", "1010", "0101" }

    Returns: 8

  34. {"0101", "1010", "0101", "1010" }

    Returns: 8

  35. {"0100", "1000", "1101", "0110" }

    Returns: 3

  36. {"1001", "0000", "0000", "1001" }

    Returns: 6

  37. {"1011", "1010", "0000", "0000" }

    Returns: 2

  38. {"0001", "0101", "1111", "1011" }

    Returns: 7

  39. {"1111", "1011", "0110", "1101" }

    Returns: 7

  40. {"1010", "1100", "0101", "0011" }

    Returns: 7

  41. {"0100", "1010", "0100", "0000" }

    Returns: 3

  42. {"1111", "1011", "1101", "1111" }

    Returns: 8

  43. {"0110", "1001", "0110", "0000" }

    Returns: 2

  44. {"1111", "1101", "1011", "1111" }

    Returns: 8

  45. {"0100", "1000", "0100", "0000" }

    Returns: 5


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: