HDU1372:Knight Moves(BFS)

 

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

 

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 Specification

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 Specification

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.

题解:骑士走日,和中国象棋中的马一样走日。

广搜bfs就可以

AC代码:

#include <iostream>
#include <queue>
#include<string.h>
using namespace std;
int s1,s2,e1,e2;
int tu[][];
int xx[] = {, , , , -, -, -, -};//x坐标变化
int yy[] = {, , -, -, , , -, -};//y坐标变化
int bfs()
{
memset(tu,,sizeof(tu));//数组值全为0
queue<int> q;
int x1,y1,x2,y2;
tu[s1][s2]=;//
q.push(s1);
q.push(s2);
while(!q.empty())
{
x1=q.front();
q.pop();
y1=q.front();
q.pop();
if(x1==e1&&y1==e2)//在原地不动
return tu[x1][y1];
for(int i=; i<; i++)
{
x2=x1+xx[i];
y2=y1+yy[i];
if(x2<||x2>||y2<||y2>||tu[x2][y2]>)
continue;
tu[x2][y2]=tu[x1][y1]+;
q.push(x2);
q.push(y2);
}
}
return ;
}
int main()
{
char a[],b[];
int total;
while(cin>>a>>b)
{
s1=a[]-'a';//输入的字母,是列,从第一列a开始,减去'a',转换
s2=a[]-'';//输入的数字,是行,从的第一行开始,减去'1',转换
e1=b[]-'a';
e2=b[]-'';
total=bfs();
cout<<"To get from "<<a<<" to "<<b<<" takes "<<total<<" knight moves."<<endl;
}
return ;
}

HDU1372:Knight Moves(经典BFS题)的更多相关文章

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

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

  2. HDU-1372 Knight Moves (BFS)

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

  3. 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 ( ...

  4. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...

  5. HDU1372:Knight Moves(BFS)

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

  6. hdu1372 Knight Moves BFS 搜索

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

  7. HDU 1372 Knight Moves (bfs)

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

  8. HDU 1372 Knight Moves【BFS】

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

  9. uva439 - Knight Moves(BFS求最短路)

    题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...

随机推荐

  1. 使用开源软件sentry来收集日志

    原文地址:http://luxuryzh.iteye.com/blog/1980364 对于一个已经上线的系统,存在未知的bug或者运行时发生异常是很常见的事情,随之而来的几点需求产生了: 1.系统发 ...

  2. expresscalculate

    //本程序接受任意含有+.-.*./和()的数值表达式,数值允许带小数,输入以#结束.如:((2.4+3.5)*2-(2+3.5)/2)*(2+3)# #include <stdio.h> ...

  3. CodeForces 220(div 2)

    悲剧的div2..... A 题意:在n * m的矩形平面直角坐标系中,从(x, y)可以到四个点(x - a, y - b),(x + a, y - b),(x - a, y + b),(x + a ...

  4. 常用Application Server

    综述,我们这里列出的application server应用服务器,主要是对servlet和JSP提供了良好的支持; [resin] [Tomcat] [jboss] community.jboss. ...

  5. python--把一个方法变成属性调用

    # coding=utf-8 ''' 装饰器(decorator)可以给函数动态加上功能,对于类的方法,装饰器一样起作用.Python内置的@property装饰器就是负责把一个方法变成属性调用的: ...

  6. Apache Kylin

    日前,eBay公司隆重宣布已经正式向开源业界推出分布式分析引擎:Kylin(http://kylin.io).作为一套旨在对Hadoop环境下分析流程进行加速.且能够与SQL兼容性工具顺利协作的解决方 ...

  7. Android性能测试工具 Emmagee

    Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资源的性能测试小工具. 支持SDK:Android2.2以及以上版本 Emmagee功能介绍 1.检测当前时间被测应用占用的CP ...

  8. ubuntu firefox 选中变成了删除

    在ubuntu下 我的firefox浏览器出现了问题.描述: 1.在firefox中所有能够删除的文字只要选中就自动删除了. 终端中 ibus-setup勾掉在应用窗口中启用内嵌编辑模式

  9. Android Dialog触摸对话框外部让其消失的实现方法

    方法一: @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent ...

  10. 打造支持apk下载和html5缓存的 IIS(配合一个超简单的android APP使用)具体解释

    为什么要做这个看起来不靠谱的东西呢? 由于刚学android开发,还不能非常好的熟练控制android界面的编辑和操作,所以我的一个急着要的运用就改为html5版本号了,反正这个运用也是须要从serv ...