zoukankan      html  css  js  c++  java
  • poj1753Flip Game(枚举+DFS)

    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 38749   Accepted: 16834

    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
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<iostream>
     5 using namespace std;
     6 int r[]={-1,1,0,0,0};
     7 int c[]={0,0,-1,1,0};
     8 int chess[6][6]={0};
     9 int step,flag;
    10 int ac()
    11 {
    12     for(int i=1;i<5;i++)
    13     {
    14         for(int j=1;j<5;j++)
    15         {
    16             if(chess[i][j]!=chess[1][1])
    17             return 0;
    18         }
    19     }
    20     return 1;
    21 }
    22 void q(int a,int b)
    23 {
    24     for(int i=0;i<5;i++)
    25     {
    26         chess[a+r[i]][b+c[i]]=!chess[a+r[i]][b+c[i]];
    27     }
    28     return;
    29 }
    30 void dfs(int a,int b,int deep)
    31 {
    32     if(deep==step)
    33     {
    34         flag=ac();
    35         return;
    36     }
    37     if(flag||a==5)
    38     return;
    39     q(a,b);
    40     if(b<4)
    41     dfs(a,b+1,deep+1);
    42     else
    43     dfs(a+1,1,deep+1);
    44     q(a,b);
    45     if(b<4)
    46     dfs(a,b+1,deep);
    47     else
    48     dfs(a+1,1,deep);
    49     return;
    50 }
    51 main()
    52 {
    53     int i,j;
    54     char a;
    55     for(i=1;i<5;i++)
    56     {
    57         for(j=1;j<5;j++)
    58         {
    59             cin>>a;
    60             if(a=='b')
    61             chess[i][j]=1;
    62         }
    63     }
    64     for(step=0;step<=16;step++)
    65     {
    66         dfs(1,1,0);
    67         if(flag) break;
    68     }
    69     if(flag)
    70     printf("%d
    ",step);
    71     else
    72     printf("Impossible
    ");
    73 }
    View Code

     

  • 相关阅读:
    怎么看到数据库以前做过的日志?
    感觉很好的网站
    uview 滑动切换
    Flyweight享元(结构型模式)
    悟空,真的是空?
    Interpreter解释器(行为型模式)
    Proxy代理(结构型模式)
    [转]有一种爱叫索取
    Command命令(行为型模式)
    Composite组合(结构型模式)
  • 原文地址:https://www.cnblogs.com/CrazyBaby/p/5487459.html
Copyright © 2011-2022 走看看