• POJ 1753 Flip Game


    Flip Game
    Time Limit: 1000MSMemory Limit: 65536K
    Total Submissions: 23368Accepted: 10062

    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).

    POJ 1753 Flip Game - qhn999 - 码代码的猿猿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

    bwwbbbwbbwwbbwww

    Sample Output

    4

    Source

    #include <string>
    #include <cstring>
    #include <queue>

    #define GOAL1 0
    #define GOAL2 65535
    #define MAX 100000

    using namespace std;

    int change(string s)
    {
        int sum=0;
        int len=s.length();
        for(int i=0;i<len;i++)
        {
            if(s=='b')
            {
                sum+=1<<i;
            }
        }
        return sum;
    }

    void _move(string& s,int n)
    {
        if(s[n]=='b')  s[n]='w';
        else s[n]='b';
        if(n-4>=0)
        {
            if(s[n-4]=='b')  s[n-4]='w';
            else s[n-4]='b';
        }
        if(n+4<=15)
        {
            if(s[n+4]=='b')  s[n+4]='w';
            else s[n+4]='b';
        }
        if(n-1>=0&&n/4==(n-1)/4)
        {
            if(s[n-1]=='b')  s[n-1]='w';
            else s[n-1]='b';
        }
        if(n+1<=15&&(n+1)/4==n/4)
        {
            if(s[n+1]=='b')  s[n+1]='w';
            else s[n+1]='b';
        }
    }

    struct Note
    {
        int deepth;
        string str;
    }note[MAX];

    queue<Note> q;
    int vis[MAX];

    int bfs()
    {
        Note t, l;
        while(!q.empty())
        {
            l=t=q.front();
            q.pop();
            if(change(t.str)==GOAL1||change(t.str)==GOAL2)
            {
                return t.deepth;
            }
            for(int i=0;i<16;i++)
            {
                l=t;
                _move(l.str,i);
                int k=change(l.str);
                if(vis[k]==0)
                {
                    l.deepth=t.deepth+1;
                    q.push(l);
                    vis[k]=1;
                }
            }
        }
        return -999;
    }

    int main()
    {
        Note k;
        string stk;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<4;i++)
        {
            cin>>stk;
            k.str+=stk;
        }
        k.deepth=0;
        int num=change(k.str);
        vis[num]=1;
        q.push(k);
        int B=bfs();

        if(B==-999)
            cout<<"Impossible"<<endl;
        else
            cout<<B<<endl;

        return 0;
    }

  • 相关阅读:
    docker 的端口映射
    Python OpenCV实现图像模板匹配详解
    async和await用法(Task)
    C#,哈夫曼编码(Huffman Code)压缩(Compress )与解压缩(Decompress)算法与源代码
    python opencv轮廓检测
    Docker 创建镜像、修改、上传镜像
    C# .NET4.0 自定义文件并实现文件与应用程序关联
    How to Assign a Custom Icon to a File Type
    【服务器数据恢复】服务器误删除KVM虚拟机的数据恢复案例
    【服务器数据恢复】OceanStor存储中NAS卷数据丢失的数据恢复案例
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351097.html
Copyright © 2020-2023  润新知