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 ;}
  • 相关阅读:
    使用Nginx搭建图片服务器(windows)
    Vue中使用百度地图——设置地图标注
    大型网站的 HTTPS 实践(三)——基于协议和配置的优化(转)
    大型网站的 HTTPS 实践(二)——HTTPS 对性能的影响(转)
    大型网站的 HTTPS 实践(一)—— HTTPS 协议和原理(转)
    TCP的状态兼谈Close_Wait和Time_Wait的状态
    SYN Flood攻击及防御方法 (转)
    LINUX下解决netstat查看TIME_WAIT状态过多问题(转)
    IP负载均衡技术
    ubuntu下安装Apache+PHP+Mysql
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2390566.html
Copyright © 2011-2022 走看看