POJ 2243 Knight Moves

Knight Moves
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9346 Accepted: 5342

Description

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part. 

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

Input

The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

Output

For each test case, print one line saying "To get from xx to yy takes n knight moves.".

Sample Input

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

Sample Output

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.

 

直接简单BFS就可以过得题,练习下双向BFS

 

#include<cstdio>
#include<cstring>

using namespace std;

typedef struct Node{
	int x,y;
	int cent;
}; 

Node queue[2][100];
int hash[2][10][10];

int BFS(Node &star,Node &end)
{
	int base[2],top[2];
	int flag=0,i,j,k,tmp;
	int cc[8][2]={1,2,   1,-2,   -1,2,   -1,-2,   2,1,   2,-1,   -2,1,   -2,-1};

	if(star.x==end.x && star.y==end.y)
		return 0;

	star.cent=0;	end.cent=0;
	base[0]=base[1]=top[0]=top[1]=0;


	queue[0][0]=star;
	queue[1][0]=end;

	memset(hash,-1,sizeof(hash));;

	hash[0][star.x][star.y]=0;	hash[1][end.x][end.y]=0;

	while(base[flag]<=top[flag] || base[tmp]<=top[tmp])
	{
		tmp=(flag+1)%2;
		if(base[flag]>top[flag])
		{
			flag=tmp;
			continue;
		}

		for(i=0;i<8;i++)
		{
			int tmp_x=queue[flag][base[flag]].x+cc[i][0];
			int tmp_y=queue[flag][base[flag]].y+cc[i][1];

			if(tmp_x>0 && tmp_x<=8 && tmp_y>0 && tmp_y<=8 && hash[flag][tmp_x][tmp_y]==-1)
			{
				if(hash[tmp][tmp_x][tmp_y]!=-1)
					return queue[flag][base[flag]].cent+hash[tmp][tmp_x][tmp_y]+1;
				queue[flag][++top[flag]].x=tmp_x;
				queue[flag][top[flag]].y=tmp_y;
				queue[flag][top[flag]].cent=queue[flag][base[flag]].cent+1;
				hash[flag][tmp_x][tmp_y]=queue[flag][top[flag]].cent;
			}
		}

		base[flag]++;
		flag=tmp;
	}

}


int main()
{
	int A,B;
	char a,b;

	Node star,end;

	while(scanf("%c%d %c%d",&a,&A,&b,&B)==4)
	{
		star.x=a-'a'+1;	star.y=A;
		end.x=b-'a'+1;	end.y=B;

		printf("To get from %c%d to %c%d takes %d knight moves.\n",a,A,b,B,BFS(star,end));

		getchar();
	}

	return 0;
}


 

POJ1915是一道经典的搜索题目,也被称为“Knight Moves”。下面我将为您提供解题思路和解题步骤: 1. 题目描述 在8x8的国际象棋棋盘上,棋子“马”从初始位置出发,允许走日字形的移动。给定目标位置,求出从初始位置到目标位置最少需要几步。 2. 解题思路 这道题目可以采用广度优先搜索(BFS)算法解决。将初始位置加入队列中,然后依次处理队列中的每个位置,根据“马”的走法,生成下一步可以到达的位置,如果这个位置没有被访问过,则将其加入队列中,并记录到达这个位置的步数。 3. 解题步骤 具体的解题步骤如下: (1)将初始位置加入队列中,并记录步数为0。 (2)依次处理队列中的每个位置,生成下一步可以到达的位置。 (3)如果下一步可以到达的位置没有被访问过,则将其加入队列中,并记录到达这个位置的步数为当前位置的步数+1。 (4)重复步骤(2)和(3),直到队列为空。 (5)如果目标位置被访问过,则返回到达目标位置的步数,否则返回-1。 4. 注意事项 在实现BFS算法的过程中,需要注意以下几点: (1)需要记录每个位置是否被访问过,以避免重复访问。 (2)需要注意边界条件,防止数组越界。 (3)需要注意判断目标位置是否合法,即是否在棋盘范围内。 5. 总结 本题是一道经典的搜索题目,采用BFS算法可以求解出从初始位置到目标位置最少需要几步。在解题过程中,需要注意边界条件和目标位置是否合法等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值