HDU 1372 Knight Moves

Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10362 Accepted Submission(s): 6097

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.

GNU C++略坑 G++一次过 GNU C++硬是格式错误..最后一组test完了居然不能输出\n

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
//#define CHECK_TIME

struct P{
int x,y;
};

int mp[9][9];
int di[8][2]={{2,-1},{2,1},
{-2,-1},{-2,1},
{-1,-2},{1,-2},
{-1,2},{1,2}};

int bfs(P st,P end){
for(int i=1;i<9;++i)
fill(mp[i],mp[i]+9,INF);
mp[st.x][st.y]=0;
deque<P>de;
de.push_back(st);
while(mp[end.x][end.y]>=INF){
P f=de.front();
de.pop_front();
for(int i=0;i<8;++i){
P to={f.x+di[i][0],f.y+di[i][1]};
if(to.x>0&&to.x<9&&to.y>0&&to.y<9)
if(mp[to.x][to.y]==INF){
mp[to.x][to.y]=mp[f.x][f.y]+1;
de.push_back(to);
}
}
}
return mp[end.x][end.y];
}

int main()
{
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);

char sq[2][3];
int t=0;
while(~scanf("%s",sq[0])){
if(t!=0)
putchar('\n');
++t;
P st={sq[0][0]-'a'+1,sq[0][1]-'0'};
scanf("%s",sq[1]);
P end={sq[1][0]-'a'+1,sq[1][1]-'0'};
printf("To get from %2s to %2s takes %d knight moves.",sq[0],sq[1],bfs(st,end));
}

return 0;
}


• 本文已收录于以下专栏：

HDU1372：Knight Moves(BFS)

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

HDU 1372 - Knight Moves

（感觉自己越来越懒了，题目都不用贴了……） 谨以此纪念一下自己学会的第一道BFS~ #include #include #include using namespace std; struct typ...

HDU 1372 Knight Moves

Knight Moves Problem Description A friend of you is doing research on the Traveling Knight P...

hdu 1372 Knight Moves(广搜)

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

HDU1372 Knight Moves（BFS）

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

poj2243 && hdu1372 Knight Moves（BFS）

poj2243&&hdu1372 Knight Moves（BFS）

【hdu 1372】 Knight Moves （BFS）

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

POJ 2243 || HDU 1372:Knight Moves(BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11223...

【启发式搜索】[HDU 1372]Knight Moves

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)