zoukankan      html  css  js  c++  java
  • HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少。

    析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态,然后再BFS,就简单了。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 100 + 5;
    const int mod = 1e9 + 7;
    const int dr[] = {0, 1, 0, -1};
    const int dc[] = {1, 0, -1, 0};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    struct Node{
        int x, y, d, state;
        Node() { }
        Node(int xx, int yy, int dd, int k) : x(xx), y(yy), d(dd), state(k) { }
    };
    char s[maxn][maxn];
    bool vis[maxn][maxn][20];
    map<char, int> key, door;
    
    void bfs(int x, int y){
        queue<Node> q;
        memset(vis, 0, sizeof vis);
        q.push(Node(x, y, 0, 0));
        vis[x][y][0] = true;
    
        while(!q.empty()){
            Node u = q.front(); q.pop();
            for(int i = 0; i < 4; ++i){
                int x = u.x + dr[i];
                int y = u.y + dc[i];
                int d = u.d + 1;
                if(!is_in(x, y))  continue;
                if(s[x][y] == 'X'){
                    printf("Escape possible in %d steps.
    ", d);
                    return ;
                }
                if(s[x][y] == '#')  continue;
                int state;
                if(islower(s[x][y])) state = u.state | (1 << key[s[x][y]]);
                else if(isupper(s[x][y])){
                    if(u.state & (1<<door[s[x][y]]))  state = u.state;
                    else continue;
                }
                else if(s[x][y] == '.' || s[x][y] == '*')  state = u.state;
                if(!vis[x][y][state]){
                    vis[x][y][state] = true;
                    q.push(Node(x, y, d, state));
                }
            }
        }
        printf("The poor student is trapped!
    ");
    }
    
    int main(){
        key['b'] = 1;  door['B'] = 1;
        key['y'] = 2;  door['Y'] = 2;
        key['r'] = 3;  door['R'] = 3;
        key['g'] = 0;  door['G'] = 0;
        while(scanf("%d %d", &n, &m) == 2){
            if(!n && !m)  break;
            for(int i = 0; i < n; ++i)  scanf("%s", s+i);
            for(int i = 0; i < n; ++i)
                for(int j = 0; j < m; ++j)
                    if(s[i][j] == '*') { bfs(i, j);  break;  }
        }
        return 0;
    }
    
  • 相关阅读:
    vue全局启用 emulateJSON 选项
    vue全局配置数据接口的根域名
    CSS实现按钮YES-NO按钮+Jquery获取按钮状态。
    Redis命令
    在js中获取 input checkbox里选中的多个值
    Python中常见字符串去除空格的方法总结
    e.target.value和this的区别
    用脚本来运行scrapy crawl ...
    生成器的两种方式
    python中ord()函数,chr()函数,unichr()函数
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6072774.html
Copyright © 2011-2022 走看看