魔戒

来源:互联网 发布:如何查看mac os版本 编辑:程序博客网 时间:2024/04/30 05:58

Problem Description

蓝色空间号和万有引力号进入了四维水洼,发现了四维物体--魔戒。

这里我们把飞船和魔戒都抽象为四维空间中的一个点,分别标为 "S" 和 "E"。空间中可能存在障碍物,标为 "#",其他为可以通过的位置。

现在他们想要尽快到达魔戒进行探索,你能帮他们算出最小时间是最少吗?我们认为飞船每秒只能沿某个坐标轴方向移动一个单位,且不能越出四维空间。

Input

输入数据有多组(数据组数不超过 30),到 EOF 结束。

每组输入 4 个数 x, y, z, w 代表四维空间的尺寸(1 <= x, y, z, w <= 30)。

接下来的空间地图输入按照 x, y, z, w 轴的顺序依次给出,你只要按照下面的坐标关系循环读入即可。

for 0, x-1

    for 0, y-1

        for 0, z-1

            for 0, w-1

保证 "S" 和 "E" 唯一。

Output

对于每组数据,输出一行,到达魔戒所需的最短时间。

如果无法到达,输出 "WTF"(不包括引号)。

Example Input

2 2 2 2...S..#.#..E.#..2 2 2 2...S#.##E..##...

Example Output

13
#include<stdio.h>#include<string.h>#include<stdlib.h>#define maxn 50struct node{    int a;    int b;    int c;    int d;    int s;}queue[900000];char str[maxn][maxn][maxn][maxn];int book[maxn][maxn][maxn][maxn];int next[8][4]={{0, 0, 0, 1}, {0, 0, 1, 0}, {0, 1, 0, 0}, {1, 0, 0, 0},{0, 0, 0, -1}, {0, 0, -1, 0}, {0, -1, 0, 0}, {-1, 0, 0, 0},};int x,y,z,w;int front,rear;void enter_q(struct node p){    rear++;    queue[rear]=p;}struct node delete_q(){    front++;    return queue[front];}int judge(struct node p){    int flag=0;    if(p.a>=0 && p.a<x&& p.b>=0 && p.b<y&& p.c>=0 && p.c<z&& p.d>=0 && p.d<w&& str[p.a][p.b][p.c][p.d]!='#'){    flag=1;}    return flag;}int bfs(){    int i;    struct node p,q;    while(front!=rear)    {        p=delete_q();        if(str[p.a][p.b][p.c][p.d]=='E')        {            return p.s;        }        for(i=0;i<8;i++)        {            q.a=p.a+next[i][0];            q.b=p.b+next[i][1];            q.c=p.c+next[i][2];            q.d=p.d+next[i][3];            if(judge(q)&&book[q.a][q.b][q.c][q.d]==0)            {                q.s=p.s+1;                enter_q(q);                book[q.a][q.b][q.c][q.d]=1;            }        }    }    return -1;}int main(){    int ans;    int i,j,u,v;    struct node f;    while(scanf("%d%d%d%d",&x,&y,&z,&w)!=EOF)    {        front=rear=0;        memset(book,0,sizeof(book));        for(i=0;i<x;i++)        {            for(j=0;j<y;j++)            {                for(u=0;u<z;u++)                {                    scanf("%s",str[i][j][u]);                    for(v=0;v<w;v++)                    {                        if(str[i][j][u][v]=='S')                        {                            f.a=i;                            f.b=j;                            f.c=u;                            f.d=v;                            f.s=0;                        }                    }                }            }        }        enter_q(f);        book[f.a][f.b][f.c][f.d]=1;        ans=bfs();        if(ans!=-1)        {            printf("%d\n",ans);        }        else        {            printf("WTF\n");        }    }    return 0;}