Knight Moves

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

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.".
 
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 <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int dx[]={,-,,-,,-,,-};
int dy[]={,-,-,,,-,-,};
int vis[][];
struct point
{
int x;
int y;
int t;
}st,tem,nex;
int sx,sy,ex,ey,tim;
char a,b,c,d;
void bfs()
{
queue<point> s;
st.x=sx,st.y=sy;
st.t=;
s.push(st);
memset(vis,,sizeof(vis));
while(!s.empty())
{
tem=s.front();
s.pop(); if(tem.x==ex&&tem.y==ey)
{
tim=tem.t;
return;
}
if(vis[tem.x][tem.y]||tem.x<=||tem.y<=||tem.x>||tem.y>)
continue;
vis[tem.x][tem.y]=;
for(int i=;i<;i++)
{
int nx=tem.x+dx[i];
int ny=tem.y+dy[i];
int nt=tem.t+;
nex.x=nx,nex.y=ny,nex.t=nt;
s.push(nex);
}
}
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%c%c %c%c",&a,&b,&c,&d)!=EOF)
{
getchar();
tim=;
sy=a-'a'+,sx=b-'';
ey=c-'a'+,ex=d-'';
bfs();
printf("To get from %c%c to %c%c takes %d knight moves.\n",a,b,c,d,tim);
}
}
 

Knight Moves(BFS,走’日‘字)的更多相关文章

  1. hdu1372 Knight Moves BFS 搜索

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

  2. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  3. hdu5794 A Simple Chess 容斥+Lucas 从(1,1)开始出发,每一步从(x1,y1)到达(x2,y2)满足(x2−x1)^2+(y2−y1)^2=5, x2>x1,y2>y1; 其实就是走日字。而且是往(n,m)方向走的日字。还有r个障碍物,障碍物不可以到达。求(1,1)到(n,m)的路径条数。

    A Simple Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

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

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

  5. UVA 439 Knight Moves(BFS)

    Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...

  6. HDU 1372 Knight Moves(BFS)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  7. POJ 1915 Knight Moves(BFS+STL)

     Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 ...

  8. HDU1372:Knight Moves(BFS)

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

  9. POJ-1915 Knight Moves (BFS)

    Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26952   Accepted: 12721 De ...

随机推荐

  1. 排队论的C实现

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下鄙人实现了排队论思想,语言是C语言   #include<stdio.h> #in ...

  2. 开心菜鸟学习系列笔记-----Javascript(1)

    js 一些常见的使用方法        // target : 不管是否出现冒泡,他都是代表最开始引发事件的对象   // this   : 是指当前函数.  //ie 事件对象   : window ...

  3. .NET抽象工厂模式微理解--教你在项目中实现抽象工厂

    .NET抽象工厂模式微理解--教你在项目中实现抽象工厂 最近在学习MVC,对于MVC里面的一些项目上的东西都和抽象模式有关,今天就微说明一下个人对于抽象工厂模式的理解,以方便学习MVC及工厂模式相关的 ...

  4. Canvas Path 绘制柱体

    public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceS ...

  5. mybatis批量update(mysql)

    Mapper文件中的写法 <insert id="batchUpdateTjData"> <foreach collection="list" ...

  6. GitHub使用说明

    登陆https://github.com/,并注册账号 从如下地址下载windows客户端:https://msysgit.googlecode.com/files/Git-1.8.4-preview ...

  7. UVa232.Crossword Answers

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. MediaInfo源代码分析 1:整体结构

    MediaInfo 用来分析视频和音频文件的编码和内容信息,是一款是自由软件 (免费使用.免费获得源代码).之前编程的时候,都是直接调用它提供的Dll,这次突然来了兴趣,想研究一下它内部究竟是怎么实现 ...

  9. 【转载】C代码优化方案

    C代码优化方案 1.选择合适的算法和数据结构2.使用尽量小的数据类型3.减少运算的强度 (1)查表(游戏程序员必修课) (2)求余运算 (3)平方运算 (4)用移位实现乘除法运算 (5)避免不必要的整 ...

  10. WPF可视化控件打印

    Introduction While coding an application that displays a detailed report in a ScrollViewer, it was d ...