| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
虽然无数次提问从未有人回答,但是我就当自己跟自己说话好了,bfs, 居然还tle#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
int Dungeon[50][50][50];
int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},
{0,1,0},{0,0,-1},{0,0,1}};
int px, py, pz;
struct Point
{
int x, y, z;
int ans;
};
Point s,e;
void bfs()
{
queue<Point> q ;
q.push(s);
while(!q.empty())
{
Point start = q.front();
q.pop();
if(start.x == e.x && start.y == e.y && start.z == e.z)
{
cout<<"Escaped in "<<start.ans<<" minute(s)."<<endl;
return;
}
for(int i=0; i<6; i++)
{
int startx = start.x + dir[i][0];
int starty = start.y + dir[i][1];
int startz = start.z + dir[i][2];
if(0<=startx && px > startx && 0<=starty && py>starty && 0 <= startz && pz > startz
&& Dungeon[startx][starty][startz] == 1)
{
Point temp;
temp.x = startx;
temp.y = starty;
temp.z = startz;
temp.ans = start.ans + 1;
q.push(temp);
Dungeon[start.x][start.y][start.z] = 0;
}
}
}
cout<<"Trapped!"<<endl;
return;
}
int main()
{
while(true)
{
cin>>px>>py>>pz;
if (px == 0 && py ==0 && pz == 0) break;
for(int i=0; i<px; i++)
{
for(int j=0; j<py; j++)
{
for(int k=0; k<pz; k++)
{
char sl;
cin>>sl;
if(sl=='#')
Dungeon[i][j][k] = 0;
if(sl=='.')
Dungeon[i][j][k] = 1;
if(sl=='S')
{
Dungeon[i][j][k] = 1;
s.x = i;
s.y = j;
s.z = k;
s.ans = 0;
}
if(sl=='E')
{
Dungeon[i][j][k] = 1;
e.x = i;
e.y = j;
e.z = k;
}
}
}
}
bfs();
}
return 0;
}
居然会超时!!求解释!!!
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator