zoukankan      html  css  js  c++  java
  • Flip Game

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 25302   Accepted: 10918

    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 //位运算启蒙题
     2 
     3 #include<stdio.h>
     4 #include<string.h>
     5 #include<iostream>
     6 #include<queue>
     7 using namespace std;
     8 
     9 struct node
    10 {
    11     int data;
    12     int step;
    13 };
    14 queue <node> que;
    15 int vis[65536];//数组标记是否进队列
    16 
    17 int filp(int s, int n)
    18 {
    19     s = s^(1<<n);
    20     if(n > 3) s = s^(1 << (n-4));
    21     if(n < 12) s = s^(1 << (n+4));
    22     if(n %4 != 0) s = s^(1 << (n-1));
    23     if(n %4 != 3) s = s^(1 << (n+1));
    24     return s;
    25 }
    26 void bfs(int x)
    27 {
    28     int ans;
    29     while(!que.empty())
    30         que.pop();
    31     struct node tmp,sub;
    32 
    33     tmp.data = x;
    34     tmp.step = 0;
    35     que.push(tmp);
    36     vis[x] = 1;
    37 
    38     while(!que.empty())
    39     {
    40         tmp = que.front();
    41         que.pop();
    42         int cnt = tmp.data;
    43         if(cnt == 0 || cnt == 65535)//当x等于0或65535时说明整个棋盘都是白色或黑色
    44         {
    45             printf("%d
    ",tmp.step);
    46             return;
    47         }
    48         for(int i = 0; i < 16; i++)//对其二进制的每一位穷举,
    49         {
    50             ans = filp(cnt,i);
    51             sub.data = ans;
    52             sub.step = tmp.step + 1;
    53             if(!vis[ans])
    54             {
    55                 vis[ans] = 1;
    56                 que.push(sub);
    57             }
    58 
    59         }
    60     }
    61     printf("Impossible
    ");
    62 }
    63 int main()
    64 {
    65     int i,j;
    66     char s[16][16];
    67     int x = 0;//x初始化为0,用x的二进制表示整个棋盘;
    68     for(i = 0; i < 4; i++)
    69     {
    70         scanf("%s",s[i]);
    71         for(j = 0; j < 4; j++)
    72         {
    73             if(s[i][j] == 'w')
    74                 x = x << 1;//用0表示白色,x左移一位
    75             else
    76             {
    77                 x = (x << 1)+1;//用1表示黑色,x左移一位加1;
    78             }
    79         }
    80     }
    81     memset(vis,0,sizeof(vis));
    82     bfs(x);
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    C# Debugger.IsAttached 调试启动浏览器 VS if DEBUG 启动调试内容
    【转载】如何三个月从零基础到C#中级程序员
    【转载】如何成为一个高级程序员
    如何找回QQ聊天记录、语音、图片?
    hexo博客yili主题个性化自定义教程(1) ——借鉴中学习,初认yili主题
    2019hexo博客部署到coding该绕的坑-奥怪的小栈
    2019Hexo博客Next主题深度美化 打造一个炫酷博客(2)-奥怪的小栈
    2019软件工程专业大学排名附官网-奥怪的小栈
    浅谈互联网+足球
    2019基于Hexo快速搭建个人博客,打造一个炫酷博客(1)-奥怪的小栈
  • 原文地址:https://www.cnblogs.com/LK1994/p/3234355.html
Copyright © 2011-2022 走看看