Knight Moves
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.
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.
case, print one line saying "To get from xx to yy takes n knight
moves.".
e2 to e4 takes 2 knight moves.
a1 to b2 takes 4 knight moves.
b2 to c3 takes 2 knight moves.
a1 to h8 takes 6 knight moves.
a1 to h7 takes 5 knight moves.
h8 to a1 takes 6 knight moves.
b1 to c3 takes 1 knight moves.
f6 to f6 takes 0 knight moves.
#include
#include
#include
#define maxn 10
using namespace std;
int
dir[8][2]={{-2,-1},{-1,-2},{1,-2},{2,-1},{-2,1},{-1,2},{1,2},{2,1}};
int visit[maxn][maxn];
int check(int a,int b)
{
if(a<1||a>8||b<1||b>8||visit[a][b])
return 1;
else
return 0;
}
struct node
{
int
x,y;
int
times;
}start,fr,a;
int bfs(int sx,int sy,int x,int y)
{
memset(visit,0,sizeof(visit));
queue
Q;
start.x=sx;
start.y=sy;
start.times=0;
visit[start.x][start.y]=1;
Q.push(start);
while(!Q.empty())
{
fr=Q.front();
Q.pop();
//cout<<"x="<<x<<"
"<<"y="<<y<<endl;
if(fr.x==x&&fr.y==y)
return fr.times;
for(int i=0;i<8;i++)
{
a.x=fr.x+dir[i][0];
a.y=fr.y+dir[i][1];
if(check(a.x,a.y))
continue;
a.times=fr.times+1;
visit[a.x][a.y]=1;
Q.push(a);
}
}
int main()
{
//freopen("in.txt", "r", stdin);
char
a,b;
int
sx,sy,x,y;
while(~scanf("%c%d %c%d\n",&a,&sy,&b,&y))
{
//cout<<"sx="<<a<<endl;
//cout<<"sy="<<sy<<endl;
//cout<<"x="<<b<<endl;
//cout<<"y="<<y<<endl;
sx=a-'a'+1;
x=b-'a'+1;
//坐标转化成数字
int ans=bfs(sx,sy,x,y);
printf("To get from %c%d to %c%d takes %d knight
moves.\n",a,sy,b,y,ans);
}
return
0;
}
Knight Moves的更多相关文章
- 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) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] 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 ...
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
- 【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 ...
- HDU 1372 (搜索方向稍有改变) Knight Moves
其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- UVA 439 Knight Moves
// 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...
随机推荐
- 利用Docker快速创建Nginx负载均衡节点
本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws 一.Self-Host Kestrel 1. 在vs2017中新建dotnet core2. ...
- Postman 串行传参和动态传参详解
Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件 用Postman做接口测试的时候,要把多条用例一起执行,就需要把用例连接起来,一次性执行 目录 串行传参 动态传参 使用 ...
- Opengl4.5 中文手册—E
索引 A B C D E F G H I J K L M N O P Q ...
- 如何解决Python.h:No such file or directory
安装python2.7对应的dev sudo apt-get install python-dev 安装python3.6对应的dev sudo apt-get install python3-dev
- Spring Boot Document Part I
最近准备学习Spring Boot 随便翻一下官方的文档 Part I. Spring Boot Documentation Spirng Boot简短介绍,作为接下来内容的导航,可快速预览本章内容. ...
- TCP/IP(七)之玩转HTTP协议
前言 前面一篇的博文简单的介绍了一下属于应用层的HTTP协议,这一篇我将详细的学习HTTP协议,这也是做Web开发中一定要用到的协议.虽然我是做大数据的,但是多学习一点肯定是 没有坏处的.国庆放假7天 ...
- BZOJ-1010-[HNOI2008]玩具装箱toy(斜率优化)
Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...
- (转)深度学习word2vec笔记之基础篇
深度学习word2vec笔记之基础篇 声明: 1)该博文是多位博主以及多位文档资料的主人所无私奉献的论文资料整理的.具体引用的资料请看参考文献.具体的版本声明也参考原文献 2)本文仅供学术交流,非商用 ...
- Ubuntu16.04下安装redis
Ubuntu16.04下安装redis 保证网络畅通,选定好下载工作路径,执行以下命令下载redis-3.2.6: sudo wget http://download.redis.io/release ...
- 第9期Unity User Group Beijing图文报道:《Unity实战经验分享》
时间来到了金秋九月,北京UUG活动也来到了第九期.本次活动的主题为<Unity实战经验分享>,为此我们邀请了3位资深的行业大神.这次我们仍然在北京市海淀区丹棱街5号微软大厦举行活动,在这里 ...