H - Knight Moves DFS
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.
InputThe 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.
OutputFor 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.
思路 :骑士每次移动只能移动一个方格 其实就是一个日字(具体原因我也不知道)8个方向,,直接用BFS查找就可以了
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
using namespace std;
struct stu{
int a,b;
};
char a,b;
int aa,bb;
int arr[][];
int arr1[][];
int arr2[][];
int ar[][]={{-,-},{-,},{,},{,-},{,},{,-},{-,},{-,-}};
int bfs(int x1,int y1,int x2,int y2){
memset(arr1,,sizeof(arr1));
queue<stu>que;
que.push({x1,y1});
arr1[x1][y1]=;
arr2[x1][y1]=;
while(que.size()){
int x=que.front().a;
int y=que.front().b;
que.pop();
int dx,dy;
for(int i=;i<;i++)
{
dx=x+ar[i][];
dy=y+ar[i][];
if(dx>=&&dx<&&dy>=&&dy<&&arr1[dx][dy]!=){
arr1[dx][dy]=;
arr2[dx][dy]=arr2[x][y]+;
que.push({dx,dy});
if(dx==x2&&dy==y2){
return arr2[dx][dy];
}
}
}
}
return -;
}
int main()
{
while(~scanf("%c%d %c%d",&a,&aa,&b,&bb)){
getchar();
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
arr[i][j]=;
}
}
int x1=a-'a';
// cout<<x1<<endl;
int y1=aa-;
// cout<<y1<<endl;
int x2=b-'a';
// cout<<x2<<endl;
int y2=bb-;
// cout<<y2<<endl;
arr[x1][aa-]=;
arr[x2][bb-]=;
if(x1==x2&&y1==y2)
printf("To get from %c%d to %c%d takes 0 knight moves.\n",a,aa,b,bb);
else {
int y=bfs(x1,y1,x2,y2);
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,aa,b,bb,y);
}
} return ;
}
H - Knight Moves DFS的更多相关文章
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
- 【UVa】439 Knight Moves(dfs)
题目 题目 分析 没有估价函数的IDA...... 代码 #include <cstdio> #include <cstring> #include <a ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- 【POJ 2243】Knight Moves
题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...
- hdu Knight Moves
这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...
- OpenJudge/Poj 1915 Knight Moves
1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...
- HDU-1372 Knight Moves (BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
随机推荐
- 洛谷1265prim算法求最小生成树
题目链接:https://www.luogu.com.cn/problem/P1265 最小生成树的prim算法跟dijkstra算法非常像,就是将点分成两个集合,一个是已经在生成树中的点的集合,一个 ...
- JDBC(三)----JDBC控制事务
## JDBC控制事务 1.事务:一个包含多个步骤的业务操作.如果这个业务员操作被事务管理,则这多个步骤要么同时成功,要么同时失败. 2.操作: 1.开启事务 2.提交事务 3.回滚事务 3.使用C ...
- 快速排序-无序数组K小元素
13:07:382020-03-10 11:16:13 问题描述: 找到一个无序数组中第K小的数 样例 1: 输入: [3, 4, 1, 2, 5], k = 3 输出: 3 样例 2: 输入: [1 ...
- 【笔记3-31】Python语言基础-元组tuple
创建元组 my_tuple = () my_tuple1 = 1, 2, 3, 4, 5, 6 元组解包 与元组元素数量一致 a,s,d,f,g,h = my_tuple1 a, b, c, *f = ...
- Spss统计描述分析
总觉得有些技能学会了是不会忘的,但是还是要记录一下,怕记忆力不像狗皮膏药,并不那么牢固. 1.文件的合并 两个数据文件的合并 点击添加个案,这一步按照自己的需求选择,也可以打开外部数据集,在这里打开的 ...
- varchar int 查询 到底什么情况下走索引?
一个字符类型的.一个int类型的,查询的时候到底会不会走索引,其实很多工作了几年的开发人员有时也会晕,下面就用具体事例来测试一下. 1. 准备工作 先准备2张表,以备后续测试使用. 表1:创建表te ...
- NLPer,你知道最近很火的自然语言处理库么?
介绍 "NLP's ImageNet moment has arrived." 想象一下我们有能力构建支持谷歌翻译的自然语言处理(NLP)模型,并且在Python中仅需几行代码来完 ...
- 打造Worktile敏捷开发管理工具的思与惑
从2019年初,我们团队准备开发一款适合研发团队使用的敏捷开发管理工具,那时候我们也在思考,到底什么样的工具才算是优秀的研发管理工具,研发管理的场景.方法和流派有很多,市面上关于研发管理工具的产品也是 ...
- Java 数组 字符 函数
一. 1. package Hello; import java.util.Scanner; public class hello_test { public static void main(Str ...
- centos默认终端bash美化、颜色设置
centos默认终端bash是一个很简单的界面,又无法通过像zsh一样直接安装主题和代码高亮插件,但是我们可以在bashrc的配置文件中通过代码实现一部分功能: 1.代码介绍: 这里推荐一篇大佬的文章 ...