Asteroids! (hdu1240)
搜索
Problem Description
You’re in space.
You want to get home.
There are asteroids.
You don’t want to hit them.
题目直链
题解
简单的bfs搜索,多层迷宫类型的题目
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
const int maxn = 1e5+5;
struct node
{
int x,y,z,step;
};
int flag[15][15][15];
char mp[15][14][15];
int d[6][3] = {0,1,0,1,0,0,0,-1,0,-1,0,0,0,0,-1,0,0,1};
int n;
node ed,bg;
int bfs()
{
queue<node> q;
q.push(bg);
if(bg.x==ed.x&&bg.y==ed.y&&bg.z==ed.z)
{
return 0;
}
while(!q.empty())
{
node f = q.front();
node t;
q.pop();
for(int i=0;i<6;i++)
{
t.x = f.x + d[i][0];
t.y = f.y + d[i][1];
t.z = f.z + d[i][2];
t.step = f.step+1;
if(t.x>=0&&t.x<n&&t.y>=0&&t.y<n&&t.z>=0&&t.z<n&&flag[t.z][t.x][t.y]==0&&mp[t.z][t.x][t.y]=='O')
{
if(t.x==ed.x&&t.y==ed.y&&t.z==ed.z)
{
return t.step;
}
q.push(t);
flag[t.z][t.x][t.y] = 1;
}
}
}
return -1;
}
int main()
{
char s[105];
while (cin>>s>>n)
{
rep(k,0,n)
rep(i,0,n)
{
rep(j,0,n)
{
cin>>mp[k][i][j];
}
}
scanf("%d %d %d",&bg.x,&bg.y,&bg.z);
scanf("%d %d %d",&ed.x,&ed.y,&ed.z);
bg.step = 0;
flag[bg.z][bg.x][bg.y] = 1;
scanf(" %s",s);
int ans = bfs();
if(ans!=-1)
printf("%d %d\n",n,ans);
else
{
printf("NO ROUTE\n");
}
}
return 0;
}