首页 > 其他 > 详细

Knight Moves UVA - 439

时间:2021-02-18 23:14:19      阅读:32      评论:0      收藏:0      [点我收藏+]

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 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

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.

HINT

解决思路就是利用一个BFS(Breadth_First_Search)遍历。

思路梳理:

  1. BFS的应用场景有两个:

    • 层序遍历
    • 求无权图单源最短路径
  2. BFS为何可以求得无权图的最短路?

    BFS的算法思想和二叉树的层序遍历类似,利用队列来处理,每一次出队首元素的时候就将队首元素的最近的结点或者距离为一个步长的结点拉入队列。因此如果在这个过程中我们将入队的元素都赋给他们自己的层号,那么层号就是从出发点到目的地的最短路径。结合下面图片来理解(图片来自《大话数据结构(点击下载)》):
    技术分享图片

  1. 对于这个题目如何利用BFS来解决?

    BFS处理的是图的遍历,要使用BFS就要将题目中的棋盘转化为图。这里将当前马所在的位置看作一个结点,马可以从这个点走一步所到达的地方就是它所连接的几个结点。那么连接的几个结点的层号就是原来位置的层号+1 。

Accepted

#include <bits/stdc++.h>
using namespace std;
pair<int,int>Next[] = { {1,2},{2,1},{-1,-2},{-2,-1},{1,-2},{2,-1},{-1,2},{-2,1} };		//将所有可能的坐标位置先写出来
int main() {
	string s1, s2;																		//保存其实和重视位置
	while (cin >> s1 >> s2) {
		pair<int, int>temp;	
		queue<pair<int, int>>Q;															//用于BFS遍历的数组
		int vis[8][8] = { 0 };															//标记是否访问,如果为访问那么为0或者是起始位置,否则为其到起始点的距离
		pair<int, int>a = { (int)(s1.front() - ‘a‘),(int)(s1.back() - ‘0‘)-1 };			//保存其实坐标
		pair<int, int>b = { (int)(s2.front() - ‘a‘),(int)(s2.back() - ‘0‘)-1 };			//保存目的地
		Q.push(a);vis[a.first][a.second] = 0;											//将起始地入队,并标记访问
		while (!Q.empty()) {
			temp = Q.front();	Q.pop();												//保存并出队
			if (temp == b)break;														//如果是目的地,那么结束循环
			for (int i = 0;i < 8;i++) {													//否则,找到它的可能走到的坐标,也就是与他相连的结点
				int x = temp.first + Next[i].first;										//计算横坐标
				int y = temp.second + Next[i].second;									//计算纵坐标
				pair<int, int>t = { x,y };			
				if (x > -1 && y > -1 && x < 8 && y < 8 && !vis[x][y]&&t!=a) {			//如果坐标没有越界,并且不是起始点
					Q.push(t);vis[x][y] = vis[temp.first][temp.second] + 1;				//入队,并且标记到起始点的距离
				}
			}
		}
		cout << "To get from " << s1 << " to " << s2 << " takes " << vis[b.first][b.second] << " knight moves." << endl;//输出结果
	}
}

Knight Moves UVA - 439

原文:https://www.cnblogs.com/3236676588buladuo/p/14413174.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!