现在,我正处于骑士之旅中,我只希望骑士能够移动,直到他无法移动,然后停下来为止。该代码在大多数情况下都可以正常工作,但偶尔会打印出两个相同编号的代码。

来自我的移动功能的代码:

    board[0][0] = 1;

    if (row + 1 > -1 && row + 1 < 5 && col + 2 > -1 && col + 2 < 5 && board[row + 1][col + 2] == 0) {
        move++;
        row += 1;
        col += 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 1 > -1 && row + 1 < 5 && col - 2 > -1 && col - 2 < 5 && board[row + 1][col - 2] == 0) {
        move++;
        row += 1;
        col -= 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 1 > -1 && row - 1 < 5 && col + 2 > -1 && col + 2 < 5 && board[row - 1][col + 2] == 0) {
        move++;
        row -= 1;
        col += 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 1 > -1 && row - 1 < 5 && col - 2 > -1 && col - 2 < 5 && board[row - 1][col - 2] == 0) {
        move++;
        row -= 1;
        col -= 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 2 > -1 && row - 2 < 5 && col + 1 > -1 && col + 1 < 5 && board[row - 2][col + 1] == 0) {
        move++;
        row -= 2;
        col += 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 2 > -1 && row - 2 < 5 && col - 1 > -1 && col - 1 < 5 && board[row - 2][col - 1] == 0) {
        move++;
        row -= 2;
        col -= 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 2 > -1 && row + 2 < 5 && col + 1 > -1 && col + 1 < 5 && board[row + 2][col + 1] == 0) {
        move++;
        row += 2;
        col += 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 2 > -1 && row + 2 < 5 && col - 1 > -1 && col - 1 < 5 && board[row + 2][col - 1] == 0) {
        move++;
        row += 2;
        col -= 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }
}


输出:

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  0  0
  0  0  0  0  0
  0  0  0  0  0
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  0  3
  3  0  0  0  0
  0  0  0  0  0
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  0  3
  3  0  4  0  0
  0  0  0  0  0
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  0  3
  3  0  4  0  0
  0  0  0  0  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  6  3
  3  0  4  0  0
  0  0  0  0  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  6  3
  3  7  4  0  0
  0  0  0  0  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  0  6  3
  3  7  4  0  0
  0  0  0  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  9  6  3
  3  7  4  0  0
  0  0  0  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  9  6  3
  3  7  4  0 10
 10  0  0  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0  0  9  6  3
  3  7  4  0 10
 10  0 11  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0 12  9  6  3
  3  7  4  0 10
 10  0 11  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0 12  9  6  3
  3  7  4 13 10
 10  0 11  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0  0
  0 12  9  6  3
  3  7  4 13 10
 10 14 11  8  5
Press any key to continue . . .

  1  0  0  0  0
  0  0  2  0 15
 15 12  9  6  3
  3  7  4 13 10
 10 14 11  8  5
Press any key to continue . . .

  1 16  0  0  0
  0  0  2  0 15
 15 12  9  6  3
  3  7  4 13 10
 10 14 11  8  5
Press any key to continue . . .

  1 16  0  0  0
  0  0  2 17 15
 15 12  9  6  3
  3  7  4 13 10
 10 14 11  8  5
Press any key to continue . . .


printBoard函数:

void printBoard() {
    cout << endl;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            cout << setw(3) << board[i][j];
        }
            cout << endl;
    }

}


整个程序:

// ConsoleApplication15.cpp : Defines the entry point for the console  application.


#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

void makeBoard();
void moveKnight(int row, int col, int move);
void printBoard();

int board[4][4];

int main()
{
    int row = 0;
int col = 0;
int move = 1;

makeBoard();
moveKnight(0, 0, 1);
printBoard();



system("pause");
return 0;
}

void makeBoard() {
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        board[i][j] = 0;

    }
}

}

void printBoard() {
cout << endl;
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        cout << setw(3) << board[i][j];
    }
    cout << endl;
}

}

void moveKnight(int row, int col, int move) {

board[0][0] = 1;

    if (row + 1 > -1 && row + 1 < 5 && col + 2 > -1 && col + 2 < 5 && board[row + 1][col + 2] == 0) {
        move++;
        row += 1;
        col += 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 1 > -1 && row + 1 < 5 && col - 2 > -1 && col - 2 < 5 && board[row + 1][col - 2] == 0) {
        move++;
        row += 1;
        col -= 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 1 > -1 && row - 1 < 5 && col + 2 > -1 && col + 2 < 5 && board[row - 1][col + 2] == 0) {
        move++;
        row -= 1;
        col += 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 1 > -1 && row - 1 < 5 && col - 2 > -1 && col - 2 < 5 && board[row - 1][col - 2] == 0) {
        move++;
        row -= 1;
        col -= 2;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 2 > -1 && row - 2 < 5 && col + 1 > -1 && col + 1 < 5 && board[row - 2][col + 1] == 0) {
        move++;
        row -= 2;
        col += 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row - 2 > -1 && row - 2 < 5 && col - 1 > -1 && col - 1 < 5 && board[row - 2][col - 1] == 0) {
        move++;
        row -= 2;
        col -= 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 2 > -1 && row + 2 < 5 && col + 1 > -1 && col + 1 < 5 && board[row + 2][col + 1] == 0) {
        move++;
        row += 2;
        col += 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }

    else if (row + 2 > -1 && row + 2 < 5 && col - 1 > -1 && col - 1 < 5 && board[row + 2][col - 1] == 0) {
        move++;
        row += 2;
        col -= 1;
        board[row][col] = move;
        printBoard();
        system("pause");
        moveKnight(row, col, move);
    }
}

最佳答案

尝试将int board[4][4];替换为int board[5][5]; :-)

关于c++ - 使用递归的骑士之旅,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41684578/

10-11 01:26