转载请注明出处:

viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents

题目链接:

POJ:http://poj.org/problem?id=2243

HDU: 

pid=1372">http://acm.hdu.edu.cn/showproblem.php?

pid=1372

Problem Description
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 <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
#define M 1017
struct node
{
int x, y;
int step;
};
int xx[8] = {1,2,2,1,-1,-2,-2,-1};
int yy[8] = {2,1,-1,-2,-2,-1,1,2};
bool vis[M][M];
int n, ansx, ansy;
queue<node>q;
int BFS(int x, int y)
{ if(x == ansx && y == ansy)
return 0;
int dx, dy, i;
node front, rear;
front.x = x, front.y = y, front.step = 0;
q.push(front);
vis[x][y] = true;
while(!q.empty())
{
front = q.front();
q.pop();
for(i = 0; i < 8; i++)
{
dx = front.x+xx[i];
dy = front.y+yy[i];
if(dx>=1&&dx<=8&&dy>=1&&dy<=8&&!vis[dx][dy])
{
vis[dx][dy] = true;
if(dx == ansx && dy == ansy)
{
return front.step+1;
}
rear.x = dx, rear.y = dy, rear.step = front.step+1;
q.push(rear);
}
}
} }
int main()
{
char s,e;
int a1,a2;
while(~scanf("%c%d %c%d",&s,&a1,&e,&a2))
{
getchar();
while(!q.empty())
q.pop();
memset(vis,0,sizeof(vis));
int s1 = s-'a'+1;
int e1 = e-'a'+1;
ansx = e1, ansy = a2;
int ans = BFS(s1,a1);
printf("To get from %c%d to %c%d takes %d knight moves.\n",s,a1,e,a2,ans);
}
return 0;
}

poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)的更多相关文章

  1. HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏

    Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...

  2. poj2243 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...

  3. HDU-1372 Knight Moves (BFS)

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  4. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  5. 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 ...

  6. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  7. ZOJ 1091 Knight Moves(BFS)

    Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where you are t ...

  8. poj1915 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=1915 题意 输入正方形棋盘的边长.起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点. 思路 经典bfs题目. 代 ...

  9. HDU1372:Knight Moves(经典BFS题)

    HDU1372:Knight Moves(BFS)   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

随机推荐

  1. nyoj--1170--最大的数(数学技巧)

    最大的数 时间限制:1000 ms  |           内存限制:65535 KB 难度:3 描述 小明和小红在打赌说自己数学学的好,于是小花就给他们出题了,考考他们谁NB,题目是这样的给你N个 ...

  2. Nginx-虚拟主机配置问题

    Nginx-虚拟主机配置问题 标签(空格分隔): linux,php,nginx,larave 这两天突然想配置lnmp环境,学习下Nginx配置结果就遇到了下边的问题 Nginx: server下的 ...

  3. POJ 2828 线段树 逆序插入

    思路: 1.线段树 逆着插入就OK了 2.块状链表 (可是我并不会写) //By SiriusRen #include <cstdio> #include <cstring> ...

  4. AngularJs轻松入门(六)表单校验

    表单数据的校验对于提高WEB安全性意义不大,因为服务器接收到的请求不一定来自我们的前端页面,有可能来自别的站点,黑客可以自己做一个表单,把数据提交到我们的服务器(即跨站伪造请求),这样就绕过了前端页面 ...

  5. iOS开发——设置屏幕亮度

    想在APP里面调节屏幕的亮度,这只是个小众需求.而且,虽然可以直接调节手机的亮度,但是它还是个需求,客户有需求,剩下的就是我们的事了,好了,吐槽到此结束. 刚拿到这个需求的人,或许想的是直接对view ...

  6. XML和Schema命名空间详解

    来源:https://blog.csdn.net/wanghuan203/article/details/9204337 XML和Schema具有无关平台,技术厂商,简单,规范统一等特点,极具开放性, ...

  7. 【Henu ACM Round#17 F】Upgrading Array

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果我们对某一个位置i操作两次的话. 显然结果就和操作一次一样. 因为第一次操作过后1..i这些数字就变成是互质的了. gcd为1. ...

  8. silverlight wcf mvvm

    近期工作比較忙.也没有时间发表新内容,今天有点时间,就顺便写点,说说近期开发的一套系统心得. 我刚去这个公司已经将前端确定要用Silverlight,我不知道为什么要选择这个,或许是为以后转C/S系统 ...

  9. WEB前端,混合排版,有的宽有的窄,滚动会出现空白处,怎么办。

    多数时候出现空白都是由于有滚动栏滚到一边就会产生空白. overflow-x: hidden; 在最大图的那个div里写这句.

  10. Objective-C学习笔记(十)——循环语句for和do-while的使用

    在OC中.除了while这样的循环方式外,还有另外for循环和do-while循环.它们在不同的业务逻辑下会有不同的作用.能够和C语言和Java对照着学习. (一)代码一: int main(int ...