您的位置:首页 > 其它

POJ - 2251 Dungeon Master

2016-07-25 19:56 387 查看

1.题面

http://poj.org/problem?id=2251

2.题意

没题意

3.思路

bfs搜索

4.代码

/*****************************************************************
> File Name: tst.cpp
> Author: Uncle_Sugar
> Mail: uncle_sugar@qq.com
> Created Time: 2016年07月25日 星期一 18时43分23秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

template<class T>void PrintArray(T* first,T* last,char delim=' '){
for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim);
}

const int debug = 1;
const int size  = 10 + 30;
const int INF = INT_MAX>>1;
typedef long long ll;

char g[size][size][size];
int vis[size][size][size];
int l,r,c;

int dx[6] = {0,0,0,0,1,-1};
int dy[6] = {0,0,1,-1,0,0};
int dz[6] = {1,-1,0,0,0,0};

struct Coord{
int x,y,z;
Coord(){}
Coord(int _x,int _y,int _z):x(_x),y(_y),z(_z){}
}coord[size];

Coord s,e;

bool inrange(int x,int y,int z){
return x<l&&x>=0&&y<r&&y>=0&&z<c&&z>=0;
}

int main()
{
std::ios::sync_with_stdio(false);cin.tie(0);
int i,j,k;
queue<Coord> que;
while (cin >> l >> r >> c){
if (l==0&&r==0&&c==0) break;
while (!que.empty()) que.pop();
memset(vis,-1,sizeof(vis));
for (i=0;i<l;i++){
for (j=0;j<r;j++){
cin >> g[i][j];
for (k=0;k<c;k++){
if (g[i][j][k]=='S'){
s = Coord(i,j,k);
g[i][j][k] = '.';
}
if (g[i][j][k]=='E'){
e = Coord(i,j,k);
g[i][j][k] = '.';
}
}
}
}
que.push(s);
vis[s.x][s.y][s.z] = 0;
while (!que.empty()){
Coord T = que.front();que.pop();
for (i=0;i<6;i++){
int nx = T.x + dx[i];
int ny = T.y + dy[i];
int nz = T.z + dz[i];
if (inrange(nx,ny,nz)&&g[nx][ny][nz]!='#'&&vis[nx][ny][nz]==-1){
vis[nx][ny][nz] = vis[T.x][T.y][T.z] + 1;
que.push(Coord(nx,ny,nz));
}
}
if (vis[e.x][e.y][e.z]!=-1) break;
}
if (vis[e.x][e.y][e.z]!=-1)
cout << "Escaped in " << vis[e.x][e.y][e.z] << " minute(s)." << endl;
else
cout << "Trapped!" << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: