您的位置:首页 > 其它

HDU 1240 Asteroids!

2012-01-15 20:31 239 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1240

bfs

我的代码

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N=15;
const int dx[6]={1,0,0,-1,0,0};
const int dy[6]={0,1,0,0,-1,0};
const int dz[6]={0,0,1,0,0,-1};
struct coor
{
int x,y,z;
};
queue<coor> q;
char maze

;
int vis

,dis

;
int n,sx,sy,sz,ex,ey,ez;
int bfs(int x,int y,int z)
{
if (x==ex && y==ey && z==ez) return 0;
while (!q.empty()) q.pop();
vis[x][y][z]=1; dis[x][y][z]=0;
coor u={x,y,z};
q.push(u);
int nx,ny,nz,d;
while (!q.empty())
{
u=q.front(); q.pop();
x=u.x; y=u.y; z=u.z;
for (d=0;d<6;d++)
{
nx=x+dx[d]; ny=y+dy[d]; nz=z+dz[d];
if (nx<0 || nx>=n || ny<0 || ny>=n || nz<0 || nz>=n) continue;
if (vis[nx][ny][nz] || maze[nx][ny][nz]=='X') continue;
vis[nx][ny][nz]=1;
dis[nx][ny][nz]=dis[x][y][z]+1;
if (nx==ex && ny==ey && nz==ez) return dis[nx][ny][nz];
coor v={nx,ny,nz};
q.push(v);
}
}
return -1;
}
int main()
{
char temp[10];
int x,y,z,ans;
while (~scanf("%s%d",temp,&n))
{
getchar();
for (z=0;z<n;z++)
for (x=0;x<n;x++)
{
for (y=0;y<n;y++) maze[x][y][z]=getchar();
getchar();
}
scanf("%d%d%d%d%d%d",&sx,&sy,&sz,&ex,&ey,&ez);
scanf("%s",temp);
ans=bfs(sx,sy,sz);
if (ans<0) printf("NO ROUTE\n");
else printf("%d %d\n",n,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: