zoukankan      html  css  js  c++  java
  • HDU 1429 (bfs+状态的位压缩)

    hdu 1429 胜利大逃亡(续)

    一共有10把钥匙,用10位的二进制反映钥匙的拥有情况

    #include <iostream>
    #include <cstring>
    #include<cstdio>
    #include <queue>
    using namespace std;
    char maze[21][21];
    int n,m,t;
    int dx[4]={0,1,0,-1};
    int dy[4]={1,0,-1,0};
    int vis[1024][21][21];     //2^10-1 = 1023
    typedef struct node{
        int x,y,key,step;
    }Node;
    queue<Node> q;
    int bfs(int sx,int sy){
        while (!q.empty()) q.pop();
        vis[0][sx][sy]=1;
        Node s={sx,sy,0,0};
        q.push(s);
        while(!q.empty()){
            Node u = q.front();
            q.pop();
            int x,y,key,step,nx,ny,nkey,nstep,d;
            x=u.x; y=u.y; key=u.key; step=u.step;
            if(step >= t-1) return -1;
            for(d = 0;d < 4;d ++){
                nx=x+dx[d]; ny=y+dy[d]; nkey=key;
                if (!maze[nx][ny] || maze[nx][ny]=='*') continue;
                if (isupper(maze[nx][ny]))
                 {
                    int t=maze[nx][ny]-'A';
                    if (!(key&(1<<t))) continue;      //钥匙不匹配
                 }
                 if (islower(maze[nx][ny]))
                 {
                     int t=maze[nx][ny]-'a';
                     nkey=key|(1<<t);                 //更新钥匙状态
                 }
                 if (vis[nkey][nx][ny]) continue;
                 vis[nkey][nx][ny]=1;
                 nstep=step+1;
                 node v={nx,ny,nkey,nstep};
                 if (maze[v.x][v.y]=='^')
                    return v.step;
                 q.push(v);
             }
            }
            return -1;
    }
    int main()
    {
        int x,y,ans,i,j;
        while(cin >> n >> m >> t){
            memset(vis,0,sizeof(vis));
            for (i=1;i<=n;i++)
                scanf("%s",maze[i]+1);
            for (i=1;i<=n;i++)
            for (j=1;j<=m;j++)
                 if (maze[i][j]=='@')
                 {
                     x=i; y=j;
                     maze[i][j]='.';
                 }
            ans = bfs(x,y);
            cout << ans << endl;
            for (i=0;i<=n+1;i++){
            for (j=0;j<=m+2;j++){
                cout << maze[i][j] << " ";
            }
            cout << endl;}
    
        }
        return 0;
    }
  • 相关阅读:
    大数问题(三)(大数相除)
    直接插入排序的四种实现方法
    蟠桃记
    杭电oj find your present (2)
    CSS中的class与id区别及用法
    史上最全的css hack(ie6-9,firefox,chrome,opera,safari) (zz)
    CSS之CSS hack
    HTML语言的一些元素(五)
    HTML语言的一些元素(四)
    HTML语言的一些元素(三)
  • 原文地址:https://www.cnblogs.com/zhangjialu2015/p/5657763.html
Copyright © 2011-2022 走看看