首页 > 解决方案 > 试图解决嵌套向量上的骑士之旅> 但它不起作用

问题描述

我为骑士巡回问题编写了一个代码,该代码适用于 2D 数组但不适用于 vector<vector<pair<int,int>>

工作代码

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

bool isPossible(int sol[N][N], int x, int y)
{
    if ( sol[x][y]==-1 && x >= 0 && x < N && y >= 0 && y < N)
    {
        return true;
    }
    return false;
}
bool KNT(int sol[N][N], int moveNUM, int x, int y, int movex[8], int movey[8])
{
    int i,next_x,next_y;
    if (moveNUM == N * N)
    {
        return true;
    }
    for ( i = 0; i < 8; i++)
    {
         next_x = x + movex[i];
         next_y = y + movey[i];
        if (isPossible(sol, next_x, next_y))
        {
            sol[next_x][next_y]=moveNUM;
            
            if (KNT(sol, moveNUM + 1, next_x, next_y, movex, movey))
            {
                return true;
            }
        
           sol[next_x][next_y] = -1; //Backtracking
            
        }
    }
    return false; 
}
int main()
{
   
    
    int sol[N][N];
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            sol[i][j]=-1;
        }
        
    }

    int movex[8] = {2, 1, -1, -2, -2, -1, 1, 2};
    int movey[8] = {1, 2, 2, 1, -1, -2, -2, -1};
    KNT(sol,1,0,0,movex, movey);
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            cout << sol[i][j] << " ";
        }
        cout << endl;
    }
}

非工作代码

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

bool isPossible(vector<vector<pair<int, int>>> &Brd, int x, int y)
{
    if ((Brd[x][y].first == -1) && x >= 0 && x < N && y >= 0 && y < N)
    {
        return true;
    }
    return false;
}
bool KNT(vector<vector<pair<int, int>>> &Brd, int moveNUM, int x, int y, int movex[8], int movey[8])
{
    int i,next_x,next_y;
    if (moveNUM == N * N)
    {
        return true;
    }
    for ( i = 0; i < 8; i++)
    {
         next_x = x + movex[i];
         next_y = y + movey[i];
        if (isPossible(Brd, next_x, next_y))
        {
             Brd[next_x][next_y].first = 1;
             Brd[next_x][next_y].second = moveNUM;
           
            if (KNT(Brd, moveNUM + 1, next_x, next_y, movex, movey))
            {
                return true;
            }
            Brd[next_x][next_y].first = -1;
            Brd[next_x][next_y].second = 0;
             //Backtracking
            
        }
    }
    return false; //Check for error
}
int main()
{
    vector<vector<pair<int, int>>> Brd;
    for (int i = 0; i < N; i++)
    {
        vector<pair<int, int>> temp;
        for (int j = 0; j < N; j++)
        {
            temp.push_back(make_pair(-1, 0));
        }
        Brd.push_back(temp);
    }
    
     int movex[8] = {2, 1, -1, -2, -2, -1, 1, 2};
    int movey[8] = {1, 2, 2, 1, -1, -2, -2, -1}; 
   
    KNT(Brd,1,0,0,movex,movey);
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            cout << Brd[i][j].second << " ";
        }
        cout << endl;
    }
}

在非工作代码中,当我运行代码时,它不会给出任何输出,而是突然结束。

PS 任何帮助都意味着很多,我已经浪费了大约 2 天的时间来寻找解决方案。

标签: c++c++11vectornestedknights-tour

解决方案


这两个程序都有未定义的行为,因为您访问 2D 数组/向量超出范围。

您首先检查是否,sol[x][y] == -1然后检查是否在界限内:xy

bool isPossible(int sol[N][N], int x, int y)
{
    if (sol[x][y]==-1 && x >= 0 && x < N && y >= 0 && y < N)

您需要先检查边界。

您的第一个解决方案应该这样做:

    if (x >= 0 && x < N && y >= 0 && y < N && sol[x][y]==-1)

您的第二个解决方案应该这样做:

    if(x >= 0 && x < N && y >= 0 && y < N && Brd[x][y].first == -1)

注意:这两个程序产生不同的解决方案。你必须决定哪一个是正确的(如果有的话)。

第一的:

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

第二:

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

推荐阅读