Knight Moves UVA - 439
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)遍历。
思路梳理:
BFS的应用场景有两个:
- 层序遍历
- 求无权图单源最短路径
BFS为何可以求得无权图的最短路?
BFS的算法思想和二叉树的层序遍历类似,利用队列来处理,每一次出队首元素的时候就将队首元素的最近的结点或者距离为一个步长的结点拉入队列。因此如果在这个过程中我们将入队的元素都赋给他们自己的层号,那么层号就是从出发点到目的地的最短路径。结合下面图片来理解(图片来自《大话数据结构(点击下载)》):
对于这个题目如何利用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的更多相关文章
- UVA 439 Knight Moves(BFS)
Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
- UVA 439 Knight Moves
// 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...
- 【UVa】439 Knight Moves(dfs)
题目 题目 分析 没有估价函数的IDA...... 代码 #include <cstdio> #include <cstring> #include <a ...
- uva 439 Knight Moves 骑士移动
这道题曾经写过,bfs.用队列,不多说了,上代码: #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- UVA Knight Moves
题目例如以下: Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
随机推荐
- js中国标准时间转换成datetime格式
var format = function (time, format) { var t = new Date(time); var tf = function (i) { return (i < ...
- 深入理解 Web 协议 (三):HTTP 2
本篇将详细介绍 HTTP 2 协议的方方面面,知识点如下: HTTP 2 连接的建立 HTTP 2 中帧和流的关系 HTTP 2 中流量节省的奥秘:HPACK 算法 HTTP 2 协议中 Server ...
- IVMS-5000海康平台安装
某学校系统因中勒索病毒重装监控系统平台 一 安装前准备 二 数据库安装 采用免安装版本安装,原先平台采用免安装版mysql 5.6.24 ,学校IT管理员将数据库文件完全复制,用虚拟机将数据库搭 ...
- Markdown(2)基本语法
Markdown 是一种轻量级标记语言 , 通过简单的标记语法,使文本内容具有一定的格式 . 一.段落 1. 标题 语法格式: 符号 "#" 可以标记 1~6级标题.一级标题对 ...
- HDOJ-1686(KMP算法)
Oulipo HDOJ-1686 本题的思路就是KMP,和HDOJ-1711思路一样,不再赘述详情可以看链接:1711题解 #include<iostream> #include<c ...
- TestLink测试用例管理工具使用说明
TestLink使用说明 打开网页,登录账号:(这里的账号是已经注册过的,并且拥有admin权限,可以创建用户.当然也可以通过点击登录页面的"新用户注册"按钮进行注册,但是权限是g ...
- 【Arduino学习笔记02】第一个Arduino项目——点亮LED Blink.ino程序解读 Arduino程序基本结构 pinMode() digitalWrite() delay()
/* Blink Turns an LED on for one second, then off for one second, repeatedly. */// define variables ...
- Go中定时器实现原理及源码解析
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码15.7,需要注意的是由于timer是1.14版本进行改版,但是1. ...
- Thinkphp3分析与审计
0x00 前言: 这篇是去年组内分享的时候给小伙伴写的0基础快速审计tp3系列的文章,主要是对架构做个分析以及审计一些sql注入漏洞~ 现在想想打算放出来,过了一年了,可能里面有一些问题,望看到的大佬 ...
- 如何在 C# 8 中使用 Channels
在面对 生产者-消费者 的场景下, netcore 提供了一个新的命名空间 System.Threading.Channels 来帮助我们更高效的处理此类问题,有了这个 Channels 存在, 生产 ...