三维bfs

来源:互联网 发布:sql loader 350 编辑:程序博客网 时间:2024/04/28 04:51

神秘的迷宫

Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 17 Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
ZZC学长因为发现了奇异的花朵被神秘组织绑架到了一个阴暗的迷宫,这个迷宫有四种暗门和一个出口,每个暗门都有一把钥匙与其对应,粗心的神秘组织成员把一些钥匙散落在迷宫内。
ZZC学长只有找到钥匙才能打开暗门,他醒来后找到一张也是粗心的神秘组织成员留下的地图。
因为刚刚醒来,ZZC学长一分钟之内只能向上下左右走一格,走路的同时,他也能拿起钥匙或者打开暗门,不会影响走路速度。
ZZC学长希望以最快的速度离开迷宫,聪明的同学能帮帮他么?
Input
多组输入,每组输入第一行两个数字N和M表示迷宫的行数和列数。之后N行,每行M个字符描述该迷宫:.表示可以行走的路,#表示出口,*表示迷宫的墙壁,0表示ZZC学长当前位置,
1、2、3、4分别表示每种暗门,5、6、7、8依次对应每种钥匙。(0 < N,M < 1000)
Output
对于每组输入,在一行内输出一个数字,表示离开迷宫的最短时间,若无法找到出口,则输出-1。
Sample Input
3 3

.0.
.#.

5 5
.0.
.1*5*
.*.
…**
#**
Sample Output
1
11

hint:第二个样例里,ZZC学长先用2分钟拿到钥匙5,再用4分钟打开暗门1,最后用5分钟走到出口。

代码:

#include <iostream>#include <string>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <map>#define MST(s,q) memset(s,q,sizeof(s))using namespace std;int N, M;char mp[1005][1005];struct node{    int x, y, status, Time;    node() {};    node(int xx, int yy, int s, int t) {        x = xx;        y = yy;        status = s;        Time = t;    };} s, e;int move_x[4] = {1, -1, 0, 0}, move_y[4] = {0, 0, 1, -1};bool vis[20][1005][1005];int bfs(int sx, int sy){    queue<node> Q;    Q.push(node(sx, sy, 0, 0));    MST(vis, 0);    while (!Q.empty())    {        e = Q.front();        Q.pop();        for (int i = 0; i < 4; i++)        {            int x = e.x + move_x[i], y = e.y + move_y[i];            int ST = e.status, Time = e.Time;            if (x >= 0 && x < N && y >= 0 && y < M && mp[x][y] != '*' && !vis[ST][x][y])            {                if (mp[x][y] == '.' || mp[x][y] == '0')  //  road                {                    vis[ST][x][y] = 1, Q.push(node(x, y, ST, Time + 1));                }                else if (mp[x][y] >= '1' && mp[x][y] <= '4') // find door                {                    int k = mp[x][y] - '1';                    int t = pow(2, k);                    if (!(ST & t)) continue;                    else                    {                        vis[ST][x][y] = 1;                        Q.push(node(x, y, ST, Time + 1));                    }                }                else if (mp[x][y] >= '5' && mp[x][y] <= '8') // find key                {                    int k = mp[x][y] - '5';                    int t = pow(2, k);                    ST |= t;                    vis[ST][x][y] = 1;                    Q.push(node(x, y, ST, Time + 1));                }                else if (mp[x][y] == '#')   //  out                    return e.Time + 1;            }        }    }    return -1;}int main(){    while (cin >> N >> M)    {        int ans = 0, sx, sy;        for (int i = 0; i < N; ++i)        {            scanf("%s", mp[i]);            for (int j = 0; j < M; j++)                if (mp[i][j] == '0')                    sx = i, sy = j;        }        cout << bfs(sx, sy) << endl;    }}
0 0
原创粉丝点击