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

2024-03-05 21:08

本文主要是介绍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而是=-1for (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;
}


题外话:可以打表。

这篇关于UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS纯数学方法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/777809

相关文章

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行