HDU——1372 Knight Move(dfs)

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.

Problem solving ideas:

The main idea of ​​the title: 8*8 matrix, the columns are represented by (a~h), and the rows are represented by (1~8), calculate the minimum number of cells that need to be skipped from one cell to another.

Note: This question requires an understanding of the movement law of chess, which is to move on the diagonal of the 3 * 2 grid. In this way, there are 8 directions that each piece can choose from.

Idea: classic dfs

Code:

#include <cstdio>
#include <cstring>
using namespace std;


//八个方向
int dir[8][2] = {-2 , 1 , -2 , -1 , 2 , -1 , 2 , 1 , 1 , 2 , -1 , 2 , 1 , -2 , -1 , -2 };

int map[10][10];
char s1[3] , s2[3];
int x2 , y2 , ans;

void dfs(int x , int y , int ans){
    map[x][y] = ans;
    for(int i = 0 ; i < 8 ; i ++){
        for(int j = 0 ; j < 8 ; j ++){
            printf("%d ",map[i][j]);
        }
        printf("\n");
    }
    if(x == x2 && y == y2)
        return ;

    for(int i = 0 ; i < 8 ; i ++){
        int tempx = x + dir[i][0];
        int tempy = y + dir[i][1];
        if(tempx < 0 || tempy < 0 || tempx >= 8 || tempy >= 8  || map[tempx][tempy] <= ans + 1)   //这边最后一个条件保证了一定不会再去走一条比当前路径大的路。
            continue;
        dfs(tempx , tempy , ans + 1);
    }
}
int main(){

    //freopen("D://testData//1372.txt" , "r" , stdin);
    while(scanf("%s%s",s1,s2) != EOF){
        memset(map , 0x3f , sizeof(map));
        ans = 0;
        int x1 = s1[0] - 'a';
        int y1 = s1[1] - '1';

        x2 = s2[0] - 'a';
        y2 = s2[1] - '1';

        dfs(x1 , y1 ,ans);

        printf("To get from %s to %s takes %d knight moves.\n",s1 , s2 , map[x2][y2]);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325858191&siteId=291194637