【第22期】观点:IT 行业加班,到底有没有价值?

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

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

HDU-1372-Knight Moves

HDU-1372-Knight Moves http://acm.hdu.edu.cn/showproblem.php?pid=1372 求“马”从一点到另一点的最短距离,马走日,BFS即可</

hdu 1372 Knight Moves (简单bfs,囧!)

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

HDU 1372 knight moves

Problem Description <div class="panel_c

HDU-#1372 Knight Moves(双向BFS)

题目大意:在一个8*8的qipuanzhong
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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