hduoj1372 Knight Moves 暴搜bfs

Knight Moves

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


 

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.

 

Source

University of Ulm Local Contest 1996

思路:

这道题其实挺简单,就是可能读题有点问题,题意大致就是象棋游戏里的模拟马的走法“日”字形,只要把边界算一下,题目中也已经给出共有a-h行也就是从0到第7行,从0到第7列,输入一个是起点,一个是终点,字母代表行,数字代表列

OK,读懂题之后就很简单了,直接暴搜

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#define ll long long
using namespace std;
bool vis[10][10];
char line[5],co[5];
int l = 8,c = 8;//边界
struct node
{
    int x,y,step;
    friend bool operator < (node a,node b)
    {
        return a.step > b.step;
    }
};
int dx[8][2] = {1,2,-1,2,2,1,-2,1,2,-1,-2,-1,1,-2,-1,-2};//一共八种走法
void bfs(int x1,int y1,int x2,int y2)
{
    memset(vis,0,sizeof(vis));
    int ans = 0;
    priority_queue<node> que;
    node p,p2;
    p.x = x1,p.y = y1,p.step = 0;
    vis[x1][y1] = 1;
    que.push(p);
    while (!que.empty())
    {
        p = que.top();
        que.pop();
        p2 = p;
        if (p.x == x2 && p.y == y2)
        {
            ans = p.step;
            break;    
        }
        for (int i = 0;i < 8;i ++)
        {
            p2.x = p.x + dx[i][0],p2.y = p.y + dx[i][1];
            if (vis[p2.x][p2.y]) continue;
            if (p2.x < 0 || p2.x >= l || p2.y < 0 || p2.y >= c) continue;
            p2.step = p.step + 1;
            que.push(p2);
            vis[p2.x][p2.y] = 1;     
        }    
    }
    printf("To get from %s to %s takes %d knight moves.\n",line,co,ans);
}
int main()
{
    
    int bx,by,ex,ey;
    while (~scanf("%s %s",line,co))
    {
        bx = line[0] - 'a',by = line[1] - '1';
        ex = co[0] - 'a',ey = co[1] - '1';
        bfs(bx,by,ex,ey);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/cloudy_happy/article/details/81323310