您的位置:首页 > 编程语言

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: