hdu 1372 BFS
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most 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 input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying “To get from xx to yy takes n knight moves.”.
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 <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int vis[9][9];
int ans=999999999;
int sx,sy;
int ex,ey;
int dis[8][2]= {-1,-2,-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2};
struct node {
int x,y;
int step;
};
int check(int x,int y) {
if(x<1||y<1||x>8||y>8)
return 1;
if(vis[x][y])
return 1;
return 0;
}
void bfs() {
node a,next;
a.x=sx;
a.y=sy;
a.step=0;
queue<node> q;
q.push(a);
vis[sx][sy]=1;
while(!q.empty()) {
a=q.front();
q.pop();
if(a.x==ex&&a.y==ey) {
if(ans>a.step)
ans=a.step;
return;
}
for(int i=0; i<8; i++) {
next.x=a.x+dis[i][0];
next.y=a.y+dis[i][1];
if(check(next.x,next.y))
continue;
next.step=a.step+1;
vis[next.x][next.y]=1;
q.push(next);
}
}
}
int main() {
// freopen("input.txt","r",stdin);
char ch1[5],ch2[5];
while(scanf("%s %s",ch1,ch2)!=EOF) {
ans=999999999;
memset(vis,0,sizeof(vis));
sx=ch1[0]-'a'+1;
// cout<<sx<<endl;
sy=ch1[1]-'1'+1;
ex=ch2[0]-'a'+1;
ey=ch2[1]-'1'+1;
bfs();
printf("To get from %s to %s takes %d knight moves.\n",ch1,ch2,ans);
}
return 0;
}
hdu 1372 BFS的更多相关文章
- HDU<1372>/bfs
题目连接 简单bfs搜索 #include <set> #include <map> #include <cmath> #include <queue> ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- 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)
嗯... 题目链接: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 ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- 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 ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
随机推荐
- axios 发送post请求的时候会发送两次
第一次发送的时候会先发送OPTIONS, 第二次才发送POST, 解决方法: 引用qs模块 安装qs依赖 npm install qs --save 引入qs依赖 import qs from 'qs ...
- 配置合适的Visual Studio 2017 开发环境(其它版本的也适用)
1.VS 安装完成后,可以重新配置合适的开发环境 第一步: 第二步: 第三步: 第四步:选择合适自己的开发环境 这里我选择常规,具体的可以看窗口右边的说明
- HTML DOM 的nodeType属性
在HTML DOM中每一部分都是节点: HTML元素是元素节点 HTML中属性是属性节点 文本是文本节点 注释是注释节点 这时我们要给它区分开我们就可以使用HTML DOM的nodeType属性 no ...
- 使用Spark进行搜狗日志分析实例——map join的使用
map join相对reduce join来说,可以减少在shuff阶段的网络传输,从而提高效率,所以大表与小表关联时,尽量将小表数据先用广播变量导入内存,后面各个executor都可以直接使用 pa ...
- redisi应用--布隆过滤器
但是如果我们想知道某一个值是不是已经在 HyperLogLog 结构里面了,它就无能为力了,它只提供了 pfadd 和 pfcount 方法,没有提供 pfcontains 这种方法.
- 最近工作再弄基于bootstrap的定制sass
封装各种组件如 button table 当然..我只才做完两个. 比如table 抽出很多类以后可以配置的值 还有button 目录结构大致是 scss主要css文件 base和components ...
- Eclipse如何新建TOMCAT并配置Server Locations和Publishing属性
Eclipse如何新建TOMCAT并配置Server Locations和Publishing属性 2018年05月08日 23:10:33 ACGkaka_ 阅读数:1269 一.建立TOMCA ...
- TCP学习总结(四)
TCP连接管理 TCP运输连接有3个阶段, 即: 连接建立,数据传送和连接释放. 1. TCP的连接建立(3次握手) TCP连接的建立采用客户服务器方式.主动发起连接建立的应用进程叫做客户(clien ...
- linux系统中文件的权限
查看文件权限的语句: 在终端输入:ls -l xxx.xxx (xxx.xxx是文件名) 那么就会出现相类似的信息,主要都是这些:-rw-rw-r-- 一共有10位数 其中: 最前面那个 - 代表的是 ...
- ASP.NET Web API之消息拦截
要在action执行前后做额外处理,那么ActionFilterAttribute和ApiControllerActionInvoker就派上用场了.比如客户端请求发过来的参数为用户令牌字符串toke ...