骑士巡回问题的Warnsdorff算法

Description:

描述:

This is a standard problem to implement Warnsdorff's algorithm for knight's tour problem using backtracking.

这是使用回溯来实现Warnsdorff算法解决骑士旅行问题的标准问题。

Problem statement:

问题陈述:

Given a chess board and a knight is placed to any of the position of the chess. You have to find out either the knight will go through all the positions of the chess and if it is possible then print the total chess or not possible.

给定一个棋盘和一个骑士被放置在任何位置的棋。 您必须找出骑士会走过国际象棋的所有位置,如果可能的话,请打印整个国际象棋,或者不可能。

    Test cases T
    T no. of position will be given.

    E.g.
    3
    0 0
    0 4
    1 7

    Output:
    If it is possible for the knight to travel whole chess then 
    print the total matrix otherwise print "not possible to cover".

Example

    T=3

    Input:
    X= 0, Y=0
    
    Output:
    1 38 59 36 43 48 57 52
    60 35 2 49 58 51 44 47
    39 32 37 42 3 46 53 56
    34 61 40 27 50 55 4 45
    31 10 33 62 41 26 23 54
    18 63 28 11 24 21 14 5
    9 30 19 16 7 12 25 22
    64 17 8 29 20 15 6 13
    
    Input:
    X= 0, Y= 4 
    
    Output:
    45 52 59 36 1 50 57 38
    60 35 44 51 58 37 2 49
    25 46 53 28 43 48 39 56
    34 61 26 47 54 29 42 3
    9 24 33 62 27 40 55 30
    18 63 10 21 32 13 4 41
    23 8 19 16 11 6 31 14
    64 17 22 7 20 15 12 5
    
    Input:
    X= 1, Y= 7

    Output:
    Not possible to cover.

Explanation with example:

举例说明:

Choosing the right part to cover the whole chess is a problem of combination and we will solve it through the backtracking process.

选择合适的部分覆盖整个国际象棋是一个组合问题,我们将通过回溯过程来解决。

In chess, for a knight, there are several positions to mov­­e from a position.

在国际象棋中,对于一个骑士,有几个位置可以从一个位置移动。

Warnsdorff's algorithm for Knight's tour problem

Every time it will follow a route. When the knight will be struck then they will back and choose another path to go the all position of the chess.

每次它将遵循一条路线。 当骑士被击打时,他们将退后并选择另一条路走棋的所有位置。

For the position (0,0).

对于位置(0,0)。

There are 8 points (1,2) (2,1) (2,-1) (1, -2) (-1,-2) (-2,-1) (-2,1) and (-1,2). In 8 points (2,-1) (1, -2) (-1,-2) (-2,-1) (-2,1) (-1,2) points are invalid. So we have to go either (1,2) or (2,1). We will choose any of them and then from that point will see the next 8 points and consider only the valid moves. If at any position no possible move will not possible then back to the previous move and from that go to the other path and stop if the step cont will be 64.

有8分(1,2,(2,1)(2,-1)(1,-2)(-1,-2)(-2,-1)(-2,1)和(-1 ,2)。 在8分(2,-1)(1,-2)(-1,-2)(-2,-1)(-2,1)(-1,2)中无效。 所以我们必须走(1,2)或(2,1)。 我们将选择其中任何一个,然后从那一点将看到接下来的8点,并仅考虑有效的移动。 如果在任何位置都不可能进行任何移动,则返回到先前的移动,然后从该路径转到另一条路径,如果步长为64,则停止。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

int x[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int y[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };

bool is_safe(int a, int b)
{
    return (a >= 0 && a < 8 && b >= 0 && b < 8);
}

bool traverse(int pos_x, int pos_y, int count, int* arr, bool* visited)
{
    *(arr + pos_x * 8 + pos_y) = count;
    *(visited + pos_x * 8 + pos_y) = true;
    if (count == 64)
        return true;
    if (count > 64)
        return false;
    for (int i = 0; i < 8; i++) {
        int x_axis = pos_x + x[i];
        int y_axis = pos_y + y[i];
        if (is_safe(x_axis, y_axis) && *(visited + x_axis * 8 + y_axis) == false && traverse(x_axis, y_axis, count + 1, arr, visited)) {
            return true;
        }
    }
    *(arr + pos_x * 8 + pos_y) = 0;
    *(visited + pos_x * 8 + pos_y) = false;
    return false;
}

int main()
{
    int t;

    cout << "Test Case : ";
    cin >> t;

    while (t--) {
        int pos_x, pos_y;
        cout << "Enter the position : ";
        cin >> pos_x;
        cin >> pos_y;
        int count = 1;
        int arr[8][8] = { 0 };
        bool visited[8][8] = { false };
        //arr[pos_x][pos_y]=1;
        //visited[pos_x][pos_y]=true;
        if (traverse(pos_x, pos_y, count, &arr[0][0], &visited[0][0])) {
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    cout << arr[i][j] << " ";
                }
                cout << endl;
            }
        }
        else {
            cout << "not possible to cover" << endl;
        }
    }
    return 0;
}

Output

输出量

Test Case : 3

Enter the position : 0 0
1 38 59 36 43 48 57 52
60 35 2 49 58 51 44 47
39 32 37 42 3 46 53 56
34 61 40 27 50 55 4 45
31 10 33 62 41 26 23 54
18 63 28 11 24 21 14 5
9 30 19 16 7 12 25 22
64 17 8 29 20 15 6 13

Enter the position : 0 4
45 52 59 36 1 50 57 38
60 35 44 51 58 37 2 49
25 46 53 28 43 48 39 56
34 61 26 47 54 29 42 3
9 24 33 62 27 40 55 30
18 63 10 21 32 13 4 41
23 8 19 16 11 6 31 14
64 17 22 7 20 15 12 5

Enter the position : 1 7
not possible to cover


翻译自: https://www.includehelp.com/icp/warnsdorffs-algorithm-for-knights-tour-problem.aspx

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值