POJ 2243 Knight Moves

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.

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)的更多相关文章

  1. POJ 2243 Knight Moves

    Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13222   Accepted: 7418 Des ...

  2. POJ 1915 Knight Moves(BFS+STL)

     Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 ...

  3. POJ 1915 Knight Moves

    POJ 1915 Knight Moves Knight Moves   Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29 ...

  4. OpenJudge/Poj 1915 Knight Moves

    1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...

  5. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

  6. UVA 439 Knight Moves(BFS)

    Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...

  7. HDU 1372 Knight Moves(BFS)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  8. HDU1372:Knight Moves(BFS)

    Knight Moves Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  9. hdu1372 Knight Moves BFS 搜索

    简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...

随机推荐

  1. MVC路由 路由的三种扩展 替换MVC内置的Handler

    Global.asax 是 程序入口文件 路由配置   为什么localhost:8088/Home/Index/1 能返问到我们写的 会去掉前缀跟端口号  变成Home/Index/1 用这个跟路由 ...

  2. 学习笔记35—大话 Word和Excel

    1.word中,加粗表格线条:设计---->笔画粗细. 2.Excel中,冻结某一行:点击工具栏中的视图→冻结空格→冻结单元格. 3.word中,输入卡方符号:插入----> 符号 --- ...

  3. x1c 2017 安装mint18的坑——grub2

    折腾一天,死活安装不上.用U盘安装,能进入pe,但是安装时提示无法将grub2安装到/target/ 不论如何分区.如何修改BIOS 安全启动和 启动模式.都是这个问题. ubuntu16.04.3 ...

  4. Lua面向对象之二:类继承

    1.类继承 ①代码 Sharp = { } --① 父类 function Sharp:new() local new_sharp = { } self.__index = self --②,self ...

  5. tcpdump使用方法总结

    举例: 1.针对指定网卡eth0抓包 tcpdump -i eth0 2.过滤主机 tcpdump -i eth0 host 192.168.1.1 tcpdump -i eth0 src host ...

  6. using 自动释放资源示例

    我们在使用SqlConnection的时候可以加入using,那么在using语句结束后就会自动关闭连接.那么这种情况是怎么是实现的呢?我们能够自己写一个类似于SqlConnection的类来让usi ...

  7. centos 安装npm node

    最近那vue全套造了个管理系统的轮子,发现node简直太好用了. elment-UI的出现就是不懂ui设计的后台工程师的福音~ 正好自己买的两个云服务器空闲着没用,就拿来试试看了 首先软件都安装在/u ...

  8. Windows 7 Update Settings Disabled (Important Updates Grayed Out)

    This worked for me: 1) Hold WindowsKey + R     (is hold Start & press R on your keyboard) 2) Typ ...

  9. Linq to XML 增删改查

    Linq to XML同样是对原C#访问XML文件的方法的封装,简化了用xpath进行xml的查询以及增加,修改,删除xml元素的操作.C#访问XML文件的常用类:XmlDocument,XmlEle ...

  10. public class feign.RetryableException feign.RetryableException: Connection refused (Connection refused) executing POST http://common-wx/wx/auth/client/token/v1

    一.异常出现的场景 Spring Cloud内部两个服务A和B,A调用B时,抛出该异常.提示连接拒绝 public class feign.RetryableException feign.Retry ...