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 ;}
  • 相关阅读:
    PowerDesigner概念设计模型(CDM)中的3种实体关系
    基于Prototype 1.6.2 框架下的数据分页
    中国地区,北京54坐标系条带号的选
    C#中MessageBox的使用
    C#注册表的读,写,删除,查找 (转)
    C# Tostring() 格式大全 [转]
    Layer features in this layer set, ArcEngine图层标注源码 (转)
    判断点是否在多边形之内的方法
    C#导入Excel到Dataset和导出Excel到DataTable
    Server.MapPath方法的应用方法
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2390566.html
Copyright © 2011-2022 走看看