• poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)


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

     
    第一种方法是bfs+状态压缩,将整张图压缩为01状态,算出二进制状态的十进制的值。然后跑bfs,bfs里面是枚举16个位置的变换,( 这里预处理出变换的值,然后进行异或计算),再加个标记vis不就行了。
     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 using namespace std;
    15 #define max(a,b) (a) > (b) ? (a) : (b)  
    16 #define min(a,b) (a) < (b) ? (a) : (b)
    17 #define ll long long
    18 #define eps 1e-10
    19 #define MOD 1000000007
    20 #define N 1000000
    21 #define inf 1e12
    22 
    23 int vis[(1<<17)];
    24 struct Node{
    25     int val;
    26     int t;
    27     
    28 }st;
    29 int mp[20]={
    30     51200,58368,29184,12544,35968,20032,10016,4880,2248,1252,626,305,140,78,39,19
    31 };
    32 void bfs(){
    33     queue<Node>q;
    34     q.push(st);
    35     Node t1,t2;
    36     vis[st.val]=1;
    37     while(!q.empty()){
    38         t1=q.front();
    39         q.pop();
    40         if(t1.val==0 || t1.val==((1<<16)-1)){
    41             printf("%d
    ",t1.t);
    42             return ;
    43         }
    44         for(int i=0;i<16;i++){
    45             t2=t1;
    46             t2.val^=mp[i];
    47             if(vis[t2.val]) continue;
    48             vis[t2.val]=1;
    49             t2.t++;
    50             q.push(t2);
    51         }
    52     }
    53     printf("Impossible
    ");
    54 }
    55 int main()
    56 {
    57     char s[6];
    58     int ans=0;
    59     int num=15;
    60     for(int i=0;i<4;i++){
    61         scanf("%s",s);
    62         for(int j=0;j<4;j++){
    63             if(s[j]=='b'){
    64                 ans+=(1<<num);
    65             }
    66             num--;
    67         }
    68     }
    69     st.val=ans;
    70     st.t=0;
    71     memset(vis,0,sizeof(vis));
    72     bfs();
    73 
    74     return 0;
    75 }
    View Code
     
    第二种方法是dfs枚举。

    思路:仔细考虑,可以发现,一个位置的棋子,要么保持原状不变,要么改变一次。因为改变偶数次和不改变的效果是一样的,改变奇数次和改变1次的效果是一样的。也就是说,达到最后颜色都一样的状态,某个位置的棋子,要么改变一次,要么一次也不改变。所以求最少经过多少步的改变,可以转化为最少需要改变多少个棋子。因为数据范围小,总共16个棋子,所以我们可以枚举。

    我们可以判断改变0个,改变1个,改变2个,,,改变16个。改变x个,就是从16个元素中选x个的过程,可以用dfs实现。如果一直到最后改变16个都不行,则输出“Impossible”即可。

      1 #pragma comment(linker, "/STACK:1024000000,1024000000")
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<math.h>
      7 #include<algorithm>
      8 #include<queue>
      9 #include<set>
     10 #include<bitset>
     11 #include<map>
     12 #include<vector>
     13 #include<stdlib.h>
     14 using namespace std;
     15 #define max(a,b) (a) > (b) ? (a) : (b)  
     16 #define min(a,b) (a) < (b) ? (a) : (b)
     17 #define ll long long
     18 #define eps 1e-10
     19 #define MOD 1000000007
     20 #define N 26
     21 #define inf 1e12
     22 int mp[6][6];
     23 int newMp[6][6];
     24 int newRecord[N];
     25 int dirx[]={0,0,-1,1};
     26 int diry[]={-1,1,0,0};
     27 int flag;
     28 int vis[N];
     29 void judge(int cnt){
     30     for(int i=0;i<4;i++){
     31         for(int j=0;j<4;j++){
     32             newMp[i][j]=mp[i][j];
     33         }
     34     }
     35     for(int i=0;i<cnt;i++){
     36         int x=newRecord[i]/4;
     37         int y=newRecord[i]-x*4;
     38         newMp[x][y]=!newMp[x][y];
     39         for(int j=0;j<4;j++){
     40             int newX=x+dirx[j];
     41             int newY=y+diry[j];
     42             if(newX<0 || newX>=4 || newY<0 || newY>=4) continue;
     43             newMp[newX][newY]=!newMp[newX][newY];
     44         }
     45     }
     46     int ans=0;
     47     for(int i=0;i<4;i++){
     48         for(int j=0;j<4;j++){
     49             if(newMp[i][j]){
     50                 ans++;
     51             }
     52         }
     53     }
     54     if(ans==0 || ans==16){
     55         flag=1;
     56     }
     57 }
     58 void dfs(int st,int num,int cnt){
     59     if(num==cnt){
     60         judge(cnt);
     61         return;
     62     }
     63     
     64     for(int i=st;i<16;i++){
     65         if(!vis[i]){
     66             vis[i]=1;
     67             newRecord[num]=i;
     68             dfs(i,num+1,cnt);
     69             if(flag) 
     70                return;
     71             vis[i]=0;
     72         }
     73     }
     74     
     75 }
     76 int main()
     77 {
     78     char s[6];
     79     for(int i=0;i<4;i++){
     80         scanf("%s",s);
     81         for(int j=0;j<4;j++){
     82             if(s[j]=='b'){
     83                 mp[i][j]=1;
     84             }
     85             else{
     86                 mp[i][j]=0;
     87             }
     88         }
     89     }
     90     flag=0;
     91     for(int i=0;i<=16;i++){//改变多少个可以达到目的 
     92          memset(vis,0,sizeof(vis));
     93          
     94         dfs(0,0,i);
     95         if(flag){
     96             printf("%d
    ",i);
     97             break;
     98         }
     99     }
    100     if(flag==0){
    101         printf("Impossible
    ");
    102     }
    103     
    104     return 0;
    105 }
    View Code
  • 相关阅读:
    .net常用框架总结
    微信小程序 语音转换
    nginx+redis实现session共享 .NET分布式架构
    Redis 安装及注册服务
    WebApi跨域
    Uri各个属性取值测试
    一些常用的FFMPEG命令集合
    动态规划重学习笔记
    给自己的电脑时间进行精准校时
    [NOI题库][POJ2536][匈牙利算法][二分图最大匹配]Gopher II
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4811375.html
Copyright © 2020-2023  润新知