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 ...
随机推荐
- Python环境配置, atom-python配置
环境变量 路径 在window下配置环境变量,配置到文件夹级就可以了: D:\program\python3 编码 因为windows默认的编码是ASIIC,所以使用atom时候中文是乱码,需要在环境 ...
- 根据条件决定My97DatePicker日期控件弹出的日期格式
代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- Mysql一些记忆
mysql修改密码报错是yum 安装mysql5.7 是 出现无法登陆问题以及mysql error You must reset your password using ALTER USER sta ...
- webservice jaxws header验证
@WebService @HandlerChain public class UserService { ... } package com.xx.ws.header; import org.w3c. ...
- poj3616(LIS简单变式)
题目链接:http://poj.org/problem?id=3616 思路: 我的第一反应是背包,因为每个interval要么选择要么不选,后来发现状态方程很难写出来.后来想一想发现就是LIS的简单 ...
- 第六章 图(c)广度优先搜索
- GridView自定义分页样式(上一页,下一页,到第几页)
今天要为网站做一个文章列表,发现GridView的分页样式很难看,于是结合网上的例子,自己做了一个.不是很美观,不过还是很实用的,先看下效果吧,如图(1). 图(1)GridView分页效果 自定义G ...
- day5:vcp考试
Q81. An administrator needs to recover disk space on a previously-used thin provisioned virtual disk ...
- mybatis结合redis实战二级缓存
之前的文章中我们意见分析了一级缓存.二级缓存的相关源码和基本原理,今天我们来分享下了mybatis二级缓存和redis的结合,当然mybatis二级缓存也可以和ehcache.memcache.OSC ...
- yii创建与设置默认控制器并载入模板
yii创建与设置默认控制器并载入模板 一.创建控制器 在protected下的controllers文件夹中创建自定义的控制器文件,比如: IndexController.php (文件名首字母大写) ...