zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    关于jar包的创建及引用相关问题
    关于Android系统一次能创建多大的Bitmap?
    Android开发导入第三方Jar包
    Javascript操作表格
    installanywhere 打包j2ee的方法II(转)
    C#网页自动登录和提交POST信息的多种方法
    JavaScript手册javascript语法javascript函数
    javascript 禁止复制网页
    [Android实例] Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式(转)
    android消息处理系统原理
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351097.html
Copyright © 2011-2022 走看看