<html>

题目链接:hdu 5094 Maze

题目大意:给定一张图,如今要从(1,1)的位置移动到(n,m)。中途有些位置是存在门或者墙的。

相应的门须要那到相应

的钥匙才干够通过。

而且给定钥匙的位置。

解题思路:就是普通的bfs,钥匙的拥有能够用一个二进制数表示。唯一麻烦的是它门以及墙是落在边上的,所以我预

处理的时候直接在边上搞。

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
typedef pair<int,int> pii;

const int maxn = 55;
const int maxs = 3000;
const int inf = 0x3f3f3f3f;
const int dir[4][2] = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}};

int N, M, P, v[maxn][maxn];
int g[maxn * maxn][4], dp[maxn][maxn][maxs];

inline int idx(int x, int y) {
    return (x-1) * M + y - 1;
}

void init () {
    memset(v, 0, sizeof(v));
    memset(g, -1, sizeof(g));
    memset(dp, inf, sizeof(dp));

    int n, x1, y1, x2, y2, k, d;
    scanf("%d", &n);
    while (n--) {
        scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
        if (x1 == x2)
            d = (y1 > y2 ? 2 : 1);
        else
            d = (x1 > x2 ? 0 : 3);
        g[idx(x1, y1)][d] = g[idx(x2, y2)][3-d] = k;
    }

    scanf("%d", &n);
    while (n--) {
        scanf("%d%d%d", &x1, &y1, &k);
        v[x1][y1] |= (1<<(k-1));
    }
}

int bfs () {
    queue<pii> Q;
    Q.push(make_pair(idx(1, 1), 0));
    dp[1][1][0] = 0;

    while (!Q.empty()) {
        pii u = Q.front(); Q.pop();
        int x = u.first / M + 1;
        int y = u.first % M + 1;

        if (x == N && y == M)
            return dp[x][y][u.second];

        for (int i = 0; i < 4; i++) {
            if (g[u.first][i] == 0)
                continue;
            int xx = x + dir[i][0];
            int yy = y + dir[i][1];

            if (xx <= 0 || xx > N || yy <= 0 || y > M)
                continue;

            int s = u.second | v[xx][yy];
            int tmp = g[u.first][i];
            if (tmp == -1 || (u.second & (1<<(tmp-1)))) {
                if (dp[xx][yy][s] > dp[x][y][u.second] + 1) {
                    dp[xx][yy][s] = dp[x][y][u.second] + 1;
                    Q.push(make_pair(idx(xx, yy), s));
                }
            }
        }
    }
    return -1;
}

int main () {
    while (scanf("%d%d%d", &N, &M, &P) == 3) {
        init();
        printf("%d\n", bfs());
    }
    return 0;
}
版权声明:本文为博主原创文章。未经博主同意不得转载。 举报
  • 本文已收录于下面专栏:

相关文章推荐

状态压缩(1)--hdu5094(状态压缩+bfs)(能力题)

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Problem ...

hdu 5094 状压bfs+深坑

http://acm.hdu.edu.cn/showproblem.php?pid=5094 #include #include #include #include #i...

hdu 5094 Maze (bfs+状态压缩)

hdu 5094 Maze (bfs+状态压缩)—— black 的专栏 ——waShaXiu

HDU 5094 状压BFS

给出n*m矩阵 给出k个障碍,两坐标之间存在墙或门。门最多10种。 给出s个钥匙位置及编号,对应的钥匙开对应的门 状压BFS就可以,注意有可能同一个位置有多个门或者多个钥匙 #inclu...

hdu5094(BFS+状压)

这题是上海邀请赛,BFS+状态压缩,很坑爹的一道题 坑就坑在一个位置能够有多个钥匙,我勒个去 这题学到一个方法。关于中间有墙的问题,road[maxn][maxn][4]定义这种形式推断。相比...

HDU5094->BFS&&状态压缩

HDU5094->BFS&&状态压缩题意: 有一个迷宫。每一个格子有四个方向,但这四个方向可能是门,可能是墙,也可能是通路。有些格子里有钥匙,每种钥匙仅仅能开相应品种的门。求从(1,1)到(n,m)所...

Hdu 5094 Maze(状压dp+bfs)

题目链接 Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) ...

HDU 2612 Find a way(简单BFS)

HDU 2612 Find a way(简单BFS) http://acm.hdu.edu.cn/showproblem.php?

pid=2612</strong

hdu 4885 TIANKENG’s travel(bfs)

<a target="_blank" href="http://acm.hdu.ed

HDOJ 题目5094 Maze(BFS+状压)

Coder(有米!) Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/O...
  • 微博
    微信
    QQ
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多仅仅同意输入30个字)

posted on 2017-08-16 09:22  ljbguanli  阅读(218)  评论(0编辑  收藏  举报