2009
11.07

http://acm.pku.edu.cn/JudgeOnline/problem?id=2225

这道题是非常基础的广搜。没什么难度,只要注意下,起点,终点的坐标输入顺序是:”列,行,层”,其他的就什么了!

有点想不明白,把储存图的g数组开在了main里面就老是输出不了结果,耗了我n久

直接贴乱码了!

#include<stdio.h>
#include<string.h>
struct node
{
	int i,j,k,step;
}w[1000];
int dir[6][3]={{1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
int main()
{
	freopen("1.txt","r",stdin);
	int x,y,z;
	bool u[10][10][10];
	int n,i,j,k,si,sj,sk,ei,ej,ek;
	char a[10];
	int l,r;
	bool find;
	char g[10][10][10];
	while(scanf("%s",a)!=EOF)
	{
		find=false;
		scanf("%d ",&n);
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
			{
				for(k=0;k<n;k++)
				{
					scanf("%c",&g[i][j][k]);
				}
				getchar();
			}
		scanf("%d%d%d",&sk,&sj,&si);
		scanf("%d%d%d",&ek,&ej,&ei);
		scanf("%s",a);
		if(si==ei && sj==ej && sk==ek)
		{
			printf("%d 0\n",n);
			continue;
		}
		memset(u,true,sizeof(u));
		w[0].i=si;
		w[0].j=sj;
		w[0].k=sk;
		w[0].step=0;
		u[si][sj][sk]=false;
		l=0;
		r=1;
		while(l<r)
		{
			for(i=0;i<6;i++)
			{
				x=w[l].i+dir[i][0];
				y=w[l].j+dir[i][1];
				z=w[l].k+dir[i][2];
				if(x==ei && y==ej && z==ek)
				{
					find=true;
					break;
				}
				if(x>=0 &&y>=0 &&z>=0&&x<n&&y<n&&z<n&& u[x][y][z] && g[x][y][z]=='O')
				{
					w[r].i=x;
					w[r].j=y;
					w[r].k=z;
					w[r].step=w[l].step+1;
					u[x][y][z]=false;
					r++;
				}
			}
			if(find)
				break;
			l++;
		}
		if(find)
		{
			printf("%d %d\n",n,w[l].step+1);
		}
		else
			printf("NO ROUTE\n");
 
	}
	return 0;
}

暂无回复

添加回复