Knight Moves(BFS,走’日‘字)
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8831 Accepted Submission(s): 5202
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.
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.
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int dx[]={,-,,-,,-,,-};
int dy[]={,-,-,,,-,-,};
int vis[][];
struct point
{
int x;
int y;
int t;
}st,tem,nex;
int sx,sy,ex,ey,tim;
char a,b,c,d;
void bfs()
{
queue<point> s;
st.x=sx,st.y=sy;
st.t=;
s.push(st);
memset(vis,,sizeof(vis));
while(!s.empty())
{
tem=s.front();
s.pop(); if(tem.x==ex&&tem.y==ey)
{
tim=tem.t;
return;
}
if(vis[tem.x][tem.y]||tem.x<=||tem.y<=||tem.x>||tem.y>)
continue;
vis[tem.x][tem.y]=;
for(int i=;i<;i++)
{
int nx=tem.x+dx[i];
int ny=tem.y+dy[i];
int nt=tem.t+;
nex.x=nx,nex.y=ny,nex.t=nt;
s.push(nex);
}
}
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%c%c %c%c",&a,&b,&c,&d)!=EOF)
{
getchar();
tim=;
sy=a-'a'+,sx=b-'';
ey=c-'a'+,ex=d-'';
bfs();
printf("To get from %c%c to %c%c takes %d knight moves.\n",a,b,c,d,tim);
}
}
Knight Moves(BFS,走’日‘字)的更多相关文章
- hdu1372 Knight Moves BFS 搜索
简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...
- POJ 2243 Knight Moves(BFS)
POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- hdu5794 A Simple Chess 容斥+Lucas 从(1,1)开始出发,每一步从(x1,y1)到达(x2,y2)满足(x2−x1)^2+(y2−y1)^2=5, x2>x1,y2>y1; 其实就是走日字。而且是往(n,m)方向走的日字。还有r个障碍物,障碍物不可以到达。求(1,1)到(n,m)的路径条数。
A Simple Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- UVA 439 Knight Moves(BFS)
Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- POJ 1915 Knight Moves(BFS+STL)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
- HDU1372:Knight Moves(BFS)
Knight Moves Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- POJ-1915 Knight Moves (BFS)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26952 Accepted: 12721 De ...
随机推荐
- JavaWeb学习笔记--2.jsp指令元素、动作元素、内置对象
目录: 一.指令 1.1 page指令 :page指令有13种属性 1.2 include指令:include指令有1种属性,为file.(注意<%@ include%>和<jsp: ...
- 【测试技术】ant里面mapper的详细用法
ant里面mapper标签是和fileset配合使用的,目的就是把fileset取出的文件名转成指定的样式.其实看懂官方文档后,感觉真心没啥好写的.但是还是写一下把. 1.<mapper typ ...
- android AndroidManifest.xml 多个android.intent.action.MAIN (
可以的 ,一个程序是可以有多个入口的.如果你设置两个,并且category都是android.intent.category.LAUNCHER,那么你就 会发现你的手机中就会出现两个快捷方式,也就是两 ...
- haporxy 负载elasticsearch
<pre name="code" class="html">-bash-4.1# cat /etc/haproxy/haproxy.cfg glob ...
- NLS_LANG SIMPLIFIED CHINESE_CHINA.AL32UTF8 和american_america.AL32UTF8
oadb01:/home/oracle> echo $NLS_LANG SIMPLIFIED CHINESE_CHINA.AL32UTF8 oadb01:/home/oracle> sql ...
- 提高效率的便签By番茄时间管理 win7标签,小功能,大作用
今日待办 把一些重要的事情,列入其中. 着重处理. 活动清单 罗列一些最近需要做的事情,不一定按照紧急重要的程度. 把活动清单中的事情,按照实际情况,安排到今日待办当中. 还有一个我喜欢的'头脑风暴' ...
- 转:MVC分页
原文地址:http://www.cnblogs.com/iamlilinfeng/p/4075292.html 分页总是搞得我很烦,也是因为刚接触,貌似有好多插件,之前在用一个,可是后来发现一翻页原来 ...
- 【转】repo 的一些用法和理解-不错
原文网址:http://blog.csdn.net/yasin_lee/article/details/5975068 repo的用法(zz) 注:repo只是google用Python脚本写的调用g ...
- VMdomainXml
1,One,Euc,Ostack 虚拟磁盘镜像制作方法[Windows,Linux,类linux OS](1,基于ios部署系统生成img,2基于vm xml定义部署系统生成img qcow2) 如需 ...
- MySQL的备份和还原
MySQL的备份和还原 备份:副本 RAID1,RAID10:保证硬件损坏而不会业务中止: DROP TABLE mydb.tb1; 备份类型: 热备份.温备份和冷备 ...