zoukankan      html  css  js  c++  java
  • poj 1753 Flip Game

    先贴个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",WA
        }
        return 0;
    }
    

      

  • 相关阅读:
    javaScript
    CSS
    HTML
    折纸 (模拟)
    不等式(数学)
    周期串查询
    大集训模拟赛十一
    大假期集训模拟赛十
    P1631 序列合并
    KMP(烤馍片)算法
  • 原文地址:https://www.cnblogs.com/Deribs4/p/3923488.html
Copyright © 2011-2022 走看看