-
Notifications
You must be signed in to change notification settings - Fork 2
/
wys.cpp
209 lines (176 loc) · 4.2 KB
/
wys.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
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
#include "wys.h"
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <queue>
#include <algorithm>
int n, k, g;
class State;
class Question;
std::unordered_map<int, State*> States;
std::unordered_set<int> UltimateStates;
int AllExcluded;
int* BitMasks;
std::queue<Question*> QuestionsQueue;
std::queue<State*> StatesQueue;
void generateBitMasks() {
BitMasks = new int[n];
int bit = 1;
for (int i = 0; i < n; i++) {
BitMasks[i] = bit;
bit *= k + 2;
}
}
void generateUltimateStates() {
AllExcluded = static_cast<int>(pow(k + 2, n)) - 1;
for (int i = 0; i < n; i++)
for (int j = 1; j <= k + 1; j++)
UltimateStates.insert(AllExcluded - j * BitMasks[i]);
}
bool isUltimate(int state) {
return UltimateStates.count(state);
}
bool isValid(int state) {
return state < AllExcluded;
}
bool isVisited(int state) {
return States.count(state);
}
int answerNo(int question, int state) {
for (int i = question; i >= 0; i--)
if ((state / BitMasks[i]) % (k + 2) < k + 1)
state += BitMasks[i];
return state;
}
int answerYes(int question, int state) {
for (int i = n - 1; i >= question + 1; i--)
if ((state / BitMasks[i]) % (k + 2) < k + 1)
state += BitMasks[i];
return state;
}
class Question {
public:
int question;
int stateYes;
int stateNo;
State* yes = nullptr;
State* no = nullptr;
Question(int q, int stateYes, int stateNo);
void AskQuestion();
int GetMaxStepsToWin();
};
Question::Question(int q, int yep, int nah) : question(q), stateYes(yep), stateNo(nah) {}
class State {
public:
Question* questionToAsk = nullptr;
int stepsToWin = 2137;
int state;
bool visited = false;
std::vector<Question*> questionsDown;
State(int s);
~State();
int GetMinStepsToWin();
};
State::State(int s) : state(s) {
States[state] = this;
if (!isUltimate(state)) {
for (int i = 0; i < n - 1; i++) {
int stateYes = answerYes(i, state);
if (stateYes == state || !isValid(stateYes))
continue;
int stateNo = answerNo(i, state);
if (stateNo == state || !isValid(stateNo))
continue;
questionsDown.push_back(new Question(i, stateYes, stateNo));
QuestionsQueue.push(questionsDown.back());
}
}
}
State::~State() {
delete questionToAsk;
for (Question* q : questionsDown) {
delete q;
}
}
int State::GetMinStepsToWin() {
if (visited)
return stepsToWin;
int stepsPerQuestion;
for (Question* question : questionsDown) {
stepsPerQuestion = question->GetMaxStepsToWin();
if (stepsPerQuestion < stepsToWin) {
stepsToWin = stepsPerQuestion;
questionToAsk = question;
}
}
++stepsToWin;
visited = true;
return stepsToWin;
}
void Question::AskQuestion() {
yes = isVisited(stateYes) ? States[stateYes] : new State(stateYes);
no = isVisited(stateNo) ? States[stateNo] : new State(stateNo);
}
int Question::GetMaxStepsToWin() {
if (yes == nullptr && no == nullptr)
return -420;
if (yes == nullptr)
return no->GetMinStepsToWin();
if (no == nullptr)
return yes->GetMinStepsToWin();
return std::max(yes->GetMinStepsToWin(), no->GetMinStepsToWin());
}
void init() {
States[0] = new State(0);
}
void buildGameTree() {
init();
while (!QuestionsQueue.empty()) {
QuestionsQueue.front()->AskQuestion();
QuestionsQueue.pop();
}
}
void createGamePaths() {
for (int ultimateState : UltimateStates) {
States[ultimateState]->stepsToWin = 0;
States[ultimateState]->visited = true;
}
States[0]->GetMinStepsToWin();
}
void setup() {
dajParametry(n, k, g);
generateBitMasks();
generateUltimateStates();
buildGameTree();
createGamePaths();
}
int parseAnswer(int state) {
int position = AllExcluded - state;
for (int i = 0; i < n; i++)
if (position / BitMasks[n - 1 - i] >= 1)
return n - i;
return -1;
}
void askAlice() {
State* currentState = States[0];
while (currentState->stepsToWin > 0) {
currentState = mniejszaNiz(currentState->questionToAsk->question + 2)
? States[answerYes(currentState->questionToAsk->question, currentState->state)]
: States[answerNo(currentState->questionToAsk->question, currentState->state)];
}
odpowiedz(parseAnswer(currentState->state));
}
void play() {
while (g--)
askAlice();
}
void cleanup() {
for (std::pair<int, State*> state : States)
delete state.second;
}
int main() {
setup();
play();
cleanup();
}