zoj1940
来源:互联网 发布:魔域登陆器源码 编辑:程序博客网 时间:2024/06/04 19:24
链接:点击打开链接
题意:三维搜索‘S’为起点,‘E’为终点,求走出的最短时间
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <queue>using namespace std;int xx[]={1,-1,0,0,0,0}; //方向向量int yy[]={0,0,1,-1,0,0};int zz[]={0,0,0,0,1,-1};int p,q,r;char str[35][35][35];struct node{ int x,y,z,sum;};int bfs(node st,node en){ int i; queue<node>qu; node cur,temp; qu.push(st); while(qu.size()){ cur=qu.front();qu.pop(); if(cur.x==en.x&&cur.y==en.y&&cur.z==en.z) return cur.sum; //找到之后直接返回走的步数 for(i=0;i<6;i++){ temp.x=cur.x+xx[i]; temp.y=cur.y+yy[i]; temp.z=cur.z+zz[i]; temp.sum=cur.sum+1; if(temp.x>=0&&temp.x<p&&temp.y>=0&&temp.y<q&&temp.z>=0&&temp.z<r) if(str[temp.x][temp.y][temp.z]!='#'){ qu.push(temp); str[temp.x][temp.y][temp.z]='#'; //走过之后就变成'#',省掉一个数组记录 } //是否走过 } } return -1;}int main(){ int i,j,k,sumsum; node st,en; while(scanf("%d%d%d",&p,&q,&r)!=EOF&&(p||q||r)){ for(i=0;i<p;i++) for(j=0;j<q;j++) for(k=0;k<r;k++){ cin>>str[i][j][k]; if(str[i][j][k]=='S') st.x=i,st.y=j,st.z=k,st.sum=0; else if(str[i][j][k]=='E') en.x=i,en.y=j,en.z=k; } sumsum=bfs(st,en); if(sumsum==-1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",sumsum); } return 0;}
0 0
- zoj1940
- zoj1940-Dungeon Master
- BFS广度优先搜索(3)--poj2251(zoj1940)(基础题)
- ZOJ1940 POJ2251 Dungeon Master 求三维迷宫路径问题,广度优先搜索
- ZF2多级树形路由Route配置实例
- Unity3D ShaderLab<二>Shader属性
- UML类图简明教程
- 架构师之我见
- C++实现Miller-Rabin素数测试
- zoj1940
- The Number Off of FFF
- vlc播放流程分析
- UVA - 11995 - I Can Guess the Data Structure!(栈、队列、优先队列)
- Windows 服务
- Truck History 【POJ--1789】【普里姆&&克鲁斯卡尔】【邻接矩阵】
- POJ-1860 Currency Exchange
- 不是所有创业公司都适合拿风投
- Servlet的原理解析——我能看懂,相信你能