HDU 1372 Knight Moves

原创 2016年08月31日 13:19:58

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.


水题 bfs跑一遍就行了
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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 1372 Knight Moves(BFS)

HDU1372:Knight Moves(BFS) Problem Description A friend of you is doing research on the Tra...

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...
  • Freenm
  • Freenm
  • 2017年03月20日 20:39
  • 87

HDU1372 Knight Moves(广搜BFS+深搜DFS)

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

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...
  • zfz1015
  • zfz1015
  • 2012年08月13日 20:04
  • 352

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

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

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

其实直接用BFS好像也可以AC但是用了A*可以更快,所以为了练习A*还是用A*老老实实写吧。估价函数就是到终点的曼哈顿距离除以3因为每走一步曼哈顿距离最多减少3,而且在所有情况下h(n)...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 1372 Knight Moves
举报原因:
原因补充:

(最多只允许输入30个字)