-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment8.cpp
77 lines (69 loc) · 2.08 KB
/
Assignment8.cpp
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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
const int N = 8;
int calculateConflicts(const std::vector<int>& board) {
int conflicts = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (board[i] == board[j] || std::abs(board[i] - board[j]) == j - i) {
conflicts++;
}
}
}
return conflicts;
}
std::vector<int> generateRandomBoard() {
std::vector<int> board(N);
for (int i = 0; i < N; i++) {
board[i] = rand() % N;
}
return board;
}
void printBoard(const std::vector<int>& board) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[j] == i) std::cout << "Q ";
else std::cout << ". ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
std::vector<int> hillClimbing() {
std::vector<int> currentBoard = generateRandomBoard();
std::cout << "Generating conflicts less board..." << std::endl;
int currentConflicts = calculateConflicts(currentBoard);
while (currentConflicts > 0) {
bool improved = false;
for (int col = 0; col < N; col++) {
int originalRow = currentBoard[col];
for (int row = 0; row < N; row++) {
if (row != originalRow) {
currentBoard[col] = row;
int newConflicts = calculateConflicts(currentBoard);
if (newConflicts < currentConflicts) {
currentConflicts = newConflicts;
improved = true;
break;
}
}
}
if (improved) break;
currentBoard[col] = originalRow;
}
if (!improved) {
currentBoard = generateRandomBoard();
currentConflicts = calculateConflicts(currentBoard);
}
}
return currentBoard;
}
int main() {
srand(time(0));
std::vector<int> solution = hillClimbing();
std::cout << "Solution found:\n";
printBoard(solution);
return 0;
}