zoukankan      html  css  js  c++  java
  • POJ 1753 Flip Game 暴力 深搜

    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 59468   Accepted: 24750

    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 #include <iostream>
      2 #include <cstdio>
      3 
      4 using namespace std;
      5 
      6 char str[6];
      7 int a[6][6];
      8 int ans=17;
      9 
     10 void flip(int x,int y)
     11 {
     12     a[x][y]=!a[x][y];
     13     a[x-1][y]=!a[x-1][y];
     14     a[x+1][y]=!a[x+1][y];
     15     a[x][y-1]=!a[x][y-1];
     16     a[x][y+1]=!a[x][y+1];
     17 }
     18 
     19 bool same_color(void)
     20 {
     21     for(int i=1;i<=4;++i)
     22     {
     23         for(int j=1;j<=4;++j)
     24         {
     25             if(a[i][j]!=a[1][1])
     26             {
     27                 return false;
     28             }
     29         }
     30     }
     31     return true;
     32 }
     33 
     34 void dfs(int x,int y,int t)
     35 {
     36     // 判断是否同色,同色满足,返回
     37     if(same_color())
     38     {
     39         if(ans>t)
     40         {
     41             ans=t;
     42         }
     43         return;
     44     }
     45     // 深搜截止条件
     46     if(x>4)
     47     {
     48         return;
     49     }
     50 
     51     if(y==4)
     52     {
     53         // 当前不反转
     54         dfs(x+1,1,t);
     55         // 当前反转
     56         flip(x,y);
     57         dfs(x+1,1,t+1);
     58         // 状态还原
     59         flip(x,y);
     60     }
     61     else
     62     {
     63         dfs(x,y+1,t);
     64         flip(x,y);
     65         dfs(x,y+1,t+1);
     66         flip(x,y);
     67     }
     68 
     69 }
     70 
     71 int main()
     72 {
     73     for(int i=1;i<=4;++i)
     74     {
     75         scanf("%s",str+1);
     76         for(int j=1;j<=4;++j)
     77         {
     78             // a->0,b->1
     79             if(str[j]=='w')
     80             {
     81                 a[i][j]=0;
     82             }
     83             else
     84             {
     85                 a[i][j]=1;
     86             }
     87             //a[i][j]=str[j]-'a';
     88             //printf("%d ",a[i][j]);
     89         }
     90         //printf("
    ");
     91     }
     92 
     93 
     94     dfs(1,1,0);
     95 
     96     if(ans==17)
     97     {
     98         printf("Impossible
    ");
     99     }
    100     else
    101     {
    102         printf("%d
    ",ans);
    103     }
    104 
    105 
    106 
    107     return 0;
    108 }
  • 相关阅读:
    转:一个实例明白AutoResetEvent和 ManulResetEvent的用法
    取消office2010默认微软拼音输入法
    错误:在 ServiceModel 客户端配置部分中,找不到引用协定“*********.I******”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配的终结点元素。
    ASP 空字符串、IsNull、IsEmpty区别分析
    asp.net防止多次登录的方法
    关于javascript的keycode
    学习资料
    关于javascript中的typeof和instanceof介绍
    完整的SQLHelper
    图片自动分割代码求注释
  • 原文地址:https://www.cnblogs.com/jishuren/p/12370371.html
Copyright © 2011-2022 走看看