<OJ_Sicily>Knight Moves

40 篇文章 0 订阅

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

There are multiple test cases. The first line contains an integer T, indicating the number of 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.".  

题目解释:对于一个8x8的棋盘上,输入起点和终点,求解起点走到终点所需要的步数。这里走的规则是中国象棋里面走的“日”,而且不能够重复

解题思路:使用广度搜索求解

#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
bool chessboard[8][8];
int moveStep[8][2] = {{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2}};
struct points{
    int x,y;
};
bool canMove(int x, int y){
    if (x >= 0 && x < 8 && y >= 0 && y < 8 && chessboard[x][y]) {
        return true;
    }
    return false;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    int caseNum;
    cin >> caseNum;
    while (caseNum--) {
        memset(chessboard, true, sizeof(chessboard));
        string inStr1, inStr2;
        cin >> inStr1 >> inStr2;
        points start, target;
        start.x = inStr1[1] - '1';   // 将输入起点和终点转换为矩阵的点
        start.y = inStr1[0] - 'a';
        target.x = inStr2[1] - '1';
        target.y = inStr2[0] -'a';
        chessboard[start.x][start.y] = false;
        vector<points> v[64];
        v[0].push_back(start);
        int k = 0;
        bool flag = false, notSame = true;
        if (start.x == target.x && start.y == target.y) {  // 起点跟终点一致
            notSame = false;
            flag = true;
        }
        while (notSame) {    // 进行广度搜索
            if (v[k].size() == 0) break;
            vector<points>:: iterator it;
            for (it = v[k].begin(); it != v[k].end(); it++) {
                points tmp = *it;
                points newPoint;
                for (int i = 0; i < 8; i++) {
                    newPoint.x = tmp.x + moveStep[i][0];
                    newPoint.y = tmp.y + moveStep[i][1];
                    if (newPoint.x == target.x && newPoint.y == target.y) {
                        flag = true;
                        break;
                    }
                    if (canMove(newPoint.x, newPoint.y)) {
                        v[k+1].push_back(newPoint);
                        chessboard[newPoint.x][newPoint.y] = false;
                    }
                }
            }
            k++;
            if (flag) break;
          
        }
        if (flag){
            cout << "To get from " << inStr1[0] << inStr1[1] << " to " << inStr2[0] << inStr2[1] << " takes " << k << " knight moves." << endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值