码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1753 Flip Game

时间:2014-08-19 23:46:15      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   for   ar   div   代码   log   

先贴个BFS+位运算的代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
unsigned short q[65536],rear,top,step[65536];//(2^16)-1
bool vis[65536];
unsigned short temp;
bool bfs(){
    memset(q,0,sizeof(q));
    memset(vis,0,sizeof(vis));
    rear=top=0;
    q[top++]=temp;
    step[temp]=0;
    while(rear<top){
        temp=q[rear++];
        if(temp==0||temp==65535){
            cout<<step[temp]<<endl;
            return true;
        }
        short i=0;
        for(;i<16;i++){
            unsigned short ex=0;
            ex|=1<<i;
            if(i>3){
                ex|=1<<(i-4);
            }
            if(i<12){
                ex|=1<<(i+4);
            }
            if(i%4){
                ex|=1<<(i-1);
            }
            if((i+1)%4){
                ex|=1<<(i+1);
            }
            ex=ex^temp;
            if(!vis[ex]){
                vis[ex]=true;
                q[top++]=ex;
                step[ex]=step[temp]+1;
            }
        }
    }
    return false;
}
int main(){
    //freopen("D:\\INPUT.txt","r", stdin);
    short i=0;
    temp=0;
    for(;i<16;i++){
        char c;
        cin>>c;
        //cout<<c<<endl;
        if(c==‘b‘){
            temp|=(1<<i);
        }
    }
    if(!bfs()){
        cout<<"Impossible\n";//"\b"打成"/n",WA
    }
    return 0;
}

  

poj 1753 Flip Game,布布扣,bubuko.com

poj 1753 Flip Game

标签:blog   os   io   for   ar   div   代码   log   

原文地址:http://www.cnblogs.com/Deribs4/p/3923488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!