zoukankan      html  css  js  c++  java
  • 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 */
  • 相关阅读:
    【转】C#中的虚方法
    【转】ASP.NET 2.0中Page事件的执行顺序
    OWC ChartSpace控件的使用
    Ext对基本类型的扩展
    OWC PivotTable的使用方法
    .net中线程同步的典型场景和问题(1)
    python中使用汉字
    如何取消后台线程的执行
    yaffs2根文件系统的构建过程
    Fuck self.delegate = self
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3690995.html
Copyright © 2011-2022 走看看