HDU-1372 Knight Moves (BFS)
Problem Description
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
Output
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.
Source
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int visit[][];
int x1,y1,x2,y2;
int dir[][]={{-,-},{-,},{-,},{-,-},
{,-},{,},{,},{,-}};
struct node
{
int x,y;
int step;
};
int BFS(int step)
{
if(x1==x2&&y1==y2)
return step;
int i,x,y,s;
queue<node> q;
node top,temp;
top.x=x1,top.y=y1,top.step=;
q.push(top);
while(!q.empty())
{
top=q.front();
q.pop();
for(i=;i<;i++)
{
x=top.x+dir[i][];
y=top.y+dir[i][];
s=top.step;
if(x>&&y>&&x<=&&y<=&&visit[x][y]==)
{
visit[x][y]=;
++s;
if(x==x2&&y==y2)
return s;
temp.x=x;
temp.y=y;
temp.step=s;
q.push(temp);
}
}
}
return -;
}
int main()
{
char str1[],str2[],t;
while(~scanf("%s%s",str1,str2))
{
memset(visit,,sizeof(visit));
x1=str1[]-'a'+;
y1=str1[]-'';
x2=str2[]-'a'+;
y2=str2[]-'';
visit[x1][y1]=;
t=BFS();
printf("To get from %s to %s takes %d knight moves.\n",str1,str2,t);
}
return ;
}
HDU-1372 Knight Moves (BFS)的更多相关文章
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- 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 ...
- HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU 1372 Knight Moves (广搜)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- 杭州电 1372 Knight Moves(全站搜索模板称号)
http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others) ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
随机推荐
- xcode 最近打开文件列表显示为空或不显示最近打开的项目或(no recent projects)解决办法
如果使用的是10.10 系统,打开系统设置-->进入通用-->在最下面的"最近使用的项目"中将0改为你可以接受的选项 如果不是10.10,那么就从系统偏好设置---&g ...
- 在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法
在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法 ==========方法1: --------1. 选相应 ...
- Codevs 1191 数轴染色
1191 数轴染色 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色. ...
- C++动态分配内存
动态分配(Dynamic Memory)内存是指在程序运行时(runtime)根据用户输入的需要来分配相应的内存空间. 1.内存分配操作符new 和 new[] Example: (1)给单个元素动态 ...
- js微博发布框
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- web api 跨域请求,ajax跨域调用webapi
1.跨域问题仅仅发生在Javascript发起AJAX调用,或者Silverlight发起服务调用时,其根本原因是因为浏览器对于这两种请求,所给予的权限是较低的,通常只允许调用本域中的资源,除非目标服 ...
- Dataset
1,if(ds == null) 这是判断内存中的数据集是否为空,说明DATASET为空,行和列都不存在!! 2,if(ds.Tables[0].Count == 0) 这应该是在内存中存在一个DAT ...
- window.onresize 多次触发的解决方法
用了window.onresize但是发现每次 onresize 后页面中状态总是不对,下面与大家分享下onresize 事件多次触发的解决方法. 之前做一个扩展,需要在改变窗口大小的时候保证页面显示 ...
- php操作memcache的用法、详解和方法介绍
1.简介 memcache模块是一个高效的守护进程,提供用于内存缓存的过程式程序和面向对象的方便的接口,特别是对于设计动态web程序时减少对数据库的访问. memcache也提供用于通信对话(sess ...
- smarty
模板引擎是用于把模板文件和数据内容合并在一起的程序,便于网站开发有利于代码分离和维护,了解一个模板最好知道其工作原理,以便于实现一通万通. 模板文件一般是HTML xml js等类型文件,如果不用模板 ...