zoukankan      html  css  js  c++  java
  • Flip Game_位运算

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 40455   Accepted: 17573

    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
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    int t[20]={
        51200,58368,29184,12544,
        35968,20032,10016,4880,
        2248,1252,626,305,
        140,78,39,19,
    };//每张牌翻后的值
    struct node
    {
        int k;
        int step;
    };
    bool vis[1<<16];
    int N=1<<16;
    void bfs(int ans)
    {
        queue<node>qu;
        node now,next;
        now.k=ans;
        now.step=0;
        qu.push(now);
        vis[now.k]=true;
        while(!qu.empty())
        {
            now=qu.front();
            qu.pop();
            if(now.k==0||now.k==N-1)//要么全为b即为0,全为w即为1<<16-1;
           {
               printf("%d
    ",now.step);
               return ;
           }
           for(int i=0;i<16;i++)
           {
               next.k=now.k^t[i];
               next.step=now.step+1;
               if(!vis[next.k])
               {
                   vis[next.k]=true;
                   qu.push(next);
               }
           }
        }
        printf("Impossible
    ");
    }
    int main()
    {
        char ch[10];
        int ans=0,cnt=15;
        for(int i=1;i<=4;i++)
        {
            scanf("%s",ch);
            for(int j=0;j<4;j++)
            {
                if(ch[j]=='w')//w标记为1,b为0;
                    ans|=1<<cnt;//左移cnt位,
    //(cnt初始化为15,随循环--,确定1的位置)(总共16位,刚开始1在第一位上所以最多左移15位) cnt
    --; } } memset(vis,false,sizeof(vis));//记录翻过以后二进制码代表的值是否出现过 bfs(ans); return 0; }
  • 相关阅读:
    千亿美元规模,云计算的下半场将走向何方?
    巧用云原生能力和工具,提升云上运维效率
    基础设施代码化(IaC)的自动化配置与编排
    盘点2020 | 阿里云弹性计算年度关键词:快、弹、稳
    整体算力提升40% 芯片级安全防护 | 阿里云发布第七代ECS云服务器
    真正云原生的智能运维体系,阿里云发布ECS自动化运维套件
    安装wireshark
    查看linux的登录日志 centos7
    CentOS查看系统当前登录用户信息的4种方法
    free -m查询内存使用情况,祥解
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5806307.html
Copyright © 2011-2022 走看看