转载请注明出处:

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. Lambda表达式-使用说明

    jdk8已经发布4年,其中有一个特性:Lambda,它是一个令开发者便捷开发的一种方式,Lambda Expression (Lambda表达式)是为了让java提供一种面向函数编程,原本在jdk8之 ...

  2. CSS 奇技淫巧

    用button实现垂直水平居中对齐 http://www.baidufe.com/item/113ce1894da2b5203669.html “今天同事在群里分享了一个特牛叉的前端小技巧:用butt ...

  3. Servlet监听器及在线用户

    Servlet中的监听器分为三种类型Ⅰ 监听ServletContext.Request.Session作用域的创建和销毁 (1)ServletContextListener (2)HttpSessi ...

  4. Generic type test java

    package test; public class GenericTest { public class Room<T> { private T t; public void add(T ...

  5. webpack(零工程构建一个前端项目)详解

    工作流程记录: 1.初始化项目:npm init -y 2.安装webpack,vue,vue-loader npm install webpack vue vue-loader 3.按装之后根据警告 ...

  6. Redis特点以及安装

       Mysql 的数据 是以"文件形式存储在硬盘"里边.硬盘运行速度相比较CPU.内存是排在第三的.而 Redis 是内存高速缓存数据库,运行速度比 Mysql 速度快,也支持数 ...

  7. Ubuntu(kali)开启mysql远程连接

    Linux 默认关闭mysql的远程连接,编辑 /etc/mysql/my.cnf 文件, 把里面的 bind-address = 127.0.0.1 改成 bind-address = 0.0.0. ...

  8. C#中使用Dictionary实现Map数据结构——VC编程网

    转载自: http://blog.51cto.com/psnx168 在VC中使用过CMap以及在Java中使用过Map的朋友应该很熟悉,使用Map可以方便实现基于键值对数据的处理,在C#中,你就需要 ...

  9. 洛谷1462 通往奥格瑞玛的道路 最短路&&二分

    SPFA和二分的使用 跑一下最短路看看能不能回到奥格瑞玛,二分收费最多的点 #include<iostream> #include<cstdio> #include<cs ...

  10. ECNUOJ 2143 端午节快乐

    端午节快乐 Time Limit:1000MS Memory Limit:65536KBTotal Submit:1720 Accepted:868 Description  有一段有趣的传说.公元前 ...