• poj 1753 Flip Game (dfs)


    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 28805   Accepted: 12461

    Description

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you 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 are chosen every round according to the following rules: 
    1. Choose any one of the 16 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 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 pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will 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 to write a program that will 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 that 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 from 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
    

    Source

     1 //164K    125MS    C++    1211B    2014-04-26 11:02:12
     2 /*
     3 
     4     题意:
     5         问最少翻几步可以使棋盘棋子一样,不可能就输出Impossible 
     6 
     7     搜索枚举:
     8         枚举全部状态,每个位置的棋子有翻或不翻两种状态,枚举全部状态。
     9     注意一个棋子翻两次则和没翻一样,所以一种有2^16种情况,用dfs枚举全部状态。 
    10     
    11 */
    12 #include<stdio.h>
    13 #include<string.h>
    14 int g[10][10];
    15 int flag;
    16 int judge(int tg[][10]) //判断 
    17 {
    18     for(int i=1;i<=4;i++)
    19         for(int j=1;j<=4;j++)
    20             if(g[i][j]!=g[1][1]) return 0;
    21     return 1;
    22 }
    23 void flip(int i,int j) //翻棋 
    24 {
    25     g[i][j]^=1;
    26     g[i-1][j]^=1;
    27     g[i+1][j]^=1;
    28     g[i][j-1]^=1;
    29     g[i][j+1]^=1;
    30 }
    31 void dfs(int x,int y,int cnt,int n)
    32 {
    33     if(cnt==n){
    34         flag=judge(g);
    35         return;
    36     }
    37     if(flag || y>4) return;
    38     flip(x,y);  
    39     if(x<4) dfs(x+1,y,cnt+1,n);
    40     else dfs(1,y+1,cnt+1,n);
    41     flip(x,y);  
    42     if(x<4) dfs(x+1,y,cnt,n);
    43     else dfs(1,y+1,cnt,n);
    44 } 
    45 int main(void)
    46 {
    47     char c[10];
    48     while(scanf("%s",c)!=EOF)
    49     {
    50         memset(g,0,sizeof(g));
    51         for(int i=0;i<4;i++) g[1][i+1]=c[i]=='b'?1:0;
    52         for(int i=1;i<4;i++){
    53             scanf("%s",c);
    54             for(int j=0;j<4;j++)
    55             g[i+1][j+1]=c[j]=='b'?1:0;
    56         }
    57         flag=0;
    58         int cnt=-1;
    59         for(int i=0;i<16;i++){
    60             dfs(1,1,0,i);
    61             if(flag){
    62                 cnt=i;break;
    63             }
    64         }
    65         if(cnt==-1) puts("Impossible");
    66         else printf("%d
    ",cnt);
    67     }
    68     return 0; 
    69 }
    70 /*
    71 
    72 bwwb
    73 bbwb
    74 bwwb
    75 bwww
    76 
    77 bwbw 
    78 bwww 
    79 wwwb 
    80 wwwb 
    81 
    82 bwww
    83 wwww
    84 wwww
    85 wwww
    86 
    87 */
  • 相关阅读:
    google code jam exercise——Your Rank Is Pure
    C/C++中的文件操作(2)
    google code jam exercise——File Fix It
    google code jam exercise——All Your Base
    C/C++中的文件操作(1)
    google code jam exercise——Center of Mass
    C/C++字符串操作split
    JavascriptDOM
    Javascript特权方法
    Report子报表
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3690995.html
Copyright © 2020-2023  润新知