博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ 1372 Knight Moves
阅读量:5935 次
发布时间:2019-06-19

本文共 3294 字,大约阅读时间需要 10 分钟。

Knight Moves

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

Total Submission(s): 3340    Accepted Submission(s): 2087

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.
1 /* 功能Function Description:     hdoj-1372  用STL队列实现BFS 2    开发环境Environment:          DEV C++ 4.9.9.1 3    技术特点Technique: 4    版本Version: 5    作者Author:                   可笑痴狂 6    日期Date:                     20120725 7    备注Notes:        哈哈!! 第一篇用深搜队列 8 */ 9 10 #include
11 #include
12 #include
13 #include
14 using namespace std;15 16 struct point 17 {18 int x;19 int y;20 int step;21 };22 23 int dir[8][2]={-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};24 25 int BFS(string st,string end)26 {27 bool visit[10][10];28 memset(visit,false,sizeof(visit));29 point s,e,t;30 queue
q;31 s.x=st[0]-'a'; //将其转化为二维坐标表示32 s.y=st[1]-'0';33 s.step=0;34 e.x=end[0]-'a';35 e.y=end[1]-'0';36 if(s.x==e.x&&s.y==e.y)37 return 0;38 q.push(s);39 visit[s.x][s.y]=true;40 while(!q.empty())41 {42 s=q.front();43 q.pop();44 for(int i=0;i<8;++i)45 {46 t.x=s.x+dir[i][0];47 t.y=s.y+dir[i][1];48 t.step=s.step+1;49 if(visit[t.x][t.y]==false)50 {51 if(t.x==e.x&&t.y==e.y)52 return t.step;53 if(t.x>=0&&t.x<8&&t.y>0&&t.y<=8)54 {55 visit[t.x][t.y]=true;56 q.push(t);57 }58 }59 }60 }61 //return 0;62 }63 64 int main()65 {66 string st,end;67 while(cin>>st>>end)68 {69 cout<<"To get from "<
<<" to "<
<<" takes "<
<<" knight moves."<

 

转载地址:http://owjtx.baihongyu.com/

你可能感兴趣的文章
jenkins集成ansible注意事项Failed to connect to the host via ssh.
查看>>
EL表达式经典用法
查看>>
java.lang.NoClassDefFoundError: javax/mail/Authenticator
查看>>
Win10 UWP 开发系列:使用多语言工具包让应用支持多语言
查看>>
04-maven学习-pom.xml解析
查看>>
js 自定义事件 包含 添加、激活、销毁
查看>>
PowerDesigner逆向生成MYSQL数据库表结构总结
查看>>
idea上activiti插件的安装及使用
查看>>
打开方式中无法添加指定程序
查看>>
图像滤镜艺术--大雾效果滤镜
查看>>
Node入门教程(3)第二章: Node 安装
查看>>
程序员图片注释字符串制作工具
查看>>
Swift代理的使用
查看>>
SQLSERVER SQL备份还原代码C#
查看>>
WPF ListView即时更新
查看>>
VUE router-view 页面布局 (嵌套路由+命名视图)
查看>>
ShareDialogDemo【分享对话框】
查看>>
DataTable 和List 相互转换
查看>>
[转]虚拟化系列-Windows server 2012 Remote桌面与应用
查看>>
iOS - (懒加载)
查看>>