hdu1240三维bfs最短路轻松1A

来源:互联网 发布:java文件夹的复制 编辑:程序博客网 时间:2024/06/16 13:15
#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <bitset>using namespace std ;#define zero {0}#define INF 2000000000#define EPS 1e-6typedef 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);        queue<Node>Q;        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;}
好开森
0 0
原创粉丝点击