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题)的更多相关文章

  1. HDOJ/HDU 1372 Knight Moves(经典BFS)

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  2. HDU-1372 Knight Moves (BFS)

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  3. 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 ( ...

  4. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...

  5. HDU1372:Knight Moves(BFS)

    Knight Moves Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  6. hdu1372 Knight Moves BFS 搜索

    简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...

  7. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  8. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  9. uva439 - Knight Moves(BFS求最短路)

    题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...

随机推荐

  1. 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理

    原文地址:http://blog.csdn.net/hawksoft/article/details/21776009 最近调试原来的微信模拟登陆时发生了“基础连接已关闭,发送时发生错误”的错误提示, ...

  2. SPJ 讨论

    设有一个SPJ数据库,包括S,P,J,SPJ四个关系模式: S( SNO,SNAME,STATUS,CITY): P(PNO,PNAME,COLOR,WEIGHT): J(JNO,JNAME,CITY ...

  3. SRM 504(2-1000pt)

    DIV2 1000pt 题意:对于一个n*m的矩阵,每个格子都有一个颜色B或者W.对矩阵A执行以下程序后变成矩阵B.给出矩阵B,求A.(若有多种情况,输出字典序最小的).(n,m <= 16) ...

  4. UVa 11796 计算几何

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. springmvc框架原理分析

    第一步:用户发起请求到前端控制器(DispatcherServlet) 第二步:前端控制器请求HandlerMapping查找Handler,可以根据xml配置,注解进行查找 第三步:处理器映射器Ha ...

  6. python最简洁的条件判断语句写法

    这篇文章主要介绍了Python返回真假值(True or False)小技巧,本文探讨的是最简洁的条件判断语句写法,本文给出了两种简洁写法,需要的朋友可以参考下 如下一段代码: def isLen(s ...

  7. loadView,viewDidLoad等几种方法的调用总结

    viewDidLoad 此方法只有当view从nib文件初始化的时候才被调用.viewDidLoad用于初始化,加载时用到的. loadView 此方法在控制器的view为nil的时候被调用.虽然经常 ...

  8. MongoDB与传统数据库的使用区别——批量插入与批量查询

    我在百X知道上回答问题时经常遇到类似与这样的问题:MongoDB有没有像MySQL一样的ODBC驱动?MongoDB能不能像MySQL一样获取字段名称或类型. 我的回答是:不行,因为MongoDB不是 ...

  9. 1.1GTK+ 的简单程序HelloWorld

    1.1GTK+ 的简单程序HelloWorld 编译执行如图所看到的:

  10. 【待解决】编译V8引擎出错-snapshot.cc

    这几天学习nodejs,翻阅官网的API文档.看到nodejs插件时,想了解一下v8的实现机制,于是我便从GitHub社区克隆了一份v8源码库.哪知道,编译安装的时候就出了问题,这问题已经折磨我两天了 ...