-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnqueen.cpp
More file actions
211 lines (197 loc) · 4.24 KB
/
nqueen.cpp
File metadata and controls
211 lines (197 loc) · 4.24 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fo(i, z, n) for (int i = z; i < N; i++)
#define Fo(i, k, n) for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define mod 1000000007
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define endl "\n"
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
#define N 8
void createPuzzle(int board[][N], int* state) {
srand(time(0));
fo(i,0,n) {
state[i] = rand() % N;
board[state[i]][i] = 1;
}
}
void printBoard(int board[][N]) {
fo(i,0,n) {
cout << " ";
fo(j,0,n) {
if(board[i][j] == 1){
cout << "Q" << " ";
}
else{
cout << "_" << " ";
}
}
cout << "\n";
}
}
bool compareStates(int* state1, int* state2) {
fo(i,0,n) {
if (state1[i] != state2[i]) {
return false;
}
}
return true;
}
void fill(int board[][N], int value) {
fo(i,0,n) {
fo(j,0,n) {
board[i][j] = value;
}
}
}
int attacks(int board[][N], int* state) {
int count = 0;
int row, col;
fo(i,0,n) {
row = state[i], col = i - 1;
while (col >= 0 && board[row][col] != 1) {
col--;
}
if (col >= 0 && board[row][col] == 1) {
count++;
}
row = state[i], col = i + 1;
while (col < N && board[row][col] != 1) {
col++;
}
if (col < N && board[row][col] == 1) {
count++;
}
row = state[i] - 1, col = i - 1;
while (col >= 0 && row >= 0 && board[row][col] != 1) {
col--;
row--;
}
if (col >= 0 && row >= 0 && board[row][col] == 1) {
count++;
}
row = state[i] + 1, col = i + 1;
while (col < N && row < N && board[row][col] != 1) {
col++;
row++;
}
if (col < N && row < N && board[row][col] == 1) {
count++;
}
row = state[i] + 1, col = i - 1;
while (col >= 0 && row < N && board[row][col] != 1) {
col--;
row++;
}
if (col >= 0 && row < N && board[row][col] == 1) {
count++;
}
row = state[i] - 1, col = i + 1;
while (col < N && row >= 0 && board[row][col] != 1) {
col++;
row--;
}
if (col < N && row >= 0 && board[row][col] == 1) {
count++;
}
}
return (int)(count / 2);
}
void gBoard(int board[][N], int* state) {
fill(board, 0);
fo(i,0,n) {
board[state[i]][i] = 1;
}
}
void copyState(int* state1, int* state2) {
fo(i,0,n) {
state1[i] = state2[i];
}
}
void states(int board[][N],int* state) {
int opb[N][N];
int ops[N];
copyState(ops, state);
gBoard(opb, ops);
int opo = attacks(opb, ops);
int nb[N][N];
int ns[N];
copyState(ns, state);
gBoard(nb,ns);
fo(i,0,n) {
fo(j,0,n) {
if (j != state[i]) {
ns[i] = j;
nb[ns[i]][i] = 1;
nb[state[i]][i] = 0;
int temp = attacks(nb, ns);
if (temp <= opo) {
opo = temp;
copyState(ops,ns);
gBoard(opb, ops);
}
nb[ns[i]][i] = 0;
ns[i] = state[i];
nb[state[i]][i] = 1;
}
}
}
copyState(state, ops);
fill(board, 0);
gBoard(board, state);
}
void hillClimbing(int board[][N], int* state) {
int nb[N][N] = {};
int ns[N];
copyState(ns, state);
gBoard(nb, ns);
do {
copyState(state, ns);
gBoard(board, state);
states(nb, ns);
if (compareStates(state,ns)) {
printBoard(board);
break;
}
else if (attacks(board, state) == attacks(nb,ns)) {
ns[rand() % N] = rand() % N;
gBoard(nb,ns);
}
} while (true);
}
int32_t main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int state[N] = {};
int board[N][N] = {};
createPuzzle(board, state);
hillClimbing(board, state);
return 0;
}
/*
Output :
_ _ _ Q _ _ _ _
_ _ _ _ _ Q _ _
Q _ _ _ _ _ _ _
_ _ _ _ Q _ _ _
_ Q _ _ _ _ _ _
_ _ _ _ _ _ _ Q
_ _ Q _ _ _ _ _
_ _ _ _ _ _ Q _
*/