-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbruteforce.cc
66 lines (58 loc) · 1.1 KB
/
bruteforce.cc
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
#include <iostream>
#include <climits>
using namespace std;
void doMove( int m, int A[] ) {
A[m-1]++;
A[m]++;
A[m+1]++;
}
void undoMove( int m, int A[] ) {
A[m-1]--;
A[m]--;
A[m+1]--;
}
int recursiveWin( int A[], int n, int& move) {
int result = -1;
for( int i = 0; i < n; i++ ) {
if( A[i] == 0 ) {
doMove( i, A );
result = -recursiveWin( A, n, move );
undoMove( i, A );
if( result > 0 ) {
move = i;
return result;
}
}
}
return result;
}
int recursiveLose( int A[], int n, int& move) {
int result = 1;
for( int i = 0; i < n; i++ ) {
if( A[i] == 0 ) {
doMove( i, A );
result = -recursiveLose( A, n, move );
undoMove( i, A );
if( result > 0 ) {
move = i;
return result;
}
}
}
return result;
}
int main() {
int n = 100;
int* B = new int[n+2];
int* A = B+1;
int move = 0;
for( int i = 0; i < n; i++ )
A[i] = 0;
for( int i = 0; i < n; i++ ) {
cout << i << ":\n win: " << recursiveWin( A, i, move );
cout << "(" << move << ")" << endl;
cout << " lose: " << recursiveLose( A, i, move );
cout << "(" << move << ")" << endl;
}
delete [] B;
}