-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnAQ_Winner.cpp
More file actions
55 lines (50 loc) · 1.3 KB
/
nAQ_Winner.cpp
File metadata and controls
55 lines (50 loc) · 1.3 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
//=========================================================
// author: nvatuan
// n-queens game brute-force to predict the winner
//=========================================================
#include <bits/stdc++.h>
using namespace std;
int N;
const int SIZE = 1001;
int row[SIZE+1], col[SIZE+1];
int d[SIZE*4+4];
int *d1 = d+SIZE;
int *d2 = d+SIZE*3+2;
int valid(int x, int y){
return (!row[x] && !col[y] && !d1[x-y] && !d2[x+y]);
}
int play(int player, int move){
if(move == 1){
for(int i = 1; i <= (N+1)/2; i++)
for(int j = i; j <= (N+1)/2; j++){
row[i] = col[j] = d1[i-j] = d2[i+j] = 1;
if(!play(player ^ 1, move+1)) {
row[i] = col[j] = d1[i-j] = d2[i+j] = 0;
return 1;
}
row[i] = col[j] = d1[i-j] = d2[i+j] = 0;
}
return 0;
}
else{
for(int i = 1; i <= N; i++){
if(!row[i])
for(int j = 1; j <= N; j++){
if(valid(i, j)){
row[i] = col[j] = d1[i-j] = d2[i+j] = 1;
if(!play(player ^ 1, move+1)) {
row[i] = col[j] = d1[i-j] = d2[i+j] = 0;
return 1;
}
row[i] = col[j] = d1[i-j] = d2[i+j] = 0;
}
}
}
return 0;
}
}
int main(){
cout << "N = "; cin >> N;
cout << "\nWith board " << N << "*" << N <<'\n';
cout << (play(0, 1) ? "First player wins\n" : "Second player wins\n");
}