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 ...
随机推荐
- 使用grid++report打印选中行
接上一篇<hibernate+spring+mvc+Easyui框架模式下使用grid++report的总结>对grid++report做进一步开发 先写一下实现流程: 1.默认为全部载入 ...
- debian系统下安装ssh服务
它是什么?? SSH 为 Secure Shell 的缩写,简单地说,SSH 为建立在应用层基础上的安全协议.SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议.利用 SSH 协议可 ...
- 让libvirt支持win7运行2核以上
现在人在部署OpenStack之后会发现按照配额运行Linux的虚拟机没有问题,但是运行windows的虚拟机会发现如果配置2个以上的核则无法识别,windows server也最多支持到4个核.无法 ...
- Quartz.net Cron表达式
由7段构成:秒 分 时 日 月 星期 年(可选)"-" :表示范围 MON-WED表示星期一到星期三"," :表示列举 MON,WEB表示星期一和星期三&qu ...
- LeetCode_Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【转】多文件目录下makefile文件递归执行编译所有c文件
首先说说本次嵌套执行makefile文件的目的:只需make根目录下的makefile文件,即可编译所有c文件,包括子目录下的. 意义:自动化编译行为,以后编译自己的c文件时可把这些makefile文 ...
- UIView 转 UIImage
这个方法很实用,特别是在做水印相机得时候... - (UIImage*) imageWithUIView:(UIView*) view{ // 创建一个bitmap的context // 并把它设置成 ...
- Android学习总结——欢迎页和导航页的实现
activity_welcome.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
- 机房收费系统中的Grid++Report报表设计器的应用
在进行账单查询功能的时候我应用了Grid++Report报表设计器,下面我就为大家介绍一下,还望大家多多指点. 首先,在Grid++Report报表设计器中进行报表界面的设置.在属性编辑窗口中这里对报 ...
- 使用ant自动编译、打包生成apk文件
上次使用命令行生成apk文件<Android 命令行编译.打包生成apk文件>,学习命令行生成的目的是为了编写ant打下基础. 一. ant环境 下载ant包,配置环境变量 二.ant编译 ...