hdu1240 Asteroids!(三维bfs水)

天啦哭一道超水的题居然被我看错输入顺序WA到现在,差点连死的心都有了。。。

总之A了就好,A了就好,A了就好。。。。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stdlib.h>

using namespace std;

const int N = 30;
const int INF = 1000000;

char Map[N][N][N];
int visit[N][N][N];
int n;

struct Poi
{
    int x, y, z;
    int t;
};

int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}};

int go(int x0, int y0, int z0)
{
    if(x0 >= 0 && x0 < n && y0 >= 0 && y0 < n && z0 >= 0 && z0 < n && Map[x0][y0][z0] == 'O') return 1;
    else return 0;
}

int BFS(int stx, int sty, int stz, int edx, int edy, int edz)
{
    Poi st, ed;
    queue <Poi> q;
    st.x = stx;
    st.y = sty;
    st.z = stz;
    st.t = 0;
    q.push(st);
    memset(visit, 0, sizeof(visit));
    visit[stx][sty][stz] = 1;
    while(!q.empty())
    {
        st = q.front();
        q.pop();
        if(st.x == edx && st.y == edy && st.z == edz) return st.t;
        for(int i = 0; i < 6; i ++)
        {
            ed.x = st.x + dir[i][0];
            ed.y = st.y + dir[i][1];
            ed.z = st.z + dir[i][2];
            if(go(ed.x, ed.y, ed.z) && !visit[ed.x][ed.y][ed.z])
            {
                visit[ed.x][ed.y][ed.z] = 1;
                ed.t = st.t + 1;
                q.push(ed);
            }
        }
    }
    return -1;
}

int main()
{
   // freopen("in.txt", "r", stdin);
    int i, j, ans, stx, sty, stz, edx, edy, edz;
    char s[20];
    while(~scanf("%s %d", s, &n))
    {
        for(i = 0; i < n; i ++)
            for(j = 0; j < n; j ++)
                scanf("%s", Map[i][j]);
        scanf("%d%d%d%d%d%d", &stz, &sty, &stx, &edz, &edy, &edx);
        scanf("%s", s);
        ans = BFS(stx, sty, stz, edx, edy, edz);
        if(ans == -1) printf("NO ROUTE\n");
        else printf("%d %d\n", n, ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值