Count how many times a solution is printed

For the life of me, I cannot figure out how to count each time I print the solution, and the total number of solutions. I know it is something I forgot from earlier classes, and any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  /** ================================================================
 *@file			KnightsTour.cpp
 *@author		Taylor Bradley	
 *@version		2.0

 This recursive function solves knights tour 
 based on a set of pre defined size constants
 and and a starting location to be passed in
 when function is called. 
 * =================================================================
 */
#include <iostream>
using namespace std;
const int ROW = 5;
const int COL = 5;
struct Board {
	int chessboard[ROW][COL];

};
/** ================================================================
 * Recursively solves the knights tour using brute force
 * Prints any solutions it finds.
 *
 * @param board - the board we’re working with (contains all moves-to-date)
 * @param row   - row we’re going to attempt to place the knight on this move.
 * @param col   - column we’re going to attempt to place the knight on this move.
 * @param currentMoveNumber - the move# we’re making (1=first move)
 *
 * @return The number of solutions the given board and move leads to
 * =================================================================
 */
int solveKnightsTour(Board board, int row, int col, int move) {
	int solution = 1;
	//Validate that row and col are not out of bounds
	//and validate that spot is not occupied
	if (row < 0 || col < 0 || row > ROW - 1 || col > COL - 1 || board.chessboard[row][col] > 0) {
		return 0;
	};
	board.chessboard[row][col] = move;
	if(move == 25) {
		
		//print the board
		cout << '\n';
		cout << "Solution # "<< solution << '\n';
		cout << '\n';
		for (int row = 0; row < ROW; row++) {
			for (int col = 0; col < COL; col++) {
				cout << board.chessboard[row][col] << ' ';
			}
			cout << endl;
			
		}
		return 1;
		
	}
	solveKnightsTour(board, row + 2, col + 1, move + 1);	
	solveKnightsTour(board, row + 1, col + 2, move + 1);	
	solveKnightsTour(board, row - 1, col + 2, move + 1);
	solveKnightsTour(board, row - 2, col + 1, move + 1);
	solveKnightsTour(board, row - 2, col - 1, move + 1);
	solveKnightsTour(board, row - 1, col - 2, move + 1);	
	solveKnightsTour(board, row + 1, col - 2, move + 1);	
	solveKnightsTour(board, row + 2, col - 1, move + 1);
	
	return 1;
};


int main() {
	Board myBoard;
	int totalSolutions = 0;
	for (int x = 0; x < ROW; x++) {
		for (int y = 0; y < COL; y++)
			myBoard.chessboard[x][y] = 0;
	}
	cout << "Welcome to Knight's tour!" << '\n';
	cout << '\n';
	cout << "Current board size = " << ROW << " x " << COL << '\n';
	cout << "Starting position is 2,2. " << '\n';
	cout << '\n';
	cout << "Thinking....." << '\n';
	cout << '\n';
	solveKnightsTour(myBoard, 2, 2, 1);










	return 1;
}
Well you have a return statement.

Which you ignore all over the place.

Consider
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
int solveKnightsTour(Board board, int row, int col, int move) {
	int solution = 1;
	//Validate that row and col are not out of bounds
	//and validate that spot is not occupied
	if (row < 0 || col < 0 || row > ROW - 1 || col > COL - 1 || board.chessboard[row][col] > 0) {
		return 0;
	};
	board.chessboard[row][col] = move;
	if(move == 25) {
		
		//print the board
		cout << '\n';
		cout << "Solution # "<< solution << '\n';
		cout << '\n';
		for (int row = 0; row < ROW; row++) {
			for (int col = 0; col < COL; col++) {
				cout << board.chessboard[row][col] << ' ';
			}
			cout << endl;
			
		}
		return solution;
	}
	solution += solveKnightsTour(board, row + 2, col + 1, move + 1);	
	solution += solveKnightsTour(board, row + 1, col + 2, move + 1);	
	solution += solveKnightsTour(board, row - 1, col + 2, move + 1);
	solution += solveKnightsTour(board, row - 2, col + 1, move + 1);
	solution += solveKnightsTour(board, row - 2, col - 1, move + 1);
	solution += solveKnightsTour(board, row - 1, col - 2, move + 1);	
	solution += solveKnightsTour(board, row + 1, col - 2, move + 1);	
	solution += solveKnightsTour(board, row + 2, col - 1, move + 1);
	
	return solution;
};


int main() {
	Board myBoard;
	int totalSolutions = 0;
	for (int x = 0; x < ROW; x++) {
		for (int y = 0; y < COL; y++)
			myBoard.chessboard[x][y] = 0;
	}
	cout << "Welcome to Knight's tour!" << '\n';
	cout << '\n';
	cout << "Current board size = " << ROW << " x " << COL << '\n';
	cout << "Starting position is 2,2. " << '\n';
	cout << '\n';
	cout << "Thinking....." << '\n';
	cout << '\n';
	int totalSolutions = solveKnightsTour(myBoard, 2, 2, 1);
	cout << "Number of solutions = " << totalSolutions << '\n';

	return 0; // 0 is success, 1 is well probably an error
}

Topic archived. No new replies allowed.