标签:acm 三维bfs asteroids hdu1240
Total Submission(s): 3161 Accepted Submission(s): 2108
START 1 O 0 0 0 0 0 0 END START 3 XXX XXX XXX OOO OOO OOO XXX XXX XXX 0 0 1 2 2 1 END START 5 OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO XXXXX XXXXX XXXXX XXXXX XXXXX OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO 0 0 0 4 4 4 END
1 0 3 4 NO ROUTE
/**************************************
***************************************
* Author:Tree *
*From :http://blog.csdn.net/lttree *
* Title : Asteroids! *
*Source: hdu 1240 *
* Hint : 三维BFS *
***************************************
**************************************/
#include <stdio.h>
#include <string>
#include <string.h>
#include <queue>
#include <iostream>
using namespace std;
int f_x,f_y,f_z,n;
bool ispos;
struct Coor
{
int x,y,z,step;
};
char mapp[51][51][51];
int dis[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
bool vis[51][51][51];
bool judge(int x,int y,int z)
{
if(x<0 || y<0 || z<0 || x>=n || y>=n || z>=n) return 0;
if(vis[x][y][z]==1 || mapp[x][y][z]==‘X‘) return 0;
return 1;
}
int bfs(int x,int y,int z)
{
queue <Coor> q;
Coor pre,next;
int i;
pre.x=x;
pre.y=y;
pre.z=z;
pre.step=0;
vis[x][y][z]=1;
q.push(pre);
while(!q.empty())
{
pre=q.front();
q.pop();
if(pre.x==f_x && pre.y==f_y && pre.z==f_z) {ispos=1;return pre.step;}
for(i=0;i<6;++i)
{
next.x=pre.x+dis[i][0];
next.y=pre.y+dis[i][1];
next.z=pre.z+dis[i][2];
if(judge(next.x,next.y,next.z))
{
next.step=pre.step+1;
vis[next.x][next.y][next.z]=1;
q.push(next);
}
}
}
return -1;
}
int main()
{
string str;
int i,j,k,rout;
int s_x,s_y,s_z;
while(cin>>str>>n)
{
for(i=0;i<n;++i)
for(j=0;j<n;++j)
{
// 把回车忽略掉
scanf("%*");
for(k=0;k<n;++k)
scanf("%c",&mapp[j][k][i]);
}
scanf("%d%d%d%d%d%d",&s_x,&s_y,&s_z,&f_x,&f_y,&f_z);
cin>>str;
memset(vis,0,sizeof(vis));
ispos=0;
rout=bfs(s_x,s_y,s_z);
if(ispos) printf("%d %d\n",n,rout);
else printf("NO ROUTE\n");
}
return 0;
}
标签:acm 三维bfs asteroids hdu1240
原文地址:http://blog.csdn.net/lttree/article/details/24794997