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, ...
随机推荐
- AI 帮助涂鸦
这个小工具挺有意思,可以在涂鸦的同时自动猜测你要画什么,并自动完成. https://quickdraw.withgoogle.com/
- 解决flask的502错误:upstream prematurely closed connection while reading response header from upstream
我在使用 tiangolo/uwsgi-nginx-flask 部署flask应用的时候,经常运行一会儿就出现502错误,重新启动容器后,就恢复. 且经常会出现数据更新后,刷新结果不一致. docke ...
- 导航菜单点击图片切换--jquery
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Python随笔--继承
- HYPERSPACE
Windows中,不管是应用程序还是内核程序,都不能直接访问物理内存,所有非IO指令都只能访问虚拟内存地址,如Mov eax, DWORD PTR[虚拟地址]形式,但是,有时候,我们明明已经知道了某个 ...
- shiro三连斩之第二斩(SSM)
在SSM框架中使用shiro.环境 使用idea工具. 最主要的大概是配置文件如何配置吧. 1配置maven依赖 <?xml version="1.0" encoding=& ...
- 【整理】QT .pro文件中的变量说明
注释 以"#"开始的行,直到结束 模板变量 告诉qmake生成哪种makefile TEMPLATE = app 其中 app - 表示该工程建立一个应用程序的makefile.这 ...
- Git问题总结
1 git push origin master 提示Everything up-to-date 表示本地仓库和远程仓库是一致的,没有更新,需要检查自己的提交. 2 git push origin m ...
- Problem E: 平面上的点和线——Point类、Line类 (V)
Description 在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定,两点确定一条线段.现在我们封装一个“Point类”和“Line类”来实现平面上的点的操作. 根据“append ...
- Java randomString
public static String randomString(int strLength) { Random rnd = ThreadLocalRandom.current(); StringB ...