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
题解:刚开始看到四维整个人都懵掉了,还尝试画图构造(哭.....),后来发现“飞船每秒只能沿某个坐标轴方向移动一个单位”,仔细想想不就是一个深搜哦,就是一个模板题的变形。
代码如下:
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 10007
#define N 100005
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
using namespace std;
typedef long long ll;
int x,y,z,w;
char a[33][33][33][33];
int vis[33][33][33][33];
int dis[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 node
{
int a,b,c,d,s;
};
int judge(node pl)
{
if(pl.a>=0&&pl.a<x&&pl.b>=0&&pl.b<y&&pl.c>=0&&pl.c<z&&pl.d>=0&&pl.d<w&&(!vis[pl.a][pl.b][pl.c][pl.d])&&a[pl.a][pl.b][pl.c][pl.d]!='#')
return 1;
return 0;
}
void bfs(node bb)
{
int flag=0;
queue<node>q;
q.push(bb);
vis[bb.a][bb.b][bb.c][bb.d]=1;
while(!q.empty())
{
node lp=q.front();
q.pop();
if(a[lp.a][lp.b][lp.c][lp.d]=='E')
{
flag=1;
cout<<lp.s<<endl;
break;
}
node tt;
for(int i=0; i<8; i++)
{
tt.a=lp.a+dis[i][0];
tt.b=lp.b+dis[i][1];
tt.c=lp.c+dis[i][2];
tt.d=lp.d+dis[i][3];
tt.s=lp.s;
if(judge(tt))
{
tt.s++;
vis[tt.a][tt.b][tt.c][tt.d]=1;
q.push(tt);
}
}
}
if(!flag)
{
cout<<"WTF"<<endl;
}
}
int main()
{
while(cin>>x>>y>>z>>w)
{
node qaq;
memset(a,0,sizeof(a));
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 l=0; l<w; l++)
{
cin>>a[i][j][k][l];
if(a[i][j][k][l]=='S')
{
qaq.a=i;
qaq.b=j;
qaq.c=k;
qaq.d=l;
qaq.s=0;
}
}
bfs(qaq);
}
return 0;
}