POJ1753 Flip Game (main topic + understanding)

Source: Internet
Author: User
Tags printf rounds time limit

Flip Game
Time limit:1000ms Memory limit:65536k
Total submissions:40249 accepted:17477

Description
Flip game is played on a rectangular 4×4 field with two-sided pieces placed on each of its squares. 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 your flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped is chosen every round according to the following rules:

Choose any one of the pieces.
Flip The chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen Piece (if there is any).

Consider the following position as an example:

Bwbw
Wwww
Bbwb
Bwwb
Here ' B ' denotes pieces lying their black side up and ' w ' denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice was shown at the picture) and 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 is 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" from each of the 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 and then write 0. If it's impossible to achieve the goal and then write the word "impossible" (without quotes).

Sample Input

Bwwb
Bbwb
Bwwb
bWWW

Sample Output

4
The main idea: to flip the pieces, and finally make the same color, each flip one, you can reverse the number of times is 0-16, because 4*4 matrix, each piece has the possibility of flipping, in fact, the inverse even several times and odd number of times two possible, such as 2 and 4 times the result is the same, because 3 will turn the original back, the minimum

#include <stdio.h> #include <string.h> int step;
Char a[6][6];//in order to not cross the line, set to 6 rows 6 columns, the actual need only intermediate 1-4 bool flag; int R[]={-1,1,0,0,0};//5 direction int c[]={0,0,-1,1,0}; bool Judge () {for (int i=1;i<5;i++) {for (int j=1;j&l
            t;5;j++) {if (a[i][j]!=a[1][1])//different {return false;    
}}} return true; } void Flip (int row,int column) {for (int i=0;i<5;i++) {A[row+r[i]][column+c[i]]=!a[row+r[i]][column+c [i]];/
/Flip Sing woo original opposite color} return;
        } void Dfs (int row,int column,int deep)//deep is the number of steps to walk {if (deep==step) {Flag=judge ();//Successful exit, find the whole state of the same
    Return } if (Flag| |
    ROW==5)//Unsatisfied failed exit {return;
    } flip (Row,column);//Flip if (column<4) DFS (row,column+1,deep+1);
    else Dfs (row+1,1,deep+1);
    Flip (row,column);//Do not meet to return it to if (column<4) DFS (row,column+1,deep);
    else Dfs (ROW+1,1,DEEP); RetuRn 
    } int main () {char C; for (int i=1;i<5;i++) {for (int j=1;j<5;j++) {C=getchar ();//input character if (c== '
    B ') {a[i][j]=1;//black full 1}} getchar (); } for (step=0;step<=16;step++)//Can flip the number of blocks {DFS (1,1,0);//pass the coordinates over if (flag) {bre
        Ak
    }} if (flag) {printf ("%d\n", step);
    } else {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.