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

  • 相关阅读:
    SunOS与Solaris系统的对应关系
    多媒体笔记
    【opencv源码解析】 二、 cvtColor
    SSE笔记
    work mark
    mark ubuntu 16.04 64bit + cpu only install mtcnn
    Ubuntu12.04+Caffe (+OpenCV+CPU-only)
    Win32 编程
    抠像的一些问题
    effect
  • 原文地址:https://www.cnblogs.com/gavinsp/p/4566534.html
Copyright © 2011-2022 走看看