Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
88 lines (74 loc) · 2.4 KB

File metadata and controls

88 lines (74 loc) · 2.4 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//----------------------------------------------------------------
//Author: Karl Anthony James
//E-Mail: kjames21@uco.edu
//Course: CMSC 3613 - Data Structures and Algorithms
//CRN: 26119, Spring 2013
//Project: PA1
//Due: Febuary 20, 2013
//Account gp034
//----------------------------------------------------------------
#include <iostream>
#include "Knights.h"
using namespace std;
//Function: Solve - Recursive function used to solve the knights tour problem.
bool Solve(Knights &configuration, int row, int col) {
if(configuration.IsSolved()) {
configuration.Print();
return true;
}
else if(configuration.IsCellValid(row, col) && !configuration.IsCellVisited(row, col)) {
configuration.VisitCell(row, col);
bool success = Solve(configuration, row + 1, col - 2);
if(!success) {
success = Solve(configuration, row - 1, col - 2);
if(!success) {
success = Solve(configuration, row + 1, col + 2);
if(!success) {
success = Solve(configuration, row - 1, col + 2);
if(!success) {
success = Solve(configuration, row - 2, col + 1);
if(!success) {
success = Solve(configuration, row - 2, col - 1);
if(!success) {
success = Solve(configuration, row + 2, col + 1);
if(!success) {
success = Solve(configuration, row + 2, col - 1);
}
}
}
}
}
}
}
if(!success) configuration.UnVisitCell(row, col);
else return true;
}
return false;
}
int main() {
int boardSize;
cout << "Enter the board size: " << flush;
cin >> boardSize;
if(boardSize < MIN_BOARD || boardSize > MAX_BOARD)
cout << "The board size must be between " << MIN_BOARD << " and " << MAX_BOARD << endl;
else {
Knights configuration(boardSize);
int startRow, startCol;
cout << "Enter the starting row: " << flush;
cin >> startRow;
if(!configuration.IsCellValid(startRow, 0))
cout << "The starting row must be between 0 and " << configuration.boardSize - 1 << endl;
else {
cout << "Enter the starting column: " << flush;
cin >> startCol;
if(!configuration.IsCellValid(startRow, startCol))
cout << "The starting column must be between 0 and " << configuration.boardSize - 1 << endl;
else {
bool success = Solve(configuration, startRow, startCol);
if(!success) {
cout << "No solution found." << endl;
}
}
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.