使用递归的骑士之旅
Knight's tour using recursion
现在我正处于骑士巡回赛的关键时刻,我只想让骑士能够移动,直到他不能再移动,然后才停下来。该代码在大多数情况下工作正常,但偶尔会打印出两个相同的数字。
我的移动函数代码:
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 . . .
打印板函数:
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[5][5];
替换 int board[4][4];
:-)
现在我正处于骑士巡回赛的关键时刻,我只想让骑士能够移动,直到他不能再移动,然后才停下来。该代码在大多数情况下工作正常,但偶尔会打印出两个相同的数字。
我的移动函数代码:
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 . . .
打印板函数:
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[5][5];
替换 int board[4][4];
:-)