UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS&纯数学方法)

439 - Knight Moves

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=380

http://acm.hdu.edu.cn/showproblem.php?pid=1372

http://poj.org/problem?id=2243

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1091

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 Specification

The input file 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 Specification

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.

题意:

在辽阔的N*N大草原上(棋盘)上你有一只草泥马,马走日。求从a到b怎么走步数最小。


思路:

1. BFS,复杂度O(N^2),N指棋盘边长。

2. 数学方法,复杂度O(N^2),但系数比第一种小很多:

设横纵坐标的差值分别是x,y。由于我们的草泥马只能有8种走法,实际上只会出现4种(方向向量有(1,2),(2,1),(1,-2),(2,-1),(-1,2),(-2,1),(-1,-2),(-2,-1)8种,但是如果要走最小的次数,就不可能同时出现(1,2)和(-1,-2)这样相反的方向向量,所以只会出现4种)。所以,我们设方向向量为(1,2),(2,1),(2,-1),(1,-2)的有a,b,c,d次,其中a,b,c,d可以为负数(比如a为负数代表方向向量为(-1,-2))。

于是,可以列两个方程:


我们要求的是|a|+|b|+|c|+|d|的最小值。把a,b,看做常量,解得:


所以a+2b≡2x+y(mod 3),即a≡-2b+2x+y(mod 3)(-3≤a,b3)

但由于a,b关系的制约,当b在-3到3范围内变动时,a只有三种情况(取-3,0,3或-2,1或-1,2)

所以a,b的组合有16或17种,比较每种情况的|a|+|b|+|c|+|d|,求最小的即可。

但是在计算角落时要另外考虑,比如(a1,b2)按上面方法算的是2,实际是4。

经过计算知,对于8*8的只有4种情况:(a1,b2),(a8,b7),(g2,h1),(g7,h8),

对这四种情况单独拿出来说就好了。


完整代码:

BFS,很慢。

/*UVaOJ: 0.022s*/
/*HDU: 31ms,244KB*/
/*POJ: 266ms,156KB*/
/*ZOJ: 10ms,180KB*/

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;

bool vis[9][9];
int a, b, c, d;
int mov[8][2] = {1, 2, -1, 2, -2, 1, -2, -1, -1, -2, 1, -2, 2, -1, 2, 1};

struct node
{
	int x, y, step;
} lnode;

int bfs()
{
	int i;
	queue<node>q;
	node cur, next;
	cur.x = a;
	cur.y = b;
	cur.step = 0;
	vis[cur.x][cur.y] = true;
	q.push(cur);
	while (!q.empty())
	{
		cur = q.front();
		q.pop();
		for (i = 0; i < 8; i++)
		{
			next.x = cur.x + mov[i][0];
			next.y = cur.y + mov[i][1];
			if (next.x >= 1 && next.x <= 8 && next.y >= 1 && next.y <= 8)
			{
				if (next.x == c && next.y == d)
				{
					next.step = cur.step + 1;
					return next.step;
				}
				if (!vis[next.x][next.y])
				{
					next.step = cur.step + 1;
					vis[next.x][next.y] = true;
					q.push(next);
				}
			}
		}
	}
	return -1;
}

int main()
{
	char str[5], str2[5];
	while (~scanf("%s %s", str, str2))
	{
		memset(vis, 0, sizeof(vis));
		a = str[0] - '`' ;//'a'前面是'`'
		b = str[1] - '0';
		c = str2[0] - '`' ;
		d = str2[1] - '0';
		int ans = 0;
		if (a != c || b != d)
			ans = bfs();
		printf("To get from %s to %s takes %d knight moves.\n", str, str2, ans);
	}
	return 0;
}


数学方法,较快。

/*UVaOJ: 0.019s*/
/*HDU: 15ms,232KB*/
/*POJ: 16ms,144KB*/
/*ZOJ: 0ms,180KB*/

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
	char except[8][3] = {"a1", "b2", "a8", "b7", "g2", "h1", "g7", "h8"};
	char s1[5], s2[5];
	int  x, y, a, b, c, d,sum, m, temp;
	while (~scanf("%s%s", s1, s2))
	{
		if (!strcmp(s1, s2))
		{
			printf("To get from %s to %s takes 0 knight moves.\n", s1, s2);
			continue;
		}
		bool flag = false;
		for (int i = 0; i < 8; i += 2)
			if (!((strcmp(s1, except[i]) || strcmp(s2, except[i + 1]))
					&& (strcmp(s1, except[i + 1]) || strcmp(s2, except[i]))))
			{
				flag = true;
				break;
			}
		if (flag)
		{
			printf("To get from %s to %s takes 4 knight moves.\n", s1, s2);
			continue;
		}
		x = s2[0] - s1[0];
		y = s2[1] - s1[1];
        sum = 1 << 6;
        //下面a,b的取值范围是由-3<=a,b<=3所确定的
        //注意b取负数和正数的情况是不一样的
        //注意取模时,-1%3!=2而是=-1
		for (b = -3; b <= 3; b++)
		{
			m = (y + 2 * x - 2 * b) % 3;
			for (a = m - 3; a <= m + 3; a += 3)
			{
				c = (2 * x + y - 4 * a - 5 * b) / 3;
				d = (5 * a + 4 * b - x - 2 * y) / 3;
				temp = abs(a) + abs(b) + abs(c) + abs(d);
				if (temp < sum)
					sum = temp;
			}
		}
		printf("To get from %s to %s takes %d knight moves.\n", s1, s2, sum);
	}
	return 0;
}


题外话:可以打表。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值