C编程:递归函数输出的现场控制台显示骑士之旅

问题描述

我被困在我的第一个更复杂的 C 编码任务中:开发一个程序来解决骑士的巡回赛(国际象棋,骑士只需要访问棋盘上的每个领域一次)并且它还应该有一个实时显示显示程序正在实时执行哪些操作。
我提出的解决方不支持实时显示,因为它通过 for 循环进行迭代,直到达到最大移动次数,然后打印移动。如何更改此代码以创建实时输出
displayBoard 函数仅在控制台输出中打印棋盘,其中包含骑士的当前位置以及字段名称(例如 A-1)。计划是在我最初的问题解决后,将其更改为控制台的流输出

(结合这个问题:函数调用 if 条件如何工作?我在网上偶然发现了这个,但遗憾的是没有解释。)

int knight(int line,int row,int moveNumber)
{
    int newLine,newRow,i;
    //char cells[]={'A','B','C','D','E','F','G','H'};

    //mark visited cell
    board[line][row] = moveNumber;
    
    //for-loop for every possible move
    for(i = 0; i < 8; i++)
    {        
        //new position
        newLine = line + moves[i][0];
        newRow = row + moves[i][1];
        
        //is the move on the board?
        if(newLine >= 1 && newLine <= N && newRow >= 1 && newRow <= N)
        {
        
            //has the cell been visited yet?
            if(!board[newLine][newRow])
            {

                //recursion until 63th move
                if(moveNumber == max_moves || knight(newLine,moveNumber+1))
                {
                    printf("Move %d: from %c-%d to %c-%d ",moveNumber,cells[row-1],line,cells[newRow-1],newLine);
                    boarddisplay(newLine,newRow);
                    return 1;
                }
            } 
        }
    }
    
    // free last visited cell,if there's no move possible anymore
    board[line][row] = 0;
    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)