-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTRIOMINO.cpp
89 lines (78 loc) · 2.21 KB
/
TRIOMINO.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
85
86
87
88
89
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm> // binsearch,max(a,b),min(a,b)
#include <math.h>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <string.h> // memset
#include <cstdlib> // abs(int),labs(int),llabs(int),min,max
#include <limits.h> // int_max,int_min,long_long_max,long_long_min
using namespace std;
int mem[810][2][2][2][2];
int grundy(int n,int leftLower, int leftUpper, int rightLower, int rightUpper) {
// base cases:
//cout << n << " " << leftLower << " " << leftUpper << " " << rightLower << " " << rightUpper << endl;
if(n==1 && !leftLower && !leftUpper && !rightUpper && !rightLower) {
return 0;
}
if(n==0) {
return 0;
}
if (mem[n][leftLower][leftUpper][rightLower][rightUpper]!=-1) {
//cout<<"Already stored"<<endl;
return mem[n][leftLower][leftUpper][rightLower][rightUpper];
}
set<int> reachable;
if(leftUpper || leftLower) {
reachable.insert(grundy(n-1,0,0,rightLower,rightUpper));
}
if(rightUpper || rightLower) {
reachable.insert(grundy(n-1,leftLower,leftUpper,0,0));
}
if (n >= 2) {
reachable.insert(grundy(n-2,0,1,rightLower,rightUpper));
reachable.insert(grundy(n-2,1,0,rightLower,rightUpper));
reachable.insert(grundy(n-2,leftLower,leftUpper,0,1));
reachable.insert(grundy(n-2,leftLower,leftUpper,1,0));
}
for(int i=2;i<=n-1;i++) {
int a,b,c,d;
// right orientations
a = grundy(i-1,leftLower,leftUpper,0,0);
b = grundy(n-i-1,0,1,rightLower,rightUpper);
c = grundy(n-i-1,1,0,rightLower,rightUpper);
reachable.insert(a^c);
reachable.insert(a^b);
// left orientations
a = grundy(n-i,0,0,rightLower,rightUpper);
b = grundy(i-2,leftLower,leftUpper,0,1);
c = grundy(i-2,leftLower,leftUpper,1,0);
reachable.insert(a^c);
reachable.insert(a^b);
}
// calculating mex of the reachable set
int answer = 0;
while(reachable.count(answer)) {
answer++;
}
return mem[n][leftLower][leftUpper][rightLower][rightUpper] = answer;
}
int main() {
int test;
memset(mem,-1,sizeof(mem));
cin>>test;
while(test--) {
int n;
cin>>n;
if(grundy(n,0,0,0,0)) {
cout<<"X"<<endl;
} else {
cout<<"Y"<<endl;
}
}
return 0;
}