杭州电 1372 Knight Moves(全站搜索模板称号)
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6439 Accepted Submission(s): 3886
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.
(a-h) representing the column and a digit (1-8) representing the row on the chessboard.
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
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.
<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(全站搜索模板称号)的更多相关文章
- HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- Knight Moves(hdu1372 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others) ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- 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 (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 ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
随机推荐
- 利用eclipse开发php<转>
1.安装php环境 Eclipse支持PHP自动提示 其实如果你已经安装好了php环境(安装过程见)的话,只需要下面2步就可以了.hoho,很简单的. 1,下载eclipse中php的插件phpecl ...
- 可清空文本的EditText
代码如下: public class DeleteEditText extends EditText { private Context mContext; //删除图标 private Drawab ...
- Cacti监控Linux主机
1 要监视一台Linux主机,需要在被监控的主机上安装net-snmp相关软件包,CentOS安装可使用“yum -y install net-snmp”命令:# yum -y install net ...
- 关于Aspose对于Word操作的一些扩展及思考
Aspose.word Aspose.Words是一款先进的类库,通过它可以直接在各个应用程序中执行各种文档处理任务.Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocu ...
- C#基础学习第三天(.net菜鸟的成长之路-零基础到精通)
1.复合赋值运算符 += -= *= /= %= 2.关系运算符 > < >= <= == != 由关系运算符连接的表达式我们称之为关系表达式. 每一个表达式都可以求解出 ...
- intellij idea 热部署失效,需要手动编译类
从网上看到的解决方案,做一下备忘: spring boot项目中遇到jrebel类需要手动编译才会触发热部署的问题(spring boot devtools一样的问题) 1.ctl + shift + ...
- 剖析c++(三) 类对象在内存中的布局
基本规则: 1.各data member的自然边界为其字节大小(char为1,short为2,int为4),并按照各自的自然边界对齐: 2.整个object的总自然边界为最大data memeber的 ...
- Linux编程基础——GDB(设置断点)(转:TianFang,cnblog: http://www.cnblogs.com/TianFang/archive/2013/01/20/2868889.html)
启动GDB后,首先就是要设置断点,程序中断后才能调试.在gdb中,断点通常有三种形式: 断点(BreakPoint): 在代码的指定位置中断,这个是我们用得最多的一种.设置断点的命令是break,它通 ...
- 最完美的匹配网页中图片 src 部分的正则表达式
$str='<p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"& ...
- centos 开启apache rewrite模式
mod_rewrite能使网页伪静态,对于搜索引擎友好,下面就是开启这个功能的说明!启用mod_rewrite模块在conf目录的httpd.conf文件中找到 LoadModule rewrite_ ...