http://acm.hdu.edu.cn/showproblem.php?pid=1372

Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6439    Accepted Submission(s): 3886

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.
 
AC代码:
<span style="font-size:24px;">#include<iostream>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std; struct node
{
int x,y,num;
}; int a1,a2,b1,b2;
char c1,c2;
int move[8][2]={-2,1,-2,-1,-1,2,-1,-2,1,-2,1,2,2,1,2,-1},v[9][9]; void bfs(int i,int j)
{
node now,temp;
queue<node>q;
now.x=i;
now.y=j;
now.num=0;
memset(v,0,sizeof(v));
q.push(now);
v[now.x][now.y]=1;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==b2&&now.y==a2)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,a1,c2,a2,now.num);
return ;
}
for(int t=0;t<8;t++)
{
temp.x=now.x+move[t][0];
temp.y=now.y+move[t][1];
if(temp.x>0&&temp.x<9&&temp.y>0&&temp.y<9&&!v[temp.x][temp.y])
{
v[temp.x][temp.y]=1;
temp.num=now.num+1;
q.push(temp);
}
}
}
} int main()
{
while(cin>>c1>>a1>>c2>>a2)
{
b1=c1-'a'+1;
b2=c2-'a'+1;
bfs(b1,a1);
}
return 0;
}</span>




版权声明:本文博主原创文章,博客,未经同意不得转载。

杭州电 1372 Knight Moves(全站搜索模板称号)的更多相关文章

  1. HDU 1372 Knight Moves(最简单也是最经典的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  2. Knight Moves(hdu1372 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)   ...

  3. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  4. HDU 1372 Knight Moves 题解

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. HDU 1372 Knight Moves (bfs)

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

  6. 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 ...

  7. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

  8. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  9. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

随机推荐

  1. Page_Load基类,重写OnLoad

    protected override void OnLoad(EventArgs e) { userid = PublicFun.GetSessionValue(HttpContext.Current ...

  2. ionic开发环境搭建

    Advanced HTML5 mobile development framework and SDK. Build incredible mobile apps with web technolog ...

  3. zepto源码研究 - zepto.js - 1

    简要:网上已经有很多人已经将zepto的源码研究得很细致了,但我还是想写下zepto源码系列,将别人的东西和自己的想法写下来以加深印象也是自娱自乐,文章中可能有许多错误,望有人不吝指出,烦请赐教. 首 ...

  4. [转]《深度探索C++对象模型》读书笔记[二]

    3.3 Data Member的存取1.   不管什么情况,每一个static data member只有一个实体,放在程序的data segment之中,每次程序取用static member,不管 ...

  5. PHP 过滤二维数组和三维数组

    <?php $arr = [ [1,3,5,7,9], [2,4,6,8,0] ]; $arr2 = [ 'list' => [ [1,3,5,7], [2,4,6,8], [3,2,9, ...

  6. shell如何生成rpm包仓库列表文件的对比结果

    基本步骤: 1.切换至仓库目录RPM_LIST_DIR1和RPM_LIST_DIR2 2.ls列出仓库的rpm包文件并分别重定向至输出文件rpm_list_file1和rpm_list_file2 3 ...

  7. 机器学习之python: kNN

    ################################################## # kNN : k Nearest Neighbour # Author : Monne # Da ...

  8. web安全:xss && csrf

    首先在user.php文件中去除黑名单的第一行标签,在白名单中添加<script>E1:csrf攻击zoobarcsrf:cross-site request forgery    跨站伪 ...

  9. recovery编译汉化源码开源地址

    本Recovery基于xiaolu开源的不完全汉化版源码,进行完全汉化,并合并Philz的最新源码. 汉化耗费我将近一整天的精力,纯手打,可能有遗漏或翻译不准的地方,请到微博反馈 本Rec完全开源,便 ...

  10. android-support-v7-appcompat下载

    http://download.csdn.net/detail/u010556601/7449661 将压缩包解压,放入sdk\extras\android\support\v7目录下 在eclips ...