zoukankan      html  css  js  c++  java
  • hdu 1240 Asteroids! 三维 BFS

    N表示三维图的大小(x,y,z为零到N-1),然后是一个三维图,最后是起点和终点的座标。
    答案输出N和从起点到终点走的步数。
    和hdu1253差不多的题目,并且本题不用剪技,直接BFS搜一遍就可以了。
     
     

    #include<stdio.h>
    #include<stdlib.h>
    #include<queue>
    #include<string.h>
    #include <iostream>
    using namespace std;
    char map[11][11][11];
    int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
    int visited[11][11][11];
    int N;
    struct coor
    {
        int x,y,z;
        int step;
    }s[2];
    bool judge(int x,int y,int z)
    {
        if(x<0||y<0||z<0||x>=N||y>=N||z>=N||map[x][y][z]=='X'||visited[x][y][z])
        return false;
        return true;
    }
    queue<coor>que;
    bool bfs()
    {
        int i,x,y,z;
        coor in,out;
        memset(visited,0,sizeof(visited));
        while(!que.empty())
        que.pop();
        in.x=s[0].x;
        in.y=s[0].y;
        in.z=s[0].z;
        in.step=0;
        que.push(in);
        while(!que.empty())
        {
            out=que.front();
            que.pop();
            if(out.x==s[1].x&&out.y==s[1].y&&out.z==s[1].z)
            {
                printf("%d %d\n",N,out.step);
                return true;
            }
            for(i=0;i<6;i++)
            {
                x=out.x+dir[i][0];
                y=out.y+dir[i][1];
                z=out.z+dir[i][2];
                if(!judge(x,y,z))
                continue;
                visited[x][y][z]=1;
                in.x=x;
                in.y=y;
                in.z=z;
                in.step=out.step+1;
                que.push(in);
            }
        }
        return false;
    }
               
       
       
    int main()
    {
        int i,j;
        char p[10],q[10];
        while(scanf("%s%d",p,&N)!=EOF)
        {
            scanf("%d",&N);
            for(i=0;i<N;i++)
            for(j=0;j<N;j++)
            scanf("%s",map[i][j]);
            for(i=0;i<2;i++)
            scanf("%d%d%d",&s[i].z,&s[i].y,&s[i].x);
            scanf("%s",q);
            if(!bfs())
            printf("NO ROUTE\n");
        }
        return 0;
    }
           

  • 相关阅读:
    Codeforces 1294E Obtain a Permutation
    Codeforces 1292C Xenon's Attack on the Gangs
    Codeforces 1292B Aroma's Search
    Codeforces 1288E Messenger Simulator
    Codeforces 1288D Minimax Problem
    Codeforces 1285E Delete a Segment
    P3368 【模板】树状数组 2 题解
    P3374 【模板】树状数组 1 题解
    P1494 [国家集训队]小Z的袜子 题解
    P2260 [清华集训2012]模积和 题解
  • 原文地址:https://www.cnblogs.com/zxj015/p/2740270.html
Copyright © 2011-2022 走看看