Poj1753flip Game (DFS + enumeration)

Source: Internet
Author: User
Tags rounds

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 37050 Accepted: 16122

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:
    1. Choose any one of the pieces.
    2. 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

Bwwbbbwbbwwbbwww

Sample Output

4

No idea,-_-.

Reprint Please specify Source: http://user.qzone.qq.com/289065406/blog/1299076400

Tip: Flip chess, can be modeled as a multi-fork tree

There are two difficult points, one is not to all black (or all white) as the goal of the search, but to the whole black (or all white) as "root", to search the leaves, to see if there is an input checkerboard state.

Another difficulty requires a little bit of math, that is, to know the maximum height of the tree, which is the "state does not exist" standard of judgment

tip: In fact, each piece can only be flipped one time (actually odd number of times, but it doesn't make sense), as long as one of the squares is repeated over 2 Times ( Whether it is a continuous or non-connected flip ) , then it is the same as the surrounding pieces and the state is not flipped, so you can determine the chess The disk can only go at most - can only be turned out at most 2^16 kind of State

Actually also thought of Dfs, can not find the termination status, yes, is 16 OK, because if each state is flipped over, then you are flipping a card, then and he did not flip is the same, so no need to

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;BOOLchess[Ten][Ten];intStep,flag;intr[4] = {-1,1,0,0};intc[4] = {0,0,-1,1};intJudge_all () { for(inti =1; I <=4; i++)    {         for(intj =1; J <=4; J + +)        {            if(Chess[i][j]! = chess[1][1])                return false; }    }    return true;}voidFlipintRowintCol) {Chess[row][col]= !Chess[row][col];  for(inti =0; I <4; i++)    {        intFX = row +R[i]; intFY = col +C[i]; if(FX >0&& FX <=4&& fy >0&& FY <=4) Chess[fx][fy]= !Chess[fx][fy]; }}voidDfsintRowintColintDeep ) {    if(Deep = = Step)//reached the specified number of flips, judging whether they are the same, and then returning{flag=Judge_all (); return; }    if(Flag | | row >4)        return;    Flip (row, col); if(Col <4) {dfs (row, col+1, Deep +1);//If this line can also be flipped, determine the next    }    Else{dfs (row+1,1, Deep +1);   } Flip (row, col); //Backtrack, turn the original back    if(Col <4) {dfs (row, col+1, deep); When backtracking to determine whether the previous step is over, so the pass is deep, judge the current state, if not end, according to Row,col+1 change}Else{dfs (row+1,1, deep); }    return;}intMain () {Chars[Ten];  while(SCANF ("%s", s)! =EOF) {memset (chess,false,sizeof(chess));  for(inti =0; I <4; i++)        {            if(S[i] = ='b') {chess[1][i +1] =true; }        }         for(inti =2; I <=4; i++) {scanf ("%s", s);  for(intj =0; J <4; J + +)            {                if(S[j] = ='b') Chess[i][j+1] =true; }} Flag=0;  for(step =0; Step <= -; step++) {DFS (1,1,0); if(flag) Break; }        if(flag) printf ("%d\n", step); Elseprintf ("impossible\n"); }    return 0;}

Poj1753flip Game (DFS + enumeration)

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.