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

bfs副代码

Posted by chenxuan123456789 at 2012-07-28 10:57:39 on Problem 2243
#include <stdio.h>
#include <string.h>
#define N 9
int maze[N][N];
int sx,sy,ex,ey;
int head,tail;
typedef struct
{
	int x,y,time;
}point;
point p[1024];
int d[8][2]={{2,1},{2,-1},{-2,-1},{-2,1},{1,2},{1,-2},{-1,-2},{-1,2}};
int times;
void bfs()
{
	int i,tempx,tempy;
	head=0;
	tail=0;
	p[head].x=sx;
	p[head].y=sy;
	p[head++].time=0;
	while(tail<=head)
	{
		if(p[tail].x==ex&&p[tail].y==ey)
		{
			times=p[tail].time;
			return;
		}
		else
		{
			for(i=0;i<8;i++)
			{
				tempx=p[tail].x+d[i][0];
				tempy=p[tail].y+d[i][1];
				if(tempx>=1&&tempy<=8&&tempy>=1&&tempy<=8&&!maze[tempx][tempy])
				{
					maze[tempx][tempy]=1;
					p[head].x=tempx;
					p[head].y=tempy;
					p[head++].time=p[tail].time+1;
				}
			}
		}
		tail++;
	}
}
int main()
{
	char e1,e2;
	int s1,s2;
	while(scanf("%c%d %c%d",&e1,&s1,&e2,&s2)!=EOF)
	{
		getchar();
		memset(maze,0,sizeof(maze));
		sx=s1;
		sy=e1-'a'+1;
		ex=s2;
		ey=e2-'a'+1;
		maze[sx][sy]=1;
		bfs();
		printf("To get from %c%d to %c%d takes %d knight moves.\n",e1,s1,e2,s2,times);
	}
	return 1;
}

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