使用c ++进行递归回溯骑士之旅

时间:2013-04-09 00:11:14

标签: recursion backtracking

所以,简而言之,我正在制作一个骑士旅行计划。如果你不知道那是什么,那么骑士会被放在国际象棋棋盘上,你必须将它移动到棋盘上的每个位置。我正在使用递归函数,但我无法让我的回溯工作。我可以在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;
}`

1 个答案:

答案 0 :(得分: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;
}

希望这会有所帮助。 快乐的编码!