POJ 1753 Flip Game (enum) _ enum

Source: Internet
Author: User
Tags rounds
Description

The Flip game is played on a rectangular 4x4 field with two-sided pieces in each of the placed. One side of each piece are white and the other one are black and each piece is lying either it's black or white side up. Each round for you on the flip 3 to 5 pieces, thus changing the color of their upper-side to white and vice versa. The pieces to is flipped are chosen every round according to the following rules:

Choose any one of the pieces.

The Flip of the chosen piece and also all adjacent pieces to the "right" to "the top" and "to" bottom Piece (if there are any).

Consider the following position as an example:

Bwbw
Wwww
Bbwb
Bwwb

Here, "B" denotes pieces lying their black side up and "w" denotes the pieces lying their white side. If we choose to flip the 1st piece from the 3rd row (this choice are shown at the picture), then the field would become:

Bwbw
bWWW
Wwwb
Wwwb

The goal of the game is to flip either all pieces white side up or all pieces black side up. You are are to write a program that would search for the minimum number of rounds needed to achieve this goal.


Input

The input consists of 4 lines with 4 characters "W" or "B", each which denote game field position.


Output

Write to the output file a single integer number-the minimum number of rounds needed to achieve the goal of the game fro M the given position. If The goal is initially achieved, then write 0. If it ' s impossible to achieve the goal, then write the word "impossible" (without quotes).


Sample Input

BWWB
bbwb
bwwb
bwww


Sample Output

4


the

There are 4*4 squares, each lattice is either black, or white, when the color of a lattice is changed (Black-> white | | White-> black), the color of the grid around it (if any) is reversed, asking at least several squares to turn the 4*4 into white or pure.


train of Thought

For each lattice, only two states, flip it once and flip the odd number of times the effect is the same, flip 0 times and flip even several times the effect is the same.

Because there are only 16 squares, choose 0, one, 2 ... 16 All situations have

c016+c116+c216+c316+...+c1516+c1616=216 c_{16}^0+c_{16}^1+c_{16}^2+c_{16}^3+...+c_{16}^{15}+c_{16}^{16}=2^{16}

Enumeration does not time out, so we can simulate 0-16 cycles with recursive thought, representing the number of pieces selected to flip.


AC Code

#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <
Algorithm> #include <stdlib.h> using namespace std;
BOOL Bits[16];

BOOL Flag=false;
    void Rese (int n)//flip {int x=n/4,y=n%4;
    for (int i=max (0,y-1); I<min (y+2,4); i++) bits[x*4+i]=!bits[x*4+i];
    if (x!=0) bits[(x-1) *4+y]=!bits[(x-1) *4+y];
if (x!=3) bits[(x+1) *4+y]=!bits[(x+1) *4+y];
    BOOL Jud ()//judgment is consistent {bool Ini=bits[0];
    for (int i=1; i<16; i++) if (Ini!=bits[i]) return false;
return true;
            } void Solve (int maxx,int now,int step) {if (maxx==step)//Flip the last piece {if (Jud ())//satisfied status {
            Flag=true;
        printf ("%d\n", Maxx);
    } return;                for (int i=now; i<16; i++)//continues {Rese (i) from the last rollover position;
        Flip i Solve (maxx,i+1,step+1);         if (flag) return;               Find the answer and return to Rese (i); Restore original Status} int main () {char str[4][4];
        for (int i=0; i<4; i++) {cin>>str[i];
    for (int j=0; j<4; j + +) bits[i*4+j]= (str[i][j]== ' B ')? 1:0;
    for (int i=0; i<=16; i++)//i The number of pieces to flip solve (i,0,0);
    if (!flag)//No answer is found printf ("impossible\n");
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.