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

    Flip Game
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 29059   Accepted: 12568

    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


    这个题也是用位运算搜索(枚举)

      0xc800,0xe400,0x7200,0x3100,
      0x8c80,0x4e40,0x2720,0x1310,
      0x08c8,0x04e4,0x0272,0x0131,
      0x008c,0x004e,0x0027,0x0013,

    用十六进制表示比较方便,这就是十六种状态,可以枚举每种即可,用异或操作,可以改变每种状态。


     1 /*======================================================================
     2  *           Author :   kevin
     3  *         Filename :   FlipGame.cpp
     4  *       Creat time :   2014-05-12 20:25
     5  *      Description :
     6  ========================================================================*/
     7 #include <iostream>
     8 #include <algorithm>
     9 #include <cstdio>
    10 #include <cstring>
    11 #include <queue>
    12 #include <cmath>
    13 #define M 6
    14 #define Max 0xffff
    15 using namespace std;
    16 int cnt[Max+100];
    17 int dir[M][M]={
    18     0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    19     0x0000,0xc800,0xe400,0x7200,0x3100,0x0000,
    20     0x0000,0x8c80,0x4e40,0x2720,0x1310,0x0000,
    21     0x0000,0x08c8,0x04e4,0x0272,0x0131,0x0000,
    22     0x0000,0x008c,0x004e,0x0027,0x0013,0x0000,
    23     0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
    24 };
    25 int BFS(int t)
    26 {
    27     queue<int>pq;
    28     cnt[t] = 1;
    29     pq.push(t);
    30     while(!pq.empty()){
    31         int num = pq.front();
    32         pq.pop();
    33         for(int i = 1; i <= 4; i++){
    34             for(int j = 1; j <= 4; j++){
    35                 int d = num^dir[i][j];
    36                 if(d == 0x0000 || d == 0xffff){
    37                     cnt[d] = cnt[num];
    38                     return cnt[d];
    39                 }
    40                 if(!cnt[d]){
    41                     cnt[d] = cnt[num]+1;
    42                     pq.push(d);
    43                 }
    44             }
    45         }
    46     }
    47     return -1;
    48 }
    49 void Init()
    50 {
    51     char str[M];
    52     int number = 0,steps;
    53     while(cin>>str){
    54         number = 0;
    55         memset(cnt,0,sizeof(cnt));
    56         for(int j = 0; j < 4; j++){
    57             number = number<<1;
    58             if(str[j] == 'w'){
    59                 number = number|1;
    60             }
    61         }
    62         for(int i = 0; i < 3; i++){
    63             cin>>str;
    64             for(int j = 0; j < 4; j++){
    65                 number = number<<1;
    66                 if(str[j] == 'w'){
    67                     number = number|1;
    68                 }
    69             }
    70         }
    71         if(number == 0x0000 || number == 0xffff){
    72             printf("0
    ");
    73             continue;
    74         }
    75         else{
    76             steps = BFS(number);
    77         }
    78         if(steps == -1){
    79             printf("Impossible
    ");
    80         }
    81         else{
    82             printf("%d
    ",steps);
    83         }
    84     }
    85 }
    86 int main(int argc,char *argv[])
    87 {
    88     Init();
    89     return 0;
    90 }
    View Code

    Do one thing , and do it well !
  • 相关阅读:
    oracle与DB2
    oracle ORA-01427: 单行子查询返回多个行
    mysql开发总结
    mysql show profile基本详解
    mysql批量插入数据
    mysql索引详解
    mysql性能调优
    MySQL优化
    mysql主从调优
    mysql主从复制
  • 原文地址:https://www.cnblogs.com/ubuntu-kevin/p/3727098.html
Copyright © 2011-2022 走看看