Connection Status:
Competition Arena > FlipFlop
SRM 142 · 2003-04-15 · by TangentZ
Class Name: FlipFlop
Return Type: int
Method Name: minMoves
Arg Types: (vector<string>)
Problem Statement

Problem Statement

Flip-Flop is a puzzle played with a m by n grid of cells. Each cell has a colored peg, which can be one of three colors: Red, Green, or Blue. Here are some examples of what the grid may look like ('R' for Red, 'G' for Green, 'B' for Blue):

  • RRGGB      RGBRGB      RRR
    RRRGG      GBRGBR      GGG
    GRRRR      BRGBRG      BBB
    GGRRR                  
    BGGRR                  
    

A color-switch is defined as changing the peg from one color to another. Red switches to Green. Green switches to Blue. Blue switches to Red.

A move is made by first choosing one of the pegs and doing a color-switch on it (the flip). Then, its (up to) four neighboring pegs are color-switched (the flop). Diagonal neighbors do not count, only vertical and horizontal neighbors do. Edges do not wrap around.

For example (the coordinates are represented as a (row, col) pair):

  12345         12345         12345
1 BBBGG       1 BBBBB       1 BBBBB
2 BBBBG       2 BBBBB       2 BBBBB
3 BBBBB  -->  3 BBBBB  -->  3 BBBBB
4 GBBBB       4 GBBBB       4 BBBBB
5 GGBBB       5 GGBBB       5 BBBBB

Move 1     |  Move 2
-----------+------------
flip(1,5)  |  flip(5,1)
flop(1,4)  |  flop(4,1)
flop(2,5)  |  flop(5,2)

Another example:

  12345         12345         12345         12345         12345
1 BRBBG       1 BGBBG       1 BBBBG       1 BBBBG       1 BBBBB
2 RRRGG       2 GGGGG       2 BBBGG       2 BBBGG       2 BBBBB
3 BRGBG  -->  3 BGGBG  -->  3 BBGBG  -->  3 BBBBG  -->  3 BBBBB
4 BGGGB       4 BGGGB       4 BGGGB       4 BBBBB       4 BBBBB
5 BBGBB       5 BBGBB       5 BBGBB       5 BBBBB       5 BBBBB
6 BBBBB       6 BBBBB       6 BBBBB       6 BBBBB       6 BBBBB

Move 1     |  Move 2     |  Move 3     |  Move 4
-----------+-------------+-------------+------------
flip(2,2)  |  flip(2,2)  |  flip(4,3)  |  flip(2,5)
flop(1,2)  |  flop(1,2)  |  flop(3,3)  |  flop(1,5)
flop(2,1)  |  flop(2,1)  |  flop(4,2)  |  flop(2,4)
flop(2,3)  |  flop(2,3)  |  flop(4,4)  |  flop(3,5)
flop(3,2)  |  flop(3,2)  |  flop(5,3)  |

The object of Flip-Flop is to start with some initial board position and reach the board with all Blue pegs by making a series of moves. Some initial positions are solvable. But for others, it is impossible. For each solvable initial position, there exists a minimum number of moves required to solve the puzzle.

You are given a String[], board, as the initial board position. Each element of board represents a row of the initial position by the colors of the pegs, with Element 0 as the first row, Element 1 as the second row, and so on. Your task is to write a function that returns the minimum number of moves to solve the puzzle. If the given initial position is unsolvable, return -1.

Constraints

  • board contains between 2 and 10 elements, inclusive
  • board contains only the uppercase letters 'R', 'G', 'B'
  • Each element of board contains between 2 and 10 characters
  • All elements of board have the same length
Examples
0)
{"BBBGG",
 "BBBBG",
 "BBBBB",
 "GBBBB",
 "GGBBB"}
Returns: 2

This is the same as the first example. It requires a minimum of 2 moves.

1)
{"BRBBG",
 "RRRGG",
 "BRGBG",
 "BGGGB",
 "BBGBB",
 "BBBBB"}
Returns: 4

This is the same as the second example. It requires a minimum of 4 moves.

2)
{"RRG",
 "RGG"}
Returns: 3

Flip the top-left corner twice and the bottom-right corner once.

3)
{"RRRBRGGB",
 "BRBGGBGR",
 "RBGBBGRG"}
Returns: -1

This position is unsolvable.

4)
{"BBBBBB",
 "BGGGGB",
 "BGRRGB",
 "BGRRGB",
 "BGGGGB",
 "BBBBBB"}
Returns: 32
6)
{"BBBBBBBBBB",
 "BGGGGGGGGB",
 "BGBBBBBGBB",
 "BBBRBBGBBB",
 "BBBBRGBBBB",
 "BBBBGRBBBB",
 "BBBGBBRBBB",
 "BBGBBBBBGB",
 "BGGGGGGGGB",
 "BBBBBBBBBB"}
Returns: 104

Watch for timeout!

Submissions are judged against all 61 archived test cases, of which 6 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class FlipFlop with a public method int minMoves(vector<string> board) · 61 test cases · 2 s / 256 MB per case

Submitting as anonymous