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 ...
随机推荐
- lightoj 1198 最大权重匹配
题目链接:http://lightoj.com/volume_showproblem.php?problem=1198 #include <cstdio> #include <cst ...
- Linux 下让进程在后台可靠运行的几种方法
想让进程在断开连接后依然保持运行?如果该进程已经开始运行了该如何补救? 如果有大量这类需求如何简化操作? 我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一 ...
- ubuntu忘记密码
Do these two things just to make sure: mount -o remount,rw / This first part remounts the root parti ...
- 《Java 并发编程实战》读书笔记之二:图文讲述同步的另一个重要功能:内存可见性
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17288243 加锁(synchronized同步)的功能不仅仅局限于互斥行为,同时还存在另 ...
- 字符集转换 字符类型转换 utf-8 gb2312 url
vs默认是GB2312编码,你看到的程序源代码是,输出结果是,内部存储是, 1 如果你想改变内部存储可以用下面的这些函数 2 如果你想改变源代码的存储方式你可以用文本编辑工具修改之后重新编译 3 如果 ...
- Effect of Switchovers, Failovers, and Control File Creation on Backups
对dataguard 官方文档里面的这句话不理解,是否能给出一个样例说明: 10.2.0.5的版本号 Effect of Switchovers, Failovers, and Control Fil ...
- Android URI简单介绍
就Android平台而言,URI主要分三个部分:scheme, authority and path.当中authority又分为host和port.格式例如以下: scheme://host:por ...
- [PWA] 11. Serve skeleton cache for root
Intead of cache the root floder, we want to cache skeleton instead. self.addEventListener('install', ...
- uva 11324 The Largest Clique (Tarjan+记忆化)
/*每个环 要么不选 要么全选 可缩点 就得到一个GAD图 然后搞搞算出最大路径*/ #include<iostream> #include<cstdio> #include& ...
- 【转】vue基础学习
1.基本绑定: new Vue( { el:'#elID', data:{ // data obj ...