POJ 1915 Knight Moves

Knight Moves
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 18909 Accepted: 8665

Description

Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.

Input

The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.

Output

For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.

Sample Input

3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1

Sample Output

5
28
0

Source

TUD Programming Contest 2001, Darmstadt, Germany
解题思路:经典的骑士游历问题,很多人双广搜过的,不过我的单广搜跑了300+ms,也过了。。。
#include<iostream>
#include<queue>
using namespace std;
int n,xa,xb,ya,yb;
long long depth[1000000],Count,s;
bool visit[305][305];
int bfs()
	//s用来记录第i步有多少种走法,并记录到depth数组中,Count记录在可走的s步中已经走了多少步
	//全部走完的话,s和Count清空为0重新计数,步数i+1
{
	int a,b,a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8,i=1;
	queue<int> x;
	queue<int> y;
	x.push(xa);
	y.push(ya);
	while(!x.empty()&&!y.empty())
	{
		if(Count==depth[i-1])
		{
			i++;
			Count=s=0;
		}
		a=x.front();
		b=y.front();
		x.pop();y.pop();
		if(a==xb&&b==yb)
			break;
		a1=a-2;b1=b-1;
		if(a1>=0&&a1<n&&b1>=0&&b1<n&&!visit[a1][b1])
		{
			visit[a1][b1]=true;
			x.push(a1);
			y.push(b1);
			s++;
		}
		a2=a-1;b2=b-2;
		if(a2>=0&&a2<n&&b2>=0&&b2<n&&!visit[a2][b2])
		{
			visit[a2][b2]=true;
			x.push(a2);
			y.push(b2);
			s++;
		}
		a3=a+1;b3=b-2;
		if(a3>=0&&a3<n&&b3>=0&&b3<n&&!visit[a3][b3])
		{
			visit[a3][b3]=true;
			x.push(a3);
			y.push(b3);
			s++;
		}
		a4=a+2;b4=b-1;
		if(a4>=0&&a4<n&&b4>=0&&b4<n&&!visit[a4][b4])
		{
			visit[a4][b4]=true;
			x.push(a4);
			y.push(b4);
			s++;
		}
		a5=a+2;b5=b+1;
		if(a5>=0&&a5<n&&b5>=0&&b5<n&&!visit[a5][b5])
		{
			visit[a5][b5]=true;
			x.push(a5);
			y.push(b5);
			s++;
		}
		a6=a+1;b6=b+2;
		if(a6>=0&&a6<n&&b6>=0&&b6<n&&!visit[a6][b6])
		{
			visit[a6][b6]=true;
			x.push(a6);
			y.push(b6);
			s++;
		}
		a7=a-1;b7=b+2;
		if(a7>=0&&a7<n&&b7>=0&&b7<n&&!visit[a7][b7])
		{
			visit[a7][b7]=true;
			x.push(a7);
			y.push(b7);
			s++;
		}
		a8=a-2;b8=b+1;
		if(a1>=0&&a1<n&&b1>=0&&b1<n&&!visit[a8][b8])
		{
			visit[a8][b8]=true;
			x.push(a8);
			y.push(b8);
			s++;
		}
		depth[i]=s;
		Count++;
	}
		return i-1;
}
int main()
{
	int t,ans;
	cin>>t;
	while(t--)
	{
		cin>>n>>xa>>ya>>xb>>yb;
		memset(visit,false,sizeof(visit));
		memset(depth,0,sizeof(depth));
		visit[xa][ya]=true;
		depth[0]=1;
		Count=s=0;
		ans=bfs();
		cout<<ans<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值