zoukankan      html  css  js  c++  java
  • hdu1429 bfs+状态压缩

    题目是十把钥匙,所以可以用状态压缩来表示(不解释),之后就是简单的BFS了,不过貌似有点容易爆内存的样子啊….

    #include <iostream>
    #include <cmath>
    #include <cctype>
    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <utility>
    #define inf (1<<28)
    #define ll long long
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define mid ((l+r)>>1)
    using namespace std;
    #define maxn 24
    
    struct Node
    {
         int x, y, t, state;
    };
    char g[maxn][maxn];
    int v[maxn][maxn][1<<10];
    int n,m,T;
    queue<Node>q;
    int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
    inline int  ok(char c)
    {
         return c - (isupper(c) ? 'A' : 'a');
    }
    inline int check(int x, int y)
    {
         return (x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '*');
    }
    int Bfs(int x,int y,int t,int state)
    {
        Node a,b;
        while(!q.empty())q.pop();
        a.x=x,a.y=y,a.t=t,a.state=state;
        q.push(a);
        v[x][y][state]=1;
        memset(v,0,sizeof(v));
        while(!q.empty())
        {
            a=q.front();
            q.pop();
            if(a.t>=T)break;
            for(int i=0;i<4;i++)
            {
                b.x=a.x+dir[i][0];
                b.y=a.y+dir[i][1];
                b.state=a.state;
                b.t=a.t+1;
                if(check(b.x,b.y)||v[b.x][b.y][b.state])continue;//越界,已访问
                if(isupper(g[b.x][b.y])&&!(b.state&(1<<ok(g[b.x][b.y]))))continue;//没钥匙
                if(islower(g[b.x][b.y]))b.state|=1<<ok(g[b.x][b.y]);//状态压缩
                if(g[b.x][b.y]=='^')return b.t;
                if(!v[b.x][b.y][b.state]){
                    v[b.x][b.y][b.state]=1;
                    q.push(b);
                }
            }
        }
        return -1;
    }
    int main()
    {
         int x=0,y=0;
         while(~scanf("%d%d%d",&n,&m,&T)){
              T -= 1;
              for(int i=0;i<n;i++){
                   scanf("%s", g[i]);
                   for(int j=0;j<m;j++){
                        if(g[i][j]=='@'){
                             g[i][j]='.';
                             x=i,y=j;
                        }
                   }
              }
              printf("%d
    ", Bfs(x,y,0,0));
         }
         return 0;
    }
    
  • 相关阅读:
    状态压缩DP 不断学习中。。。。。。
    程序员技术练级攻略(转)
    SQL Server 2008安装提示1608错误的解决方法
    Delphi字符串指针操作
    一个Python练习
    Android开发中的svn问题
    Python使用正则表达式替换源码前序号
    百度地图之Hello world !
    用Python写的一个简单的端口扫描程序
    android地图定位
  • 原文地址:https://www.cnblogs.com/wlxtuacm/p/5712287.html
Copyright © 2011-2022 走看看