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

#include

#include

#include

#define maxn 10

using namespace std;

int
dir[8][2]={{-2,-1},{-1,-2},{1,-2},{2,-1},{-2,1},{-1,2},{1,2},{2,1}};

int visit[maxn][maxn];

int check(int a,int b)

{

   
if(a<1||a>8||b<1||b>8||visit[a][b])

       
return 1;

    else

       
return 0;

}

struct node

{

    int
x,y;

    int
times;

}start,fr,a;

int bfs(int sx,int sy,int x,int y)

{

   
memset(visit,0,sizeof(visit));

    queue
Q;

   
start.x=sx;

   
start.y=sy;

   
start.times=0;

   
visit[start.x][start.y]=1;

   
Q.push(start);

   
while(!Q.empty())

    {

       
fr=Q.front();

       
Q.pop();

       
//cout<<"x="<<x<<"
"<<"y="<<y<<endl;

       
if(fr.x==x&&fr.y==y)

           
return fr.times;

       
for(int i=0;i<8;i++)

       
{

           
a.x=fr.x+dir[i][0];

           
a.y=fr.y+dir[i][1];

           
if(check(a.x,a.y))

               
continue;

           
a.times=fr.times+1;

           
visit[a.x][a.y]=1;

           
Q.push(a);

       
}

    }


}

int main()

{

   
//freopen("in.txt", "r", stdin);

    char
a,b;

    int
sx,sy,x,y;

   
while(~scanf("%c%d %c%d\n",&a,&sy,&b,&y))

    {

       
//cout<<"sx="<<a<<endl;

       
//cout<<"sy="<<sy<<endl;

       
//cout<<"x="<<b<<endl;

       
//cout<<"y="<<y<<endl;

       
sx=a-'a'+1;

       
x=b-'a'+1;

       
//坐标转化成数字

       
int ans=bfs(sx,sy,x,y);

       
printf("To get from %c%d to %c%d takes %d knight
moves.\n",a,sy,b,y,ans);

    }

    return
0;

}


Knight Moves的更多相关文章

  1. Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  2. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  3. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  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. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  6. 【POJ 2243】Knight Moves

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

  7. hdu Knight Moves

    这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...

  8. HDU 1372 (搜索方向稍有改变) Knight Moves

    其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...

  9. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  10. UVA 439 Knight Moves

      // 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...

随机推荐

  1. XML(二)之DTD——XML文件约束

    前面介绍了XML的作用和基本的格式,今天我给大家分享的是关于XML的约束.废话不多说,我们直接来正题! 一.DTD简介 1.1.DTD概述 DTD(Document Type Definition,文 ...

  2. grunt学习笔记1 理论知识

    你需要检查js语法错误,然后再去压缩js代码.如果这两步你都去手动操作,会耗费很多成本.Grunt就能让你省去这些手动操作的成本. “—save-dev”的意思是,在当前目录安装grunt的同时,顺便 ...

  3. PHP垃圾回收机制理解

    使用的是"引用计数"方式进行回收.简单地理解的话,就是每个分配的内存区域都有一个计数器,记录有多少个变量指针指向这片内存.当指向该片内存的指针数量为0,那么该片内存区域就可以被回收 ...

  4. SYSTEM_INFO

    SYSTEM_INFO结构体包含了当前计算机的信息.这个信息包括计算机的体系结构.中央处理器的类型.系统中中央处理器的数量.页面的大小以及其他信息. SYSTEM_INFO,Win32 API函数Ge ...

  5. 51nod 1270 数组的最大代价 思路:简单动态规划

    这题是看起来很复杂,但是换个思路就简单了的题目. 首先每个点要么取b[i],要么取1,因为取中间值毫无意义,不能增加最大代价S. 用一个二维数组做动态规划就很简单了. dp[i][0]表示第i个点取1 ...

  6. 关于 LindedList 我想说

    LinkedList 的一些认识: 继承于AbstractSequentialList的双向链表,可以被当作堆栈.队列或双端队列进行操作 有序,非线程安全的双向链表,默认使用尾部插入法 适用于频繁新增 ...

  7. Webx项目的获取与验证

    在JavaWeb环境配置后就可进行Webx实例项目的获取与研读了. 1.创建一个初始的Demo工程. 1)下载 Webx Maven 项目的目录结构Artifact插件. archetype-webx ...

  8. 机器学习实战K-近邻算法

    今天开始学习机器学习,第一章是K-近邻算法,有不对的地方请指正 大概总结一下近邻算法写分类器步骤: 1. 计算测试数据与已知数据的特征值的距离,离得越近越相似 2. 取距离最近的K个已知数据的所属分类 ...

  9. 自签名的https证书是不安全的

    一.项目内的需求 我们做的app都是企业级的应用,而企业级的应用的下载需要遵循itms协议,itms协议下需要https链接,这就需要你的服务器支持https的协议,该协议需要申请SSL证书,我们测试 ...

  10. winwebmail设置能用foxmail收发邮件

    域名解析注意 1.首先做A记录解析: 主机名处:输入 mail IP地址处:输入IP地址 2.做MX记录: 主机名处: 大都保持空输入,什么也不用输入   TTL:默认就可以了,不需要改动 优先级:一 ...