魔戒
来源:互联网 发布:如何查看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;}
阅读全文
0 0
- 魔戒
- 魔戒
- 魔戒
- 魔戒
- 魔戒
- 魔戒咕噜
- 看到魔戒的花絮
- 魔戒中超眩的武器装备!
- sdut 3929 魔戒
- SDUT 3929 魔戒
- 魔戒(四维bfs)
- 魔戒-BFS求最短路
- SDUT 3929 校赛 D 魔戒
- 第八届ACM校赛->魔戒
- 魔戒 4维bfs搜索
- 冲杀职场的10只魔戒
- 冲杀职场的10只魔戒
- 冲杀职场的10只魔戒
- tf.nn.conv2d 参数含义及结果维度
- tensorflow 多线程操作文件中关于文件名命名的思考
- redis自启动
- MyBatis--查询缓存
- java 检测代码运行时间
- 魔戒
- 支付宝java sdk jar和源代码包上传到本地仓库
- 机器学习-->集成学习-->BGM调参
- Ubuntu16.4构建交叉编译环境
- ubuntu提示opengl版本过低-Gallium0.4 on llvmpipe(llvm 3.8 128bits)
- 磁盘快速读写简析
- SpringBoot 定时任务
- 1009 Enigma
- 计蒜客 百度地图导航