您的位置:首页 > 其它

POJ 2251.Dungeon Master

2017-11-16 11:42 246 查看
题目:http://poj.org/problem?id=2251

AC代码(C++):

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <queue>
#include <math.h>
#include <string>
#include <string.h>
#include <bitset>

#define INF 0xfffffff
#define MAXN 35

using namespace std;

int l,r,c;
char map[MAXN][MAXN][MAXN];
bool vis[MAXN][MAXN][MAXN];
int dir[][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0
c912
,-1}};

struct point{
int x,y,z;
int step;
};

int bfs(int x, int y, int z) {
queue<point> q;

point start;
start.x = x;
start.y = y;
start.z = z;
start.step = 0;
vis[x][y][z] = true;

q.push(start);

point node;
while (!q.empty()) {
node = q.front();
q.pop();
for(int i = 0; i < 6; i++){
point tmp;
tmp.x = node.x + dir[i][0];
tmp.y = node.y + dir[i][1];
tmp.z = node.z + dir[i][2];
if (map[tmp.x][tmp.y][tmp.z]!='#'&&vis[tmp.x][tmp.y][tmp.z]==false) {
if(map[tmp.x][tmp.y][tmp.z]=='E')return node.step+1;
tmp.step = node.step + 1;
q.push(tmp);
vis[tmp.x][tmp.y][tmp.z] = true;
}
}
}
return -1;
}

int main(){
while(cin>>l>>r>>c){
getchar();
if(l==0)break;
int sx, sy, sz;
for(int i = 0; i < l+2; i++)
for(int j = 0; j < r+2; j++)
for(int k = 0; k < c+2; k++)
map[i][j][k] = '#';
for(int i = 1; i <= l; i++){
for(int j = 1; j <= r; j++){
for(int k = 1; k <= c; k++){
scanf("%c",&map[i][j][k]);
if(map[i][j][k]=='S'){
sx = i;
sy = j;
sz = k;
map[i][j][k] = '.';
}
}
getchar();
}
getchar();
}
memset(vis,false,sizeof(vis));
int ans = bfs(sx,sy,sz);
if(ans!=-1)cout<<"Escaped in "<<ans<<" minute(s).\n";
else cout<<"Trapped!\n";
}
}

总结: 入门级的广搜题.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: