0

所以,简而言之,我正在研究骑士的巡回演出。如果你不知道那是什么,马就会被放到棋盘上,你必须将它移动到棋盘上的每个位置恰好一次。我正在使用递归函数,但无法让我的回溯工作。我可以在 5x5 板上完成 22 步,但程序不会备份并尝试不同的路径。我只发布了我的代码的递归部分(抱歉它有点长)任何见解都会非常有帮助。非常感谢!

`bool findPath ( int board[][boardSize + 4], int &currRow, int &currCol, int &currMove,int boardSize )
{
    int i, j;
    bool foundSpot;

    board[currRow][currCol] = currMove;

    if ( currMove == boardSize * boardSize )
        return true;

    for ( i = 0; i < boardSize + 4; i++ )
    {
        for ( j = 0; j < boardSize + 4; j++ )
            cout << setw (3) << board[i][j];
        cout<<endl;
    }
    cout << endl;

    if ( board[currRow - 2][currCol - 1] == 0 )
    {
        currMove += 1;
        board[currRow - 2][currCol - 1] = currMove;
        currRow -= 2;
        currCol -= 1;
        if ( findPath( board, currRow, currCol, currMove, boardSize ) )
            return true;
    }

    if ( board[currRow - 2][currCol + 1] == 0 )
    {
        currMove += 1;
        board[currRow - 2][currCol + 1] = currMove ;
        currRow -= 2;
        currCol += 1;
        if ( findPath( board, currRow, currCol, currMove, boardSize ) )
            return true;
    }

    if ( board[currRow - 1][currCol + 2] == 0 )
    {
        currMove += 1;
        board[currRow - 1][currCol + 2] = currMove ;
        currRow -= 1;
        currCol += 2;
        if ( findPath( board, currRow, currCol, currMove, boardSize ) )
            return true;
    }

    if ( board[currRow + 1][currCol + 2] == 0 )
    {       
        currMove += 1;
        board[currRow + 1][currCol + 2] = currMove ;
        currRow += 1;
        currCol += 2;
        if ( findPath( board, currRow, currCol, currMove, boardSize ) )
            return true;
    }

    if ( board[currRow + 2][currCol + 1] == 0 )
    {
        currMove += 1;
        board[currRow + 2][currCol + 1] = currMove ;
        currRow += 2;
        currCol += 1;
        if ( findPath( board, currRow, currCol, currMove, boardSize ) )
            return true;
    }

    if ( board[currRow + 2][currCol - 1] == 0 )
    {
        currMove += 1;
        board[currRow + 2][currCol - 1] = currMove ;
        currRow += 2;
        currCol -= 1;
        if ( findPath( board, currRow, currCol, currMove, boardSize ) )
            return true;
    }

    if ( board[currRow + 1][currCol - 2] == 0 )
    {       
        currMove += 1;
        board[currRow + 1][currCol - 2] = currMove ;
        currRow += 1;
        currCol -= 2;
        if ( findPath( board, currRow, currCol, currMove, boardSize ) )
            return true;
    }

    if ( board[currRow - 1][currCol - 2] == 0 )
    {       
        currMove += 1;
        board[currRow - 1][currCol - 2] = currMove ;        
        currRow -= 1;
        currCol -= 2;
        if ( findPath( board, currRow, currCol, currMove, boardSize ) )
            return true;
    }
    board[currRow][currCol] = 0;
    currMove -= 2;
    return false;
}`
4

1 回答 1

0

我在 c++ 中制定了以下骑士之旅的实现:

#include<cstdio>
#include<iostream>
#define MAX 10

using namespace std;
int tour=1;
int board[MAX][MAX];

bool is_travelled(int,int);
bool knights_tour(int,int,int,int);
void initialize(int);
void display(int);

int main(int argc,char** argv){
    int n;
    scanf("%d",&n);
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            board[i][j]=-1;
        }
    }
    initialize(n);
    display(n);
    return 0;
}

bool is_travelled(int x,int y){
    if(x<0 || y<0)return true;
    if(board[x][y]==-1)return false;
    return true;
}

bool knights_tour(int i,int j,int n,int k){ // k=number of places remained , n=side of chess_board;
    int x,y;
    if(k==0)return true;
    // hard-coded cases;
    // reordering of the cases have significant effect on the execution time
    x=i+2;y=j+1;
    if((!is_travelled(x,y))&&x<n&&y<n){
        board[x][y]=tour;
        tour+=1;
        if(knights_tour(x,y,n,k-1))return true;
        board[x][y]=-1;
        tour-=1;
    }
    x=i+1;y=j+2;
    if((!is_travelled(x,y))&&x<n&&y<n){
        board[x][y]=tour;
        tour+=1;
        if(knights_tour(x,y,n,k-1))return true;
        board[x][y]=-1;
        tour-=1;
    }
    x=i-1;y=j+2;
    if((!is_travelled(x,y))&&x<n&&y<n){
        board[x][y]=tour;
        tour+=1;
        if(knights_tour(x,y,n,k-1))return true;
        board[x][y]=-1;
        tour-=1;
    }
    x=i-2;y=j+1;
    if((!is_travelled(x,y))&&x<n&&y<n){
        board[x][y]=tour;
        tour+=1;
        if(knights_tour(x,y,n,k-1))return true;
        board[x][y]=-1;
        tour-=1;
    }
    x=i-2;y=j-1;
    if((!is_travelled(x,y))&&x<n&&y<n){
        board[x][y]=tour;
        tour+=1;
        if(knights_tour(x,y,n,k-1))return true;
        board[x][y]=-1;
        tour-=1;
    }
    x=i-1;y=j-2;
    if((!is_travelled(x,y))&&x<n&&y<n){
        board[x][y]=tour;
        tour+=1;
        if(knights_tour(x,y,n,k-1))return true;
        board[x][y]=-1;
        tour-=1;
    }
    x=i+1;y=j-2;
    if((!is_travelled(x,y))&&x+y<n&&x<n&&y<n){
        board[x][y]=tour;
        tour+=1;
        if(knights_tour(x,y,n,k-1))return true;
        board[x][y]=-1;
        tour-=1;
    }
    x=i+2;y=j-1;
    if((!is_travelled(x,y))&&x<n&&y<n){
        board[x][y]=tour;
        tour+=1;
        if(knights_tour(x,y,n,k-1))return true;
        board[x][y]=-1;
        tour-=1;
    }
    return false;
}
void initialize(int n){
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            board[i][j]=0;
            int r=knights_tour(i,j,n,n*n-1);
            if(r==1)return;
            board[i][j]=-1;
        }
    }
}

void display(int n){
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            printf("%2d ",board[i][j]);
        }
        printf("\n");
    }
    cout<<endl;
}

希望这可以帮助 。快乐编码!

于 2017-02-12T20:24:24.390 回答