这道题非常无耻的一个地方:输入地图的坐标顺序和输入目的地的坐标顺序相反。卡了我好长时间,一直想不明白哪儿错了。
还是广搜。今天下午做题的时候,有一个题看到题目就知道用哪种算法需要做出那些改动,一目了然。就是算法怎么写忘了,蛋疼。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char map[10][10][10];
int n;
int dir[6][3]={0,0,1, 0,0,-1, 0,1,0, 0,-1,0, 1,0,0, -1,0,0};
int a,b,c,x,y,z;
struct node
{
int x,y,z;
int step;
};
int judge(int x,int y,int z)
{
if(x>=0&&x<n&&y>=0&&y<n&&z>=0&&z<n&&map[x][y][z]=='O')
return 1;
return -1;
}
int bfs()
{
int i;
int a1,a2,a3;
queue<node>q;
node cur,next;
cur.x=a;
cur.y=b;
cur.z=c;
cur.step=0;
map[a][b][c]='X';
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
next.step=cur.step+1;
for(i=0;i<6;i++)
{
next.x=a1=cur.x+dir[i][0];
next.y=a2=cur.y+dir[i][1];
next.z=a3=cur.z+dir[i][2];
if(judge(a1,a2,a3)==1)
{
map[a1][a2][a3]='X';
if(a1==x&&a2==y&&a3==z)
return next.step;
q.push(next);
}
}
}
return -1;
}
int main()
{
int i,j;
int ans;
char s[105];
while(scanf("START %d",&n)!=EOF)
{
getchar();
memset(map,0,sizeof(map));
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%s",map[i][j]);
}
getchar();
}
scanf("%d%d%d",&c,&b,&a);
scanf("%d%d%d",&z,&y,&x);
getchar();
gets(s);
if(map[x][y][z]=='X')
ans=-1;
if(x==a&&y==b&&z==c)
{
printf("%d 0\n",n);
continue;
}
ans=bfs();
if(ans==-1)
printf("NO ROUTE\n");
else
printf("%d %d\n",n,ans);
}
return 0;
}