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

Knight Moves

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

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 <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int mv[][] = {{-,-},{-,-},{-,},{-,},{,-},{,-},{,},{,}};
int v[][],map[][];
char a[],b[];
struct node
{
int x,y,ans;
}q[];
void bfs(int x,int y)
{
int e=;
int s=;
memset(v,,sizeof(v));
struct node t,f;
t.x=x;
t.y=y;
t.ans=;
v[t.x][t.y]=;
q[e++]=t;
while(s<e)
{
t=q[s++];
if(map[t.x][t.y]==)
{
printf("To get from %s to %s takes %d knight moves.\n",a,b,t.ans);
}
for(int i=;i<;i++)
{
f.x=t.x+mv[i][];
f.y=t.y+mv[i][];
f.ans=t.ans+;
if(f.x>=&&f.x<&&f.y>=&&f.y<&&v[f.x][f.y]==)
{
q[e++]=f;
v[f.x][f.y]=;
}
}
} }
int main()
{
while(scanf("%s%s",a,b)!=EOF)
{
memset(map,,sizeof(map));
map[b[]-''-][b[]-'a']=;//因为a-'0'从0开始,所以a[1]-'0'-1;
bfs(a[]-''-,a[]-'a'); }
return ;
}

Knight Moves(hdu1372 bfs模板题)的更多相关文章

  1. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

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

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

  3. HDU-1372 Knight Moves (BFS)

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

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

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

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

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

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

  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. Makefile 中all 和.PHONY的作用

    请编写一个makefile同时编译.链接下面两个程序: main1.c: #include<stdio.h> int main(void) { printf("main1\n&q ...

  2. 深入理解 Neutron -- OpenStack 网络实现(3):VXLAN 模式

    问题导读1.VXLAN 模式下,网络的架构跟 GRE 模式类似,他们的不同点在什么地方?2.网络节点的作用是什么?3.tap-xxx.qr-xxx是指什么? 接上篇:深入理解 Neutron -- O ...

  3. 使用SQL Server 2005作业设置定时任务【转】

    1.开启SQL Server Agent服务 使用作业需要SQL Agent服务的支持,并且需要设置为自动启动,否则你的作业不会被执行. 以下步骤开启服务:开始-->>>运行--&g ...

  4. Clojure学习之defmulti

    1. defmulti 宏defmulti 和defmethod 经常被用在一起来定义 multimethod. 宏defmulti 的参数包括一个方法名以及一个dispatch函数,这个dispat ...

  5. NC 的高级应用

    高级用法: (1)作攻击程序用,例子: 格式1:type.exe c:\exploit.txt|nc -nvv 192.168.x.x 80 格式2:nc -nvv 192.168.x.x 80 &l ...

  6. 开发常见错误之 : Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar

    SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackO ...

  7. 题目1042:Coincidence(最长公共子序列 dp题目)

    题目链接:http://ac.jobdu.com/problem.php?pid=1042 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  8. backBone.js之Model篇 (1) 简单实例

    “Model是js应用的核心,包括基础的数据以及围绕着这些数据的逻辑:数据转换.验证.属性计算和访问控制”. 一.初始化方法 我们先来看一个demo,initialize,这是一个初始化方法,但是写这 ...

  9. 从本机IIS中管理 远程服务器 IIS

    有时候,一般情况下,我们对服务器上 IIS 上的管理局限于 使用远程桌面:现在介绍一种,通过  本机 管理管理远程IIS 的方法! 1. 服务器端设置: 服务器管理器 ==>增加角色和功能向导= ...

  10. 170811、Java获取jdk系统环境变量

    package com.rick.utils; /******************************************************** *@Desc: 系统变量属性工具类 ...