hdu1240三维bfs最短路轻松1A

#define DeBUG
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
struct Node
{
    int x, y, z;
    int step;
    Node(int a,int b,int c,int s)
    {
        x=a;
        y=b;
        z=c;
        step=s;
    }
};
int dir[6][3] = {0, 0, 1, 0, 0, -1, 0, 1, 0, 0, -1, 0, 1, 0, 0, -1, 0, 0};
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    char start[10];
    char end[10];
    int n;
    while (scanf("%s%d", start, &n) + 1)
    {
        char mp[20][20][20] = zero;
        bool vis[20][20][20] = zero;
        for (int z = 0; z < n; z++)
        {
            for (int i = 0; i < n; i++)
            {
                scanf("%s", mp[z][i]);
            }
        }
        int sx, sy, sz;
        int ex, ey, ez;
        scanf("%d%d%d", &sx, &sy, &sz);
        scanf("%d%d%d", &ex, &ey, &ez);
        scanf("%s", end);

        queueQ;
        Node node(sx,sy,sz,0);
        Q.push(node);
        bool flag=false;
        while(!Q.empty())
        {
            node=Q.front();
            Q.pop();

            if(node.x==ex&&node.y==ey&&node.z==ez)
            {
                flag=true;
                break;
            }
            int x,y,z;
            for(int i=0;i<6;i++)
            {
                x=node.x+dir[i][0];
                y=node.y+dir[i][1];
                z=node.z+dir[i][2];
                if(x<0||y<0||z<0||x>=n||y>=n||z>=n||vis[z][y][x]||mp[z][y][x]=='X')
                    continue;
                Node now(x,y,z,node.step+1);
                vis[z][y][x]=true;
                Q.push(now);
            }
        }
        if(flag)
            printf("%d %d\n",n,node.step);
        else
            printf("NO ROUTE\n");

    }

    return 0;
}
好开森

你可能感兴趣的:(ACM-搜索)