-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnqueens_astar.cpp
84 lines (69 loc) · 2.15 KB
/
nqueens_astar.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
78
79
80
81
82
83
84
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
// Define a state representation for the N-Queens problem
struct State {
vector<int> queens; // queens[i] represents the column position of the queen in row i
// Calculate the heuristic value (number of attacking queen pairs)
int calculateHeuristic() const {
int heuristic = 0;
int n = queens.size();
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
// Check for attacking pairs in the same row or diagonally
if (queens[i] == queens[j] || abs(queens[i] - queens[j]) == abs(i - j)) {
heuristic++;
}
}
}
return heuristic;
}
// Overload the less than operator for priority queue ordering
bool operator<(const State& other) const {
return calculateHeuristic() > other.calculateHeuristic(); // Max heap
}
};
// A* search algorithm
bool solveNQueens(int n) {
priority_queue<State> pq;
State initial;
initial.queens.resize(n);
pq.push(initial);
while (!pq.empty()) {
State current = pq.top();
pq.pop();
if (current.calculateHeuristic() == 0) {
// Solution found
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << (current.queens[i] == j ? "Q" : ".");
}
cout << endl;
}
return true;
}
// Generate next states by moving one queen at a time
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
State next = current;
next.queens[i] = j;
pq.push(next);
}
}
}
// No solution found
return false;
}
int main() {
int n;
cout << "Enter the number of queens (N): ";
cin >> n;
if (solveNQueens(n)) {
cout << "Solution found!" << endl;
} else {
cout << "No solution exists." << endl;
}
return 0;
}