zoukankan      html  css  js  c++  java
  • POJ --- 1753 Flip Game

    Flip Game

    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次,翻偶数次等于翻0次,因此16个棋子就有2^16种翻转的状态,枚举每个状态得到最小解。可以事先将翻转每个棋子后对应的状态保存起来。
    AC代码(递归版):
     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 char s[5][5];
     5 int st[16], ans = 17, all = 0;
     6 int status[16]={0xC800, 0xE400, 0x7200, 0x3100, 
     7     0x8C80, 0x4E40, 0x2720, 0x1310,
     8     0x08C8, 0X04E4, 0X0272, 0X0131,
     9     0X008C, 0X004E, 0X0027, 0X0013};
    10 void dfs(int id, int steps){
    11     if(steps >= ans) return;
    12     if(id >= 16){
    13         if(all == 0 || all == 65535) ans = steps;
    14         return;
    15     }
    16     dfs(id+1, steps);
    17     all ^= status[id];
    18     dfs(id+1, steps+1);
    19     all ^= status[id];
    20 }
    21 int main(){
    23     scanf("%s%s%s%s", s[3], s[2], s[1], s[0]);
    24     for(int i = 0;i < 4;i ++)
    25         for(int j = 0;j < 4;j ++)
    26             if(s[i][j] == 'b') all ^= 1 << (i * 4 + j);
    27     dfs(0, 0);
    28     ans > 16 ? printf("Impossible") : printf("%d
    ", ans);
    29     return 0;
    30 }

     下面是非递归的,测试的数据结果都一样,就是不给过,=!_!=。

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 int status[16]={0xC800, 0xE400, 0x7200, 0x3100, 
     5     0x8C80, 0x4E40, 0x2720, 0x1310,
     6     0x08C8, 0X04E4, 0X0272, 0X0131,
     7     0X008C, 0X004E, 0X0027, 0X0013
     8 };
     9 int main(){
    10     char s[5][5];12     while(~scanf("%s%s%s%s", s[3], s[2], s[1], s[0])){
    13         int ans = 17, all = 0, temp, pt[20];
    14         for(int i = 0;i < 4;i ++)
    15             for(int j = 0;j < 4;j ++)
    16                 if(s[i][j] == 'b') all ^= 1 << (i * 4 + j);
    17         for(int i = 0;i < 1 << 16;i ++){
    18             int k = 0, temp = all;
    19             for(int j = 0;j < 16;j ++)
    20                 if(i & (1 << j)) pt[k++] = j;
    21             for(int j = 0;j < k;j ++) all ^= status[15-pt[j]];
    22             if(all == 0 || all == 65535) ans = min(ans, k);
    23             else all = temp;
    24         }
    25         ans > 16 ? printf("Impossible") : printf("%d
    ", ans);
    26     }
    27     return 0;
    28 }
     
  • 相关阅读:
    回顾
    单例模式
    元类
    反射和内置方法
    issubclass 和 isinstance和断点调试
    绑定方法和非绑定方法
    并发编程:IO多路复用。
    基于tcp的下载文件,以及struct模块的应用。
    并发编程:协程,异步调用。
    并发编程:GIL,线程池,进程池,阻塞,非阻塞,同步,异步
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3638013.html
Copyright © 2011-2022 走看看