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 ...
随机推荐
- bzoj 1191 [HNOI2006]超级英雄Hero(最大基数匹配)
1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2813 Solved: 1331[Submit][ ...
- 对List
class MyCompare implements Comparator//自定义比较方式 要实现Conparator的 compare 方法 { public int compare(O ...
- MAVEN 工程打包resources目录外的更多资源文件
首先,来看下MAVENx项目标准的目录结构: 一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,ma ...
- redis学习心得之二【redis主从配置】
在前一节我们已经实践启动了一个redis服务,我们将其作为主机,现为其创建一个从机作备份使用 1.复制一份配置出来为从机所用 ~$ cp redis/etc/redis.conf ...
- Java菜鸟学习笔记--Exception篇(一):异常简介
什么是异常(Exception)? 简述: 在运行过程中,应用程序可能遭遇各种严重程度不同的问题.异常提供了一种在不弄乱程序的情况下检查错误的巧妙方式.它也提供了一种直接报告错误的机制. 不同类型异常 ...
- sed 批量替换多个文件里的某个字符/串
提示: 国际惯例使用前先备份 sed -i "s/a/b/g" `grep 'a' -rl ./`
- AlertDialog dismiss 和 cancel方法的区别
AlertDialog使用很方便,但是有一个问题就是:dismiss方法和cancel方法到底有什么不同? AlertDialog继承与Dialog,现在各位看看结构图: 然后在Dialog类中找到了 ...
- OD: Peimei & Versioning Analysis
PE 中漫步—“白眉” 指令追踪技术与 Paimei 程序异常发生的位置通常离漏洞函数很远,当溢出发生时,栈帧往往也会遭到破坏,给动态调试制造很大的困难. 指令追踪最大限度地结合了动态分析和静态分析的 ...
- require.js的使用的坑!
require.js的使用心德: 都是自我的理解所得: first:为什么使用? 1,web开发js的占用比例越来越大,引入的插件也越来越多,维护困难,一个一个的script的写要废 2,模块开发的需 ...
- 工时统计的sql练习--包含时间处理
//按月统计,除去周末的考勤,(工时,请假,缺勤) --建表sql 创建[dbo].[AbsenceHourld]CREATE TABLE [dbo].[AbsenceHourld]( [id] [i ...