HDU1372:Knight Moves(经典BFS题)
HDU1372:Knight Moves(BFS)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
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.
题解:骑士走日,和中国象棋中的马一样走日。
广搜bfs就可以
AC代码:
#include <iostream>
#include <queue>
#include<string.h>
using namespace std;
int s1,s2,e1,e2;
int tu[][];
int xx[] = {, , , , -, -, -, -};//x坐标变化
int yy[] = {, , -, -, , , -, -};//y坐标变化
int bfs()
{
memset(tu,,sizeof(tu));//数组值全为0
queue<int> q;
int x1,y1,x2,y2;
tu[s1][s2]=;//
q.push(s1);
q.push(s2);
while(!q.empty())
{
x1=q.front();
q.pop();
y1=q.front();
q.pop();
if(x1==e1&&y1==e2)//在原地不动
return tu[x1][y1];
for(int i=; i<; i++)
{
x2=x1+xx[i];
y2=y1+yy[i];
if(x2<||x2>||y2<||y2>||tu[x2][y2]>)
continue;
tu[x2][y2]=tu[x1][y1]+;
q.push(x2);
q.push(y2);
}
}
return ;
}
int main()
{
char a[],b[];
int total;
while(cin>>a>>b)
{
s1=a[]-'a';//输入的字母,是列,从第一列a开始,减去'a',转换
s2=a[]-'';//输入的数字,是行,从的第一行开始,减去'1',转换
e1=b[]-'a';
e2=b[]-'';
total=bfs();
cout<<"To get from "<<a<<" to "<<b<<" takes "<<total<<" knight moves."<<endl;
}
return ;
}
HDU1372:Knight Moves(经典BFS题)的更多相关文章
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU-1372 Knight Moves (BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏
Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...
- poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...
- HDU1372:Knight Moves(BFS)
Knight Moves Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- hdu1372 Knight Moves BFS 搜索
简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- uva439 - Knight Moves(BFS求最短路)
题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...
随机推荐
- 用Processon在线绘制UML的尝试
地址https://www.processon.com/ ProcessOn是一个面向垂直专业领域的作图工具和社交网络,成立于2011年6月并于2012年启动.ProcessOn将全球的专家顾问.咨询 ...
- [Theano] Theano初探
1. Theano用来干嘛的? Theano was written at the LISA lab to support rapid development of efficient machine ...
- SRM 504(2-1000pt)
DIV2 1000pt 题意:对于一个n*m的矩阵,每个格子都有一个颜色B或者W.对矩阵A执行以下程序后变成矩阵B.给出矩阵B,求A.(若有多种情况,输出字典序最小的).(n,m <= 16) ...
- windows Compiler toolchain env
1,gygwin
- D - Flip tile
题目大意 翻瓷砖(姑且认为题目就是这个意思吧) 农民约翰知道越聪明越快乐的牛产的牛奶越多(神马鬼理论),于是他开始安排牛进行一个智力运动在一个M*N][] = { {,},{,},{,-},{ ...
- Mongodb query查询
Query.All("name", "a", "b");//通过多个元素来匹配数组Query.And(Query.EQ("name ...
- spring 定时器----quartz启动问题
今天,突然要用到定时器,在网上查了下资料,保存下以方便后面查找: 什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定). 这样总不能修改配置文件 ...
- Configuration所有配置简介
// 内存缓存的设置选项 (最大图片宽度,最大图片高度) 默认当前屏幕分辨率 // .memoryCacheExtraOptions(480, 800) // 硬盘缓存的 ...
- 谈论C++当然结果
C++编程课程的考试已经结束.这是第一次OJCBT.摸着石头过河,考试没有给学生理解的说法.现在尘埃落定.一些交代. 先说大的成就的治疗原则.事实上,有很多的纠结. 按理说,合格的太,无法挂.但实际情 ...
- 开启.htaccess重写之前先来看看mod_rewrite(转)
Apache的Mode Rewrite模块提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求.在大多数情况下,它和.htaccess文件配合使用.就是说,.htaccess文件的一个主要功能 ...