POJ 2243 Knight Moves(BFS)
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.
InputThe 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.
OutputFor 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.
分析:简单的BFS求最短路,需要注意的是国际象棋中骑士和中国象棋中的马一样走日字
代码:
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
const int N = ;
typedef struct {
int x;
int y;
} P;
int dx[] = {-, , -, , -, , -, };
int dy[] = {-, -, -, -, , , , };
int vis[N][N];
int d[N][N];
char s[];
char e[];
int sx, sy, ex, ey;
int check(int x, int y) {
return x >= && x < && y >= && y < ;
}
void bfs() {
queue<P> que;
P p;
p.x = sx;
p.y = sy;
que.push(p);
vis[sx][sy] = ;
while(que.size()) {
P p = que.front();
que.pop();
int x = p.x;
int y = p.y;
if(x == ex && y == ey) {
printf("To get from %s to %s takes %d knight moves.\n", s, e, d[x][y]);
break;
}
for(int i = ; i < ; i++) {
int nx = x + dx[i], ny = y + dy[i];
if(check(nx, ny) && !vis[nx][ny]) {
vis[nx][ny] = ;
d[nx][ny] = d[x][y] + ;
P p;
p.x = nx;
p.y = ny;
que.push(p);
}
}
}
}
int main() {
while(cin >> s >> e) {
for(int i = ; i < N; i++) {
for(int j = ; j < N; j++) d[i][j] = vis[i][j] = ;
}
sx = s[] - '' - ;
sy = s[] - 'a';
ex = e[] - '' - ;
ey = e[] - 'a';
bfs();
}
return ;
}
POJ 2243 Knight Moves(BFS)的更多相关文章
- POJ 2243 Knight Moves
Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13222 Accepted: 7418 Des ...
- POJ 1915 Knight Moves(BFS+STL)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
- POJ 1915 Knight Moves
POJ 1915 Knight Moves Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29 ...
- OpenJudge/Poj 1915 Knight Moves
1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- UVA 439 Knight Moves(BFS)
Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- HDU1372:Knight Moves(BFS)
Knight Moves Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- hdu1372 Knight Moves BFS 搜索
简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...
随机推荐
- 滑动验证 和滑动图片验证JS
滑动验证 先放效果图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- python3+虹软2.0 离线人脸识别 demo
python3+虹软2.0的所有功能整合测试完成,并对虹软所有功能进行了封装,现提供demo主要功能,1.人脸识别2.人脸特征提取3.特征比对4.特征数据存储与比对其他特征没有添加 虹软SDK下载戳这 ...
- lambda表达式底层处理机制
为了支持函数式编程,Java 8引入了Lambda表达式,那么在Java 8中到底是如何实现Lambda表达式的呢? Lambda表达式经过编译之后,到底会生成什么东西呢? 在没有深入分析前,让我们先 ...
- angular2 脏检查机制
https://www.waitig.com/angular2-%E8%84%8F%E6%A3%80%E6%9F%A5%E8%BF%87%E7%A8%8B.html https://zhuanlan. ...
- Windows Live Wirter
安装: 下载: Windows Live Writer (QQ 里) windows live writer 日志服务器发生问题 更新账户信息 从字面"editPost"我们不难看 ...
- Juniper基础配置
root> show configuration | display set 配置按set行显示,查看的配置为未commit的配置(commit check)root# set sys ...
- 如何启动iis(Internet 信息服务(IIS)管理器)
Internet 信息服务(IIS)管理器 启动 IIS 管理器1.从“开始”菜单,指向“管理工具”,然后单击“Internet 信息服务 (IIS) 管理器”. 从“运行”对话框启动 IIS 管理器 ...
- 赵炯博士《Linux内核完全注释》
赵炯:男,1963年10月5日出生,江苏苏州人,汉族. 同济大学机械工程学院机械电子教研室副教授,从事教学和科研工作. 现在主要为硕士和博士研究生开设<计算机通信技术>.<计算机控制 ...
- MYSQL 总结——1
目录 数据类型 1.数值型 2.日期/时间型 3.文本型 数据库 1.创建数据库 2.删除数据库 数据表 1.创建数据表 create 2.删除数据表 drop 3.修改数据表 4.修改表名 5.dr ...
- Git创建新项目
1. git init 2. git remote add origin 3. git pull origin --allow-unrelated-histories 4. git push orig ...