SDUT 3929 校赛 D 魔戒

xiaoxiao2021-02-28  132

魔戒

Time Limit: 1000MS  Memory Limit: 65536KB Submit  Statistic  Discuss

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

1 3  解题思路    自己也是第一次遇见这种四维的题目,刚开始还不知道怎么去bfs,然后只好一点点的在纸上写下来坐标去模拟    发现样例一                       x  y   z  w    S的坐标是 0  0  1  1    E的坐标是 1  0  1  1    然后发现S到达E是在x那里加了1,然后就是一个变形的bfs,就是要探索八个方向(x,y,z,w各两个)     有了这个思路基本上这个题就快出来了      #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<cmath> using namespace std; char dp[30][30][30][30]; int vis[30][30][30][30]; int x,y,z,w; int a[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}; struct po { int a; int b; int c; int d; int step; }; int check(po k) { if(k.a>=x||k.a<0||k.b>=y||k.b<0||k.c>=z||k.c<0||k.d>=w||k.d<0||dp[k.a][k.b][k.c][k.d]=='#'||vis[k.a][k.b][k.c][k.d]==1) return 0; return 1; } void dfs(po be ) { int kill=1; be.step=0; queue<po>q; q.push(be); vis[be.a][be.b][be.c][be.d]=1; while(!q.empty()) { po pos=q.front(); q.pop(); if(dp[pos.a][pos.b][pos.c][pos.d]=='E') { kill=0; printf("%d\n",pos.step); break; } po tt; for(int i=0;i<8;i++) { tt.step=pos.step; tt.a=pos.a+a[i][0]; tt.b=pos.b+a[i][1]; tt.c=pos.c+a[i][2]; tt.d=pos.d+a[i][3]; if(check(tt)) { tt.step++; vis[tt.a][tt.b][tt.c][tt.d]=1; q.push(tt); } } } if(kill) printf("WTF\n"); } int main() { while(scanf("%d%d%d%d",&x,&y,&z,&w)!=EOF) { getchar(); po st,en; memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); for(int i=0;i<x;i++) { for(int j=0;j<y;j++) { for(int k=0;k<z;k++) { for(int q=0;q<w;q++) { scanf("%c",&dp[i][j][k][q]); if(dp[i][j][k][q]=='S') { st.a=i,st.b=j,st.c=k,st.d=q; } } getchar(); } } } dfs(st); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-21586.html

最新回复(0)