Knight Moves UVA - 439 (广搜 + 队列)

A friend of you is doing research on theTraveling Knight Problem (TKP)where you are to find theshortest closed tour of knight moves that visits each square of a given set ofnsquares on a chessboardexactly once. He thinks that the most difficult part of the problem is determining the smallest numberof knight moves between two given squares and that, once you have accomplished this, finding the tourwould 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 squaresaandbas input and then determines thenumber of knight moves on a shortest route fromatob.

Input

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

Output

For each test case, print one line saying ‘To get fromxxtoyytakesnknight 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.

题意:

求两个给定方块之间的最小骑士移动数

思路:
骑士可移动的方向有8个,和马走日方向是一样的,因为给出的方块表示有字母也有数字,所以将两个字符分开来写,骑士走过的路用book数组标记,剩下的就是套广搜模板了嘿哈!

代码1:(广搜)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct knight
{
	int x;
	int y;
	int s;
}k[100000];
int dir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};
int book[100][100];
char a[10],b[10];
int main()
{
	while(~scanf("%s %s",a,b))
	{
	    int i,tx,ty,flag=0,minn=0,head=1,tail=1;
	    memset(book,0,sizeof(book));
		k[head].x=a[0]-'a';
		k[head].y=a[1]-'1';
		k[head].s=0;
		book[k[head].x][k[head].y]=1;
		tail++;
		while(head<tail)
		{
			for(i=0;i<8;i++)
			{
				tx=k[head].x+dir[i][0];
				ty=k[head].y+dir[i][1];
				if(tx>=0&&tx<8&&ty>=0&&ty<8&&book[tx][ty]==0)
				{
					book[tx][ty]=1;
					k[tail].x=tx;
					k[tail].y=ty;
					k[tail].s=k[head].s+1;
					tail++;
				}
				if(k[tail-1].x==int(b[0]-'a')&&k[tail-1].y==int(b[1]-'1'))
				{
					flag=1;
					minn=k[tail-1].s;
					break;
				}
			}
			if(flag==1)
			break;
			head++;
		}
		printf("To get from %s to %s takes %d knight moves.\n",a,b,minn);
	}
	return 0;
}

下面的代码就是在广搜的基础上略加更改,用了queue队列,两个代码可以说是几乎一样的,用queue之前要先知道queue的用法,这个可以自己去了解一下。

代码2:(广搜+queue)

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
	int x;
	int y;
	int s;
};
//queue<node>q;
int dir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};
int book[100][100];
char a[10],b[10];
int main()
{
	while(~scanf("%s %s",a,b))
	{
	    queue<node>q;
	    //while(!q.empty())q.pop();当queue写在全局时需要加这个while语句
	    node u,v;
	    int i,tx,ty,flag=0,minn=0,head=1,tail=1;
	    memset(book,0,sizeof(book));
		u.x=a[0]-'a';
		u.y=a[1]-'1';
		u.s=0;
		book[a[0]-'a'][a[1]-'1']=1;
		q.push(u);//相当于tail++
		while(!q.empty())//相当于while(head<tail)
		{
		    u=q.front();
			for(i=0;i<8;i++)
			{
				tx=u.x+dir[i][0];
				ty=u.y+dir[i][1];
				if(tx>=0&&tx<8&&ty>=0&&ty<8&&book[tx][ty]==0)
				{
					book[tx][ty]=1;
					v.x=tx;
					v.y=ty;
					v.s=u.s+1;
					q.push(v);
				}
				if(v.x==int(b[0]-'a')&&v.y==int(b[1]-'1'))
				{
					flag=1;
					minn=v.s;
					break;
				}
			}
			if(flag==1)
			break;
			q.pop();//相当于head++
		}
		printf("To get from %s to %s takes %d knight moves.\n",a,b,minn);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值