poj2251的代码又改了改~
2016-04-01 20:35
399 查看
#include<iostream> using namespace std; #include<queue> #include<cstring> #include<string> char temp; bool vis[50][50][50]; bool vist[50][50][50]; const int dx[6] = {1,-1,0,0,0,0}; const int dy[6] = {0,0,1,-1,0,0}; const int dz[6] = {0,0,0,0,-1,1}; struct node { int x,y,z; int step; }; node now; node S,T; int bfs() { memset(vis,false,sizeof(vis)); queue<node>q; q.push(S); while(!q.empty()) { now = q.front(); q.pop(); for(int i=0; i<6; i++) { node New; New.x = now.x + dx[i]; New.y = now.y + dy[i]; New.z = now.z + dz[i]; New.step = now.step + 1; if(!vist[New.x][New.y][New.z] || vis[New.x][New.y][New.z]) continue; q.push(New); vis[New.x][New.y][New.z] = true; if(New.x == T.x && New.y == T.y && New.z == T.z) return New.step; } } return -1; } int main() { int L, R, C; while(cin >> L >> R >> C && L) { memset(vist,false,sizeof(vist)); for(int i=1; i<=L; i++) { for(int j=1; j<=R; j++) { for(int k=1; k<=C; k++) { cin >> temp; if(temp == 'S') { vist[i][j][k] = true; S.x = i; S.y = j; S.z = k; S.step = 0; } if(temp == 'E') { vist[i][j][k] = true; T.x = i; T.y = j; T.z = k; } if(temp == '.') { vist[i][j][k] = true; } } } } int ans = bfs(); if (ans<0) cout <<"Trapped!" << endl; else cout << "Escaped in " <<ans << " minute(s)." << endl; } return 0; }
相关文章推荐
- java异常处理的学习笔记
- 【深入spring】IoC容器的实现
- java之文件
- C++上机练习
- C#-get/set和方法重载
- php如何实现上一页下一页的功能 (不是列表页 是内容页)
- 回归基础系列-快速排序[JAVA]
- 正则大全
- C++指针【数组、字符串作为函数的参数】
- 回归基础系列-选择排序[JAVA]
- java中getResourceAsStream(String path)与getClassLoader.getResourceAsStream(String path)
- 回归基础系列-插入排序[JAVA]
- 回归基础系列-冒泡排序-[JAVA]
- lua5.3源码基础阅读(luaL_openlibs)
- 信息管理代码分析<二>读取二进制文件数据
- 运算符重载
- C++泛型编程总结(一)
- GEEK学习笔记— —C++11的新特性
- JavaEE_4_1_jsp的四个内置对象作用域
- Java使用SSLSocket通信