【问题标题】:Traverse Knight in ChessBoard C COdeChessBoard C 代码中的遍历骑士
【发布时间】:2012-01-24 14:27:46
【问题描述】:

我已经编写了一个代码,让一个骑士只遍历棋盘上的所有方格一次。这个(下面)代码的问题是,它工作到 7x7 并且在 8x8 之后什么都不做。代码是 这里 chessBoardSize 定义了大小(8=> 8x8)

#include<stdio.h>
#include<stdlib.h>
#define chessBoardSize 12

int chessBoard[chessBoardSize][chessBoardSize] = {0};
typedef struct point{
    int x, y;
}POINT;
int count=0;

int nextPosition(int x, int y, POINT* array){
    int m=0;
    /* finds the next possible points for the current
    position in the chess board:
    like
    _   _   _   _   _   _
    _   *   _   *   _   _
    *   _   _   _   *   _
    _   _   P   _   _   _
    *   _   _   _   *   _
    _   *   _   *   _   _  

as above if 'P' is the current (x,y)
* represents the next possible points and 
also checks it exists within the chess board
    */

    if( (x+2) < chessBoardSize ){
        if( (y+1) < chessBoardSize ){
            array[m].x = x+2;
            array[m++].y = y+1;
        }
        if( (y-1) >-1 ){
            array[m].x = x+2;
             array[m++].y = y-1;
        }
    }

    if( (x-2) > -1){
        if( (y+1) < chessBoardSize ){
            array[m].x = x-2;
            array[m++].y = y+1;
        }
        if( (y-1) >-1 ){
            array[m].x = x-2;
            array[m++].y = y-1;
        }
    }

    if( (y+2) < chessBoardSize){
        if( (x+1) < chessBoardSize ){
            array[m].x = x+1; 
            array[m++].y = y+2;
        }
        if( (x-1) >-1 ){
            array[m].x = x-1;
            array[m++].y = y+2;     
        }
    }   

    if( (y-2) > -1){
        if( (x+1) < chessBoardSize ){
            array[m].x = x+1;
            array[m++].y = y-2;
        }
        if( (x-1) >-1 ){
            array[m].x = x-1;
            array[m++].y = y-2;     
        }
    }
    return m;
}

void displayAnswer(){
    int i, j, k;
    printf("\n");
    for(i=0; i<chessBoardSize; i++){
        for(j=0; j<chessBoardSize; j++)
            printf("%d\t",chessBoard[i][j]);
            printf("\n\n");
    }
}

//  recursive function using backtrack method
void knightTravel(int x, int y){
    POINT array[8] = {{0, 0}, {0, 0}};
    // remainin initialized to zero automatically
    volatile int noOfPossiblePoints = nextPosition(x, y, array);
    volatile int i;

    chessBoard[x][y] = ++count;

    // base condition uses count 
    if( count == chessBoardSize * chessBoardSize ){
        displayAnswer();
        exit(0);
    }

    for(i=0; i< noOfPossiblePoints; i++)
        if( chessBoard[array[i].x][array[i].y] == 0 )
            knightTravel(array[i].x, array[i].y); 

    chessBoard[x][y] = 0;
    count--;
}

int main()
{
    knightTravel(0, 0);
    printf("No solution exists\n");
    return 0;
}

【问题讨论】:

  • 风格注释:通过堆栈下方某处的 exit() 终止正常的“成功”案例可能会令人困惑;考虑重写,以便在正常情况下允许 main() 返回。
  • 是的,我想到了。但是在函数“displayAnswer()”之后如果不使用长跳转就无法进入 main。所以,如果你有想法,请告诉我..???
  • 是的,不幸的是,C 没有提供更方便的方法来做到这一点。在这种情况下,您可以给 knightTravel() 一个指示成功或失败的返回值,并在成功时退出 for 循环。

标签: c algorithm chess backtracking


【解决方案1】:

问题是您使用的方法无法在任何合理的时间内解决 8x8 或更高的问题。您的代码很好,但有 4e51 种可能的移动,因此您的程序将花费大量时间来找到一个游览。

在您的程序中,迭代次数如下:

5x5 = 74,301

6x6 = 2,511,583

7x7 = 136,328

对于 8x8,您的程序需要执行以下操作:

3,926,356,053,343,005,839,641,342,729,308,535,057,127,083,875,101,072 次迭代。

【讨论】:

  • 哦,谢谢。你是怎么想出这个(确切的)数字和任何更好的方法的建议???
  • 这个数字是一个骑士可以在 8x8 棋盘上移动的可能数量。这就是为什么我说它“达到”这个数字的原因,您的程序很可能会在更少的迭代中找到解决方案。
  • - 因为您没有找到所有的路线,而只是找到了第一个路线,即 64 步。您有两种选择来加快速度,或者您需要大量核心,然后将每个递归分成一个离散的工作以在另一个核心上完成 - 这种方法甚至可能需要大量联网的机器......或者使用非暴力方法,看到这个link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 1970-01-01
相关资源
最近更新 更多