您的位置:首页 > 其它

HDU 1240 Asteroids! 三维空间BFS

2016-02-24 23:15 351 查看
Asteroids!

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4481 Accepted Submission(s): 2891

Problem Description

You’re in space.

You want to get home.

There are asteroids.

You don’t want to hit them.

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

A single data set has 5 components:

Start line - A single line, “START N”, where 1 <= N <= 10.

Slice list - A series of N slices. Each slice is an N x N matrix representing a horizontal slice through the asteroid field. Each position in the matrix will be one of two values:

‘O’ - (the letter “oh”) Empty space

‘X’ - (upper-case) Asteroid present

Starting Position - A single line, “A B C”, denoting the

#include <iostream>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
struct node{
int x,y,z;
int step;
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
};
int N;
char edge[15][15][15];
int sta[15][15][15];
int sx,sy,sz,ex,ey,ez;
int dirx[6]={1,-1,0,0,0,0};
int diry[6]={0,0,1,-1,0,0};
int dirz[6]={0,0,0,0,1,-1};
int mina;
bool Judge(int x,int y,int z,int t)
{
if(x<0||x>=N||y<0||y>=N||z<0||z>=N||edge[x][y][z]=='X'||sta[x][y][z]<=t)
return true;
return false;

}
bool BFS()
{
priority_queue<node>Q;
node cur,next;
cur.x=sx;
cur.y=sy;
cur.z=sz;
cur.step=0;
sta[sx][sy][sz]=0;
Q.push(cur);
while(!Q.empty())
{
cur=Q.top();
Q.pop();
if(cur.x==ex&&cur.y==ey&&cur.z==ez)
{
mina=cur.step;
return true;
}
for(int i=0;i<6;i++)
{
next.x=cur.x+dirx[i];
next.y=cur.y+diry[i];
next.z=cur.z+dirz[i];
next.step=cur.step+1;
if(next.x==ex&&next.y==ey&&next.z==ez)
{Q.push(next);
continue;
}
if(Judge(next.x,next.y,next.z,next.step))
continue;
sta[next.x][next.y][next.z]=next.step;
Q.push(next);
}
}

return false;
}
int main(int argc, char *argv[])
{
string s1,s2;
while(cin>>s1)
{
//if(s1=="END")
//cout<<11<<endl;
//break;
cin>>N;
for(int k=0;k<N;k++)
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
cin>>edge[k][i][j];
cin>>sx>>sy>>sz;
cin>>ex>>ey>>ez;
cin>>s2;
if(sx==ex&&sy==ey&&sz==ez)
{
cout<<N<<" "<<0<<endl;
}
else
{
for(int k=0;k<N;k++)
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
sta[i][j][k]=999;
if(BFS())
{
cout<<N<<" "<<mina<<endl;
}
else
cout<<"NO ROUTE"<<endl;

}

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