[宽度优先搜索] HDU 1372 Knight Moves
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10542 Accepted Submission(s): 6211
Problem 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
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
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.
Source
University of
Ulm Local Contest 1996
原文大意:对一个国际象棋的棋盘,每次输入骑士(骑士=马)的起始位置和目标位置,问至少要多少步才能走到。
解题思路:其实是一个裸的广搜,贴上充数用的,输入输出打麻烦了。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct mp
{
int x,y,step;
mp(int x,int y,int step):x(x),y(y),step(step){}
};
queue<mp> q;
void bfs(int xbegin,int ybegin,int xend,int yend)
{
const int move[2][8]={{1,2,2,1,-1,-2,-2,-1},{2,1,-1,-2,-2,-1,1,2}};
int i,x,y,visit[9][9];
char sans1,sans2;
memset(visit,0,sizeof(visit));
if(xbegin==xend&&ybegin==yend)
{
sans1=xbegin+'a'-1;
sans2=xend+'a'-1;
printf("To get from %c%d to %c%d takes %d knight moves.\n",sans1,ybegin,sans2,yend,0);
return;
}
while(!q.empty()) q.pop();
q.push(mp(xbegin,ybegin,0));
visit[xbegin][ybegin]=1;
while(!q.empty())
{
for(i=0;i<8;++i)
{
x=q.front().x+move[0][i];y=q.front().y+move[1][i];
if(x==xend&&y==yend)
{
sans1=xbegin+'a'-1;
sans2=xend+'a'-1;
printf("To get from %c%d to %c%d takes %d knight moves.\n",sans1,ybegin,sans2,yend,q.front().step+1);
return;
}
if(x>0&&x<9&&y>0&&y<9&&(!visit[x][y]))
{
q.push(mp(x,y,q.front().step+1));
visit[x][y]=1;
}
}
q.pop();
}
return;
}
int main()
{
char s[5];
int x_start,x_end,y_start,y_end;
while(scanf("%s",&s)!=EOF)
{
x_start=s[0]-'a'+1;
y_start=s[1]-'0';
scanf("%s",s);
x_end=s[0]-'a'+1;
y_end=s[1]-'0';
bfs(x_start,y_start,x_end,y_end);
}
return 0;
}
[宽度优先搜索] HDU 1372 Knight Moves的更多相关文章
- 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)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- ZOJ 1091 (HDU 1372) Knight Moves(BFS)
Knight Moves Time Limit: 2 Seconds Memory Limit: 65536 KB A friend of you is doing research on ...
- HDU 1372 Knight Moves 题解
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
随机推荐
- Linux命令之md5sum
1. 背景 在网络传输.设备之间转存.复制大文件等时,可能会出现传输前后数据不一致的情况.这种情况在网络这种相对更不稳定的环境中,容易出现.那么校验文件的完整性,也是势在必行的. md5sum命令用于 ...
- ubuntu15.04安装Chrome浏览器
首先到: https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 下载最新的安装文件. 然后: sudo a ...
- ligerui_ligerTree_004_对"ligerTree"节点操作
ligerTree节点操作: 源码地址:http://download.csdn.net/detail/poiuy1991719/8571255 效果图: 代码: json.txt: [ { text ...
- JQuery基础三
1.checkbox操作:全选.全不选.反选 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...
- JavaScript encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent()
URI: Uniform Resource Identifier encodeURI() And decodeURI() The encodeURI() function is used to en ...
- 初试“七牛云”--零基础运用七牛云配合UEditor实现图片的上传和浏览(.NET篇)
(注册和建立存储空间就不介绍了,网上一把一把的资料,自己试着点点也能明白) 作为一个成熟的菜鸟,如果遇到一个新问题,第一步当然是先百度一下... 看了N个关于七牛云的使用的帖子,表示还是蒙圈的,看懂了 ...
- c/s 与b/s构架
C/S架构是一种比较早的软件架构,主要应用于局域网内.在这之前经历了集中计算模式,随着计算机网络的进步与发展,尤其是可视化工具的应用,出现过两层C/S和三层C/S架构,不过一直很流行也比较经典的是我们 ...
- lipo 合并target为Simulator和Device编译的静态库
进入项目对应的Build目录后,以下指令: $lipo -create Debug-iphoneos/libSalamaDeveloper.a Debug-iphonesimulator/libSal ...
- Linux休眠,挂起,待机,关机的区别及相关命令
休眠是一种更加省电的模式,它将内存中的数据保存于硬盘中,所有设备都停止工作.当再次使用时需按开关机键,机器将会恢复到您的执行休眠时的状态,而不用再次执行启动操作系统复杂的过程. 待机(挂起)是将当前处 ...
- 深入浅出设计模式——职责链模式(Chain of Responsibility Pattern)
模式动机 职责链可以是一条直线.一个环或者一个树形结构,最常见的职责链是直线型,即沿着一条单向的链来传递请求.链上的每一个对象都是请求处理者,职责链模式可以将请求的处理者组织成一条链,并使请求沿着链传 ...