POJ 1753Flip Game

来源:互联网 发布:现货电子交易软件 编辑:程序博客网 时间:2024/06/06 03:02
#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<map>#include<cstdio>#include <string>#define ll long longusing namespace std;int st,vis[(1 << 17)];int change(int x,int w){    x ^= (1 << w);    if(w > 3)    {        x ^= (1 << (w - 4));    }    if(w < 12)    {        x ^= (1 << (w + 4));    }    if(w % 4)    {        x ^= (1 << (w - 1));    }    if(w % 4 < 3)    {        x ^= (1 << (w + 1));    }    return x;}int bfs(){    queue<pair<int,int> >q;    pair<int,int>stt;    stt.first = st,stt.second = 0;    //cout << st << endl;    q.push(stt);    vis[st] = 1;    if(st == 0 || st == 65535)        return 0;    while(!q.empty())    {        pair<int,int> te = q.front();        q.pop();        for(int i = 0;i < 16;i++)        {            int now = change(te.first,i);            if(now == 0 || now == 65535)                return te.second + 1;            if(vis[now])                continue;            vis[now] = 1;            q.push(make_pair(now,te.second + 1));        }    }    return -1;}int main(){string gg;while(cin >> gg){        memset(vis,0,sizeof(vis));        st = 0; for(int j = 0;j < 4;j++)            {                if(gg[j] == 'b')                {                    st |= (1 << (j));                }            }        for(int i = 1;i < 4;i++)        {            char s[11];            scanf("%s",s);            for(int j = 0;j < 4;j++)            {                if(s[j] == 'b')                {                    st |= (1 << (i * 4 + j));                }            }        }        //cout << st << endl;        int ou = bfs();        if(ou == -1)            cout << "Impossible\n";        else            cout << ou << endl;    }    return 0;}
0 0
原创粉丝点击