Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10542    Accepted Submission(s): 6211

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.

Source

University of
Ulm Local Contest 1996

原文大意:对一个国际象棋的棋盘,每次输入骑士(骑士=马)的起始位置和目标位置,问至少要多少步才能走到。

解题思路:其实是一个裸的广搜,贴上充数用的,输入输出打麻烦了。

#include<stdio.h>

#include<string.h>

#include<queue>

using namespace std;

struct mp

  {

       int x,y,step;

       mp(int x,int y,int step):x(x),y(y),step(step){}

  };

queue<mp> q;

void bfs(int xbegin,int ybegin,int xend,int yend)

  {

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

       int i,x,y,visit[9][9];

       char sans1,sans2;

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

       if(xbegin==xend&&ybegin==yend)

         {

             sans1=xbegin+'a'-1;

             sans2=xend+'a'-1;

             printf("To get from %c%d to %c%d takes %d knight moves.\n",sans1,ybegin,sans2,yend,0);

             return;

          }

       while(!q.empty()) q.pop();

       q.push(mp(xbegin,ybegin,0));

       visit[xbegin][ybegin]=1;

       while(!q.empty())

         {

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

               {

                x=q.front().x+move[0][i];y=q.front().y+move[1][i];

                if(x==xend&&y==yend)

                   {

                     sans1=xbegin+'a'-1;

                     sans2=xend+'a'-1;

                     printf("To get from %c%d to %c%d takes %d knight moves.\n",sans1,ybegin,sans2,yend,q.front().step+1);

                     return;

                   }

                if(x>0&&x<9&&y>0&&y<9&&(!visit[x][y]))

                 {

                             q.push(mp(x,y,q.front().step+1));

                             visit[x][y]=1;

                       }

                     }

             q.pop();

          }

       return;

  }

int main()

  {

       char s[5];

        int x_start,x_end,y_start,y_end;

       while(scanf("%s",&s)!=EOF)

         {

             x_start=s[0]-'a'+1;

             y_start=s[1]-'0';

             scanf("%s",s);

             x_end=s[0]-'a'+1;

             y_end=s[1]-'0';

             bfs(x_start,y_start,x_end,y_end);

          }

       return 0;

  }

  

[宽度优先搜索] HDU 1372 Knight Moves的更多相关文章

  1. HDU 1372 Knight Moves(最简单也是最经典的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  2. HDU 1372 Knight Moves(bfs)

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

  3. HDU 1372 Knight Moves (bfs)

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

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

  5. HDU 1372 Knight Moves 题解

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

  6. HDU 1372 Knight Moves

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

  7. HDU 1372 Knight Moves【BFS】

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

  8. HDOJ/HDU 1372 Knight Moves(经典BFS)

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

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

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

随机推荐

  1. 解决Firefox浏览器每次打开都弹出导入向导的问题

    每次打开Firefox浏览器都会弹出导入向导这个页面,只有这个页面关闭后,Firefox界面才会打开. 解决办法: C:\Users\{用户名}\AppData\Roaming\Mozilla\Fir ...

  2. paper 107:图像的白平衡

    所谓白平衡(White Balance):指在图像处理的过程中,对原本材质为白色的物体的图像进行色彩还原,去除外部光源色温的影响,使其在照片上也显示白色.也就是不管在任何光源下,都能将白色物体还原为白 ...

  3. 获取dll中根目录

    AppDomain.CurrentDomain.BaseDirectory获取当前应用程序域的基目录 好像是万能的:form:可执行文件路径控制台:输出路径web:根目录

  4. 批处理命令——set

    [1]set命令简介 set,设置. [2]set命令使用 1. 打印系统环境变量.set命令可以打印系统所有的环境变量信息. 应用示例:新建文本文件,命名为set_sys,修改文件类型为bat,用N ...

  5. Python正则处理多行日志一例

    正则表达式基础知识请参阅<正则表达式基础知识>,本文使用正则表达式来匹配多行日志并从中解析出相应的信息. 假设现在有这样的SQL日志: SELECT * FROM open_app WHE ...

  6. UBIFS 文件系统分析1 - 磁盘结构【转】

    转自:http://blog.csdn.net/kickxxx/article/details/7109662 版权声明:本文为博主原创文章,未经博主允许不得转载. ubifs磁盘结构 UBIFS文件 ...

  7. Dynamics AX 2012 R2 RemoteApp导出项目报错

        今天,Reinhard使用RemoteApp的方式登陆AX开发环境,对项目文件进行修改后,习惯性地将项目导出到Reinhard的电脑上,做个备份.但是导出时弹出错误提示框,报以下错误:     ...

  8. oracle和mysql获取当前时间的不同

    在oracle中:select sysdate from dual,而且必须加上from dual,否则报错,只支持sysdate ,和mysql不一样,mysql的是sysdate()函数: 在my ...

  9. LINK : fatal error LNK1104: 无法打开文件“LIBCD.lib”

    出现这类问题一般是由于所运行的项目是VC6(或者vs2003)创建的,而后又用VS2005或者更高版本工具打开项目导致的,原因都是因为LIBCD.lib文件被更改了.要解决问题的话,只要在链接设置那里 ...

  10. img的hover事件闪动

    今天给同学写一个相册照片鼠标浮动显示细节的效果,遇到了闪动的bug,也顺利解决,就写下来跟大家分享. 我使用的是'标签:hover + 标签'的形式,如果使用jquery的mouseover.mous ...