Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

why wa?(附代码)

Posted by hustar at 2008-11-07 22:30:56 on Problem 2225
#include <stdio.h>
#include <string.h>
char map[20][20][20], s[20];
int N, step[20][20][20];
int sx, sy, sz, anx, any, anz;
struct node
{
	int x, y, z;
}Q[100000];
int dx[]={1,-1,0,0,0,0}, dy[]={0,0,1,-1,0,0}, dz[]={0,0,0,0,1,-1}; 
int ok(int a, int b, int c)
{
	if(a>=0&&a<N&&b>=0&&b<N&&c>=0&&c<N)
		return 1;
	return 0;
}
void init()
{
	int i, j, k;
	scanf("%d%*c",&N);
	for(i=0;i<N;i++)
		for(j=0;j<N;j++)
		{
			for(k=0;k<N;k++)
				scanf("%c",&map[i][j][k]);
			getchar();
		}
		scanf("%d%d%d%d%d%d",&sy,&sx,&sz,&any,&anx,&anz);
		map[anx][any][anz]='D';
		scanf("%s",s);
}
void bfs()
{
	int cntx, cnty, cntz, nextx, nexty,nextz, ans, i, j, k, tail=0, head=0;
	for(i=0;i<N;i++)
		for(j=0;j<N;j++)
			for(k=0;k<N;k++)
				step[i][j][k]=0x7fffffff;
			cntx=sx;cnty=sy;cntz=sz;
			step[cntx][cnty][cntz]=0;
			ans=step[anx][any][anz];
			while(1)
			{
				for(i=0;i<6;i++)
				{
					nextx=cntx+dx[i];
					nexty=cnty+dy[i];
					nextz=cntz+dz[i];
					if(ok(nextx,nexty,nextz)&&map[nextx][nexty][nextz]!='X')
					{
						if(map[nextx][nexty][nextz]=='D')
						{
							if(step[cntx][cnty][cntz]+1<ans)
								ans=step[cntx][cnty][cntz]+1;
						}
					    else if(step[cntx][cnty][cntz]+1<step[nextx][nexty][nextz])
						{
							    step[nextx][nexty][nextz]=step[cntx][cnty][cntz]+1;
							    Q[++tail].x=nextx;
							    Q[tail].y=nexty;
							    Q[tail].z=nextz;
						}
					}
				}
				if(head==tail) break;
				else
				{
					cntx=Q[++head].x;
					cnty=Q[head].y;
					cntz=Q[head].z;
				}
			}
			if(ans==0x7fffffff)
				printf("NO ROUTE\n");
			else 
				printf("%d %d\n",N,ans);
}

int main()
{
	while(scanf("%s",s)!=EOF)
	{
		init();
		bfs();
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator