hdu1240 Asteroids

题目链接: hdu1240 ( Asteroids! )

/**
* hdu1240 Asteroids
* 三维bfs,不过6个方向而已
*/

#include <cstdio>
#include <queue>
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;

const int N = 12;
char mp[N][N][N];

struct node{int x, y, z, step;};
int dir[6][3] = {{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
int bfs()
{
    int sx, sy, sz;
    int ex, ey, ez;
    cin >> sx >> sy >> sz;
    cin >> ex >> ey >> ez;
    ++sx, ++sy, ++sz;
    ++ex, ++ey, ++ez;

    queue<node> Q;
    Q.push(node{sx, sy, sz, 0});
    mp[sx][sy][sz] = 'X';
    while (!Q.empty()) {
        node t = Q.front(); Q.pop();
        if (t.x == ex && t.y == ey && t.z == ez) return t.step;
        for (int i = 0; i < 6; ++i) {
            int tx = t.x+dir[i][0];
            int ty = t.y+dir[i][1];
            int tz = t.z+dir[i][2];
            if (mp[tx][ty][tz] == 'X') continue;
            Q.push(node{tx, ty, tz, t.step+1});
            mp[tx][ty][tz] = 'X';
        }
    }
    return -1;
}

int main()
{
    int n;
    while (~scanf("%*s%d", &n)) {
        memset(mp, 'X', sizeof mp);
        for (int k = 1; k <= n; ++k) {
            for (int j = 1; j <= n; ++j) {
                getchar();
                for (int i = 1; i <= n; ++i) {
                    mp[i][j][k] = getchar();
                }
            }
        }
        int ans = bfs();
        scanf("%*s");
        if (ans == -1) puts("NO ROUTE");
        else cout << n << ' ' << ans << endl;
    }
    return 0;
}

posted @ 2021-01-23 11:32  Zewbie  阅读(70)  评论(0)    收藏  举报