Question
Hi would u offer me help on improving the following code ( for the Knight's Tour, C++,Recursion). It stuck at 15, I don't know how
Hi would u offer me help on improving the following code ( for the Knight's Tour, C++,Recursion). It stuck at 15, I don't know how to backtrack and get the number of tries.
// knightMove.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
using namespace std;
void printBoard();
void mBoard();
void moveKnight(int row, int col, int movNum);
int board[5][5];
int main()
{
int row = 0;
int col = 0;
int movNum = 0;
//int board[5][5];
//mboard();
//printBoard();
//mBoard();
moveKnight(0,0,1);
system("pause");
return 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 mBoard() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
board[i][j] = 0;
}
}
}
void moveKnight(int row, int col, int movNum) {
board[0][0] = 1;
if (row + 1 > -1 && row + 1 < 5 && col + 2 > -1 && col + 2 < 5 && board[row + 1][col + 2] == 0) {
movNum++;
row += 1;
col += 2;
board[row][col] = movNum;
printBoard();
system("pause");
moveKnight(row, col, movNum);
}
else if (row + 1 > -1 && row + 1 < 5 && col - 2 > -1 && col - 2 < 5 && board[row + 1][col - 2] == 0) {
movNum++;
row += 1;
col -= 2;
board[row][col] = movNum;
printBoard();
system("pause");
moveKnight(row, col, movNum);
}
else if (row - 1 > -1 && row - 1 < 5 && col - 2 > -1 && col - 2 < 5 && board[row - 1][col - 2] == 0) {
movNum++;
row -= 1;
col -= 2;
board[row][col] = movNum;
printBoard();
system("pause");
moveKnight(row, col, movNum);
}
else if (row - 1 > -1 && row - 1 < 5 && col + 2 > -1 && col + 2 < 5 && board[row - 1][col + 2] == 0) {
movNum++;
row -= 1;
col += 2;
board[row][col] = movNum;
printBoard();
system("pause");
moveKnight(row, col, movNum);
}
else if (row - 2 > -1 && row - 2 < 5 && col + 1 > -1 && col + 1 < 5 && board[row - 2][col + 1] == 0) {
movNum++;
row -= 2;
col += 1
;
board[row][col] = movNum;
printBoard();
system("pause");
moveKnight(row, col, movNum);
}
else if (row + 2 > -1 && row + 2 < 5 && col + 1 > -1 && col + 1 < 5 && board[row + 1][col + 1] == 0) {
movNum++;
row += 2;
col += 1;
board[row][col] = movNum;
printBoard();
system("pause");
moveKnight(row, col, movNum);
}
else if (row + 2 > -1 && row + 2 < 5 && col - 1 > -1 && col - 1 < 5 && board[row + 2][col - 1] == 0) {
movNum++;
row += 2;
col -= 1;
board[row][col] = movNum;
printBoard();
system("pause");
moveKnight(row, col, movNum);
}
else if (row - 2 > -1 && row - 2 < 5 && col - 1 > -1 && col - 1 < 5 && board[row - 2][col - 1] == 0) {
movNum++;
row -= 2;
col -= 1;
board[row][col] = movNum;
printBoard();
system("pause");
moveKnight(row, col, movNum);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started