zoukankan      html  css  js  c++  java
  • poj 2965 The Pilots Brothers' refrigerator (bfs+位运算)

    http://poj.org/problem?id=2965 

    16个位置分别有两个状态,用一个16位的二进制表示,对beg按位或运算得到初始状态,bfs中,用只包含0,1的16进制数实现翻转操作。

    剩下的就是单纯的bfs了。

    code:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std ;
    int dir[16]={0x111f0x222f0x444f0x888f,
                 0x11f10x22f20x44f40x88f8,
                 0x1f110x2f220x4f440x8f88,
                 0xf1110xf2220xf4440xf888 } ;
    bool vis[1<<17] ;
    int beg ;
    struct data{
        int pre, step, value, x, y ;
    }q[1<<17] ;
    void bfs(){
        int head, end ;
        data temp, t ;
        head = end = 0 ;
        temp.value = beg ;
        temp.step = 0 ;
        q[head] = temp ;
        end = 1 ;
        vis[beg] = 1 ;
        while(end>head){
            temp = q[head++] ;
            for(int i=0; i<16; i++){
                t = temp ;
                t.value ^= dir[i] ;
                if(!vis[t.value]){
                    vis[t.value] = 1 ;
                    t.step = q[head-1].step + 1 ;
                    t.pre = head - 1 ;
                    t.x = i / 4 + 1 ;
                    t.y = i % 4 + 1 ;
                    if(t.value==0){
                        printf("%d\n", t.step) ;
                        while(t.step){
                            printf("%d %d\n", t.x, t.y) ;
                            t = q[t.pre] ;
                        }
                        return ;
                    }
                    q[end++] = t ;
                }
            }
        }
    }
    int main(){
        int i, j ;
        char a ;
        beg = 0 ;
        for(i=0; i<16; i++){
            cin >> a ;
            if(a=='+') beg |= 1<<i ;
        }
        memset(vis, 0sizeof(vis)) ;
        bfs() ;
        return 0 ;}
  • 相关阅读:
    ☀【布局】
    _#【CSS3】
    _#minheight
    【其它】引入css
    【css3】url
    鼠标闲置一段时间后自动隐藏
    图解SQLServer2005获取WebService数据
    Oracle字符串字段内的字符排序
    一个c#读取扫雷内存的demo
    sqlserver使用bcp分解字符串
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2390566.html
Copyright © 2011-2022 走看看