SDUT 3929

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"(不包括引号)。

Sample Input

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

Sample Output

1
3

不需要知道四维空间的存在形式,经过队友的启发后,明白你只需要从低维度的空间推上来就好了。
一维空间开一维数组;二维空间开二维数组,三维空间开三维数组,所以四维空间开四维数组就好了。
每高一个维度,需要注意方向数组的不同。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<math.h>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<stack>
 8 #include<deque>
 9 #include<iostream>
10 using namespace std;
11 typedef long long  LL;
12 struct spot{
13     int x;
14     int y;
15     int z;
16     int w;
17     LL step;
18 };
19 int a,b,c,d;
20 int con[31][31][31][31];
21 int vis[31][31][31][31];
22 int way[8][4]={0,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,0};
23 int judge(int aa,int bb,int cc,int dd)
24 {
25     if(aa>=0&&aa<a&&bb>=0&&bb<b&&cc>=0&&cc<c&&dd>=0&&dd<d&&vis[aa][bb][cc][dd]==0&&con[aa][bb][cc][dd]!='#')
26         return 1;
27     else
28         return 0;
29 
30 }
31 void BFS(int aa,int bb,int cc,int dd)
32 {
33     int i,p,j;
34     queue<struct spot> qq;
35     struct spot head,mid;
36     int flag=0;
37     head.x=aa;
38     head.y=bb;
39     head.z=cc;
40     head.w=dd;
41     head.step=0;
42     vis[aa][bb][cc][dd]=1;
43     qq.push(head);
44     while(!qq.empty())
45     {
46         mid=qq.front();
47         qq.pop();
48         if(con[mid.x][mid.y][mid.z][mid.w]=='E')
49         {
50             flag=1;
51             break;
52         }
53         for(i=0;i<8;i++)
54         {
55             struct spot now;
56             now=mid;
57             now.x+=way[i][0];
58             now.y+=way[i][1];
59             now.z+=way[i][2];
60             now.w+=way[i][3];
61             if(judge(now.x,now.y,now.z,now.w))
62             {
63                 now.step++;
64                 vis[now.x][now.y][now.z][now.w]=1;
65                 qq.push(now);
66             }
67         }
68     }
69     if(flag==1)
70         printf("%lld\n",mid.step);
71     else
72         printf("WTF\n");
73     return ;
74 }
75 int main()
76 {
77     int i,p,j,q;
78     int aa,bb,cc,dd;
79     while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
80     {
81         for(i=0;i<=a-1;i++)
82             for(j=0;j<=b-1;j++)
83                 for(p=0;p<=c-1;p++)
84                     for(q=0;q<=d-1;q++)
85                     {
86                         scanf(" %c",&con[i][j][p][q]);
87                         if(con[i][j][p][q]=='S')
88                         {
89                             aa=i;
90                             bb=j;
91                             cc=p;
92                             dd=q;
93                         }
94                     }
95         memset(vis,0,sizeof(vis));
96         BFS(aa,bb,cc,dd);
97     }
98     return 0;
99 }
View Code


猜你喜欢

转载自www.cnblogs.com/daybreaking/p/9350698.html
今日推荐