zoukankan      html  css  js  c++  java
  • POJ 1753

    #include <iostream>
    #include <queue>
    using namespace std;
    
    char _m[4][4];
    bool mark[100000];
    
    struct node
    {
        int step;
        int state;
    };
    
    bool BFS(node p);
    
    bool boo;
    
    node t;
    
    void move(int r)//位操作的改变状态~
    {        
        t.state^=(1<<(15-r));
        if(r>=4)
              t.state^=(1<<(15-r+4));
        if(r<=11)
              t.state^=(1<<(15-r-4));    
        if((r%4))
              t.state^=(1<<(15-r+1));
        if(((r+1)%4))
              t.state^=(1<<(15-r-1));
    }
    
    
    
    queue<node> coll;
    
    int main()
    {
        //freopen("acm.acm","r",stdin);
        int i;
        int j;
        node begin;
        begin.state = 0;
        begin.step = 0;
    
        for(i = 0; i < 4; ++ i)
        {
            for(j = 0; j < 4; ++ j)
            {
                cin>>_m[i][j];
                if(_m[i][j] == 'b')
                {
                    begin.state += 1;
                }
                begin.state <<= 1;
            }
            
        }
        begin.state >>= 1;
        memset(mark,false,sizeof(mark));
        if(begin.state == 0 || begin.state == 65535)
        {
            cout<<"0"<<endl;
        }
        else
        {
            coll.push(begin);
            mark[begin.state] = true;
            boo = false;
            while(!coll.empty() && !BFS(coll.front()))
            {
                coll.pop();
            }
            if(!boo)
            {
                cout<<"Impossible"<<endl;
            }
        }
    
    }
    
    bool BFS(node p)
    {
    //    cout<<p.state<<endl;
        if(p.state == 0 || p.state == 65535)
        {
            boo = true;
            cout<<p.step<<endl;
            return true;
        }
        int i;
        
        for(i = 0; i < 16; ++ i)
        {
            
            t = p;
            move(i);
        //    cout<<t.state<<endl;
            if(!mark[t.state])
            {
                t.step = p.step + 1;
                coll.push(t);
                mark[t.state] = true;
            }
        }
    
        return false;
    }

    关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。 

    技术网站地址: vmfor.com

  • 相关阅读:
    SVN安装配置与使用
    ext中对json数据的处理解析
    matlab保存数据
    DLL编程总结
    【MFC 】关于对话框中的OnVScroll() 和 OnHScroll
    OpenCV cvReleaseImage把图像怎么样了?
    [code] if (x<0)x=0;else if (x>255)x=255;
    【DM642学习笔记十】DSP优化记录
    DSP日志打印 LOG_printf
    【MFC】MFC文本框中显示浮点数
  • 原文地址:https://www.cnblogs.com/gavinsp/p/4566534.html
Copyright © 2011-2022 走看看