HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏
Knight Moves
Problem Description
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 row on the chessboard.
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.
————————————————————————————————————————————
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
int map[10][10];
int dir[8][2]={{-2,-1},{-1,-2},{-1,2},{-2,1},{2,1},{1,2},{2,-1},{1,-2}}; struct node
{
int x,y,cnt; };
bool cheak(int i,int j)
{
if(i<1||i>8||j<1||j>8)
return 0;
else
return 1; }
int bfs(int si,int sj,int di,int dj)
{
queue<node>q;
node f,d;
f.x=si;
f.y=sj;
f.cnt=0;
q.push(f);
while(!q.empty())
{
f=q.front();
q.pop();
if(f.x==di&&f.y==dj)
return f.cnt;
for(int i=0;i<8;i++)
{
d.x=f.x+dir[i][0];
d.y=f.y+dir[i][1];
if(cheak(d.x,d.y))
{
d.cnt=f.cnt+1;
q.push(d);
}
}
}
return -1;
} int main()
{
char a,b;
int xa,xb,ya,yb;
while(~scanf("%c%d %c%d",&a,&ya,&b,&yb))
{
getchar();
xa=a-'a'+1;
xb=b-'a'+1;
int ans=bfs(xa,ya,xb,yb);
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,ya,b,yb,ans);
}
return 0;
}
HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏的更多相关文章
- THE DRUNK JAILER 分类: POJ 2015-06-10 14:50 13人阅读 评论(0) 收藏
THE DRUNK JAILER Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24918 Accepted: 1563 ...
- iOS 发布流程 分类: ios相关 app相关 2015-05-22 14:50 186人阅读 评论(0) 收藏
1.登陆苹果开发者中心http://developer.apple.com(99美元账号) 2.进入itunes connect 3.选择Manage Your Apps 4.选择Add New Ap ...
- js中二维数组的创建方法 2017-04-04 14:50 120人阅读 评论(0) 收藏
法一:var myarr=[[0,1,2],[1,2,3]]; 将[0,1,2]看做原来的0,将[1,2,3]看做原来的1,而二者又分别为子数组 如myarr[0][1]=1,myarr[1][1]= ...
- A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏
A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...
- HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏
Nightmare Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth w ...
- iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏
一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...
- Hdu2181 哈密顿绕行世界问题 2017-01-18 14:46 45人阅读 评论(0) 收藏
哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- Hdu2102 A计划 2017-01-18 14:40 60人阅读 评论(0) 收藏
A计划 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissio ...
- Hadoop入门经典:WordCount 分类: A1_HADOOP 2014-08-20 14:43 2514人阅读 评论(0) 收藏
以下程序在hadoop1.2.1上测试成功. 本例先将源代码呈现,然后详细说明执行步骤,最后对源代码及执行过程进行分析. 一.源代码 package org.jediael.hadoopdemo.wo ...
随机推荐
- C++17尝鲜:类模板中的模板参数自动推导
模板参数自动推导 在C++17之前,类模板构造器的模板参数是不能像函数模板的模板参数那样被自动推导的,比如我们无法写 std::pair a{1, "a"s}; // C++17 ...
- java包 命名规范 [转]
Java的包名都有小写单词组成,类名首字母大写:包的路径符合所开发的 系统模块的 定义,比如生产对生产,物资对物资,基础类对基础类.以便看了包名就明白是哪个模块,从而直接到对应包里找相应的实现. 由于 ...
- Tomcat数据库连接池配置
Tomcat数据库连接池配置 1. Server.xml的配置 (1)找到tomcat所在目录下的conf\server.xml文件 (2)在文件最后一个</host> ...
- powerdns
powerdns http://bbs.51cto.com/thread-880297-1.html https://blog.csdn.net/kepa520/article/details/791 ...
- php ip2long 负数问题
官方网站: Note: 因为PHP的 integer 类型是有符号,并且有许多的IP地址讲导致在32位系统的情况下为负数, 你需要使用 "%u" 进行转换通过 sprintf() ...
- 大型运输行业实战_day09_1_日期转换与My97DatePicker插件使用
1.日期转换 1.1字符串类型转换成时间Date类型 /** * 给定字符串 转变 为 Date 类型 * @param date 时间 * @param format 时间格式 如:yyyy-MM- ...
- SpringBoot application.yml logback.xml 多环境
启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 java -jar app.j ...
- Java GC的原理
Java GC(garbage collec,垃圾收集,回收) GC是对JVM中的内存进行标记和回收,Sun公司的JDK用的虚拟机都是HotSpot 对象化的实例是放在heap堆内存中的,这里讲的分代 ...
- 在c#中设置Excel格式
生成excel的时候有时候需要设置单元格的一些属性,可以参考一下: range.NumberFormatLocal = "@"; //设置单元格格式为文本 ange.get_Ran ...
- TZOJ 4839 麦森数(模拟快速幂)
描述 形如2^P-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2^P-1不一定也是素数.到1998年底,人们已找到了37个麦森数.最大的一个是P=3021377,它有9 ...