-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysub.c
367 lines (350 loc) · 10.2 KB
/
mysub.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <stdbool.h>
/* * * * * * * * * * * * * * *
* * * * GLOBAL VARIABLE * * *
* * * * * * * * * * * * * * */
int type_one_index_1 = 0;
int type_one_index_2 = 0;
int type_two_index_1 = 0;
int type_two_index_2 = 0;
int type_one_cnt = 0;
int type_two_cnt = 0;
int visited_cnt = 0;
int mid = 0;
int depth = 0;
/* * * * * * * * * * * * * * *
* * * * HELPER FUNCTION * * *
* * * * * * * * * * * * * * */
void init() {
type_one_index_1 = 0;
type_one_index_2 = 0;
type_two_index_1 = 0;
type_two_index_2 = 0;
type_one_cnt = 0;
type_two_cnt = 0;
visited_cnt = 0;
}
bool test_if_a_winner() {
return type_one_cnt >= mid || type_two_cnt >= mid;
}
int diff(int a, int b) {
return a >= b ? (a - b) : (b - a);
}
void classify(int n, int depth) {
int p = 0, q = 0;
if (depth == 0) {
int a[4] = {1, 2, 3, 4}, b[4] = {2, 3, 4, 5};
p = QCOUNT(1, a), q = QCOUNT(1, b);
} else {
int a[4] = {depth + 2, depth + 3, depth + 4, depth + 5};
p = 4, q = QCOUNT(1, a);
}
if (p == 0 && q == 0) {
// 0 1 0 1 0
type_one_index_1 = 1;
type_one_index_2 = 5;
int combination[3][3] = {{2, 3}, {2, 4}, {3, 4}};
for (int i = 0; i < 3; ++i) {
int a[4] = {1, combination[i][0], combination[i][1], 5};
int res = QCOUNT(1, a);
if (res == 0) {
type_two_index_1 = combination[i][0];
type_two_index_2 = combination[i][1];
break;
}
}
type_one_cnt = 3;
type_two_cnt = 2;
visited_cnt = 5;
} else if (p == 0 && q == 2) {
// 0 1 0 1 1
type_one_index_1 = 5;
type_two_index_1 = 1;
int combination[3][3] = {{2, 3}, {2, 4}, {3, 4}};
for (int i = 0; i < 3; ++i) {
int a[4] = {1, combination[i][0], combination[i][1], 5};
int res = QCOUNT(1, a);
if (res == 2) {
type_one_index_2 = combination[i][0];
type_two_index_2 = combination[i][2];
break;
}
}
type_one_cnt = 3;
type_two_cnt = 2;
visited_cnt = 5;
} else if (p == 2 && q == 0) {
// 0 0 0 1 1
type_one_index_1 = 1;
type_two_index_1 = 5;
int combination[3][3] = {{2, 3, 4}, {2, 4, 3}, {3, 4, 2}};
for (int i = 0; i < 3; ++i) {
int a[4] = {1, combination[i][0], combination[i][1], 5};
int res = QCOUNT(1, a);
if (res == 2) {
type_one_index_2 = combination[i][0];
type_two_index_2 = combination[i][2];
break;
}
}
type_one_cnt = 3;
type_two_cnt = 2;
visited_cnt = 5;
} else if (p == 2 && q == 2) {
// 0 1 0 0 0
type_one_index_1 = 1;
type_one_index_2 = 5;
int combination[3][3] = {{2, 3, 4}, {2, 4, 3}, {3, 4, 2}};
bool flag = false;
for (int i = 0; i < 3; ++i) {
int a[4] = {1, combination[i][0], combination[i][1], 5};
int res = QCOUNT(1, a);
if (res == 4) {
flag = true;
type_two_index_1 = combination[i][2];
break;
}
}
if (!flag) {
// 0 1 1 1 0
type_two_index_1 = 2;
type_two_index_2 = 3;
type_one_cnt = 2;
type_two_cnt = 3;
} else {
type_one_cnt = 4;
type_two_cnt = 1;
}
visited_cnt = 5;
} else if (p == 2 && q == 4) {
// 1 0 0 0 0
type_two_index_1 = 1;
type_one_index_1 = 2;
type_one_index_2 = 3;
type_one_cnt = 4;
type_two_cnt = 1;
visited_cnt = 5;
} else if (p == 4 && q == 2) {
// 0 0 0 0 1
type_one_index_1 = 1;
type_one_index_2 = 2;
type_two_index_1 = depth + 5;
type_one_cnt += depth + 4;
type_two_cnt += 1;
visited_cnt += depth + 5;
} else if (p == 4 && q == 4) {
// 0 0 0 0 0
int mid = n / 2 + 1;
if (depth + 5 <= mid) {
classify(n, depth + 1);
} else {
visited_cnt = mid;
type_one_cnt = mid;
type_one_index_1 = 1;
type_one_index_2 = 2;
}
}
}
void update_cnt_two(int n, int* array_n, int cnt_n) {
for (int i = 0; i < cnt_n; ++i) {
int p = array_n[i];
int q = array_n[i] + 1;
int a[4] = {p, q, type_one_index_1, type_one_index_2};
int res = QCOUNT(1, a);
switch (res) {
case 0: {
// 0 0 || 1 1 1 0
type_one_cnt += 1;
type_two_cnt += 3;
break;
}
case 2: {
// 0 0 || 0 1 x x
type_one_cnt += 1;
type_two_cnt += 1;
int first = array_n[i] + 2;
int second = array_n[i] + 3;
int b[4] = {first, second, type_one_index_1, type_one_index_2};
int v = QCOUNT(1, b);
switch (v) {
case 0: {
// 0 0 || 1 1
type_two_cnt += 2;
break;
}
case 4: {
// 0 0 || 00
type_one_cnt += 2;
break;
}
}
break;
}
case 4: {
// 0 0 || 0 0 0 1
type_one_cnt += 3;
type_two_cnt += 1;
break;
}
}
if (test_if_a_winner()) return;
}
}
void update_cnt_four(int n, int* array_n, int cnt_n) {
bool is_remaining = true;
for (int i = 0; i <= cnt_n - 2; i += 2) {
if (i == cnt_n - 2) {
is_remaining = false;
}
int first = array_n[i];
int second = array_n[i + 1];
int a[4] = {first, second, type_one_index_1, type_one_index_2};
int res = QCOUNT(1, a);
switch (res) {
case 0: {
type_two_cnt += 8;
break;
}
case 2: {
type_one_cnt += 4;
type_two_cnt += 4;
break;
}
case 4: {
type_one_cnt += 8;
break;
}
}
if (test_if_a_winner()) return;
}
if (is_remaining) {
int p = array_n[cnt_n - 1];
int q = array_n[cnt_n - 1] + 1;
int b[4] = {p, q, type_one_index_1, type_one_index_2};
int res = QCOUNT(1, b);
switch (res) {
case 0: {
// 0 0 || 1 1 1 1
type_two_cnt += 4;
break;
}
case 4: {
// 0 0 || 0 0 0 0
type_one_cnt += 4;
break;
}
}
}
}
void update_cnt_leftover(int n) {
bool is_remaining = true;
while (visited_cnt + 2 <= n) {
if (visited_cnt + 2 == n) {
is_remaining = false;
}
int a[4] = {visited_cnt + 1, visited_cnt + 2, type_one_index_1, type_one_index_2};
int res = QCOUNT(1, a);
switch (res) {
case 0: {
// 0 0 || 1 1
type_two_cnt += 2;
break;
}
case 2: {
// 0 0 || 0 1
type_one_cnt += 1;
type_two_cnt += 1;
break;
}
case 4: {
// 0 0 || 0 0
type_one_cnt += 2;
break;
}
}
visited_cnt += 2;
if (test_if_a_winner()) return;
}
if (is_remaining) {
// a
int d = diff(type_one_cnt, type_two_cnt);
if (d > 1) return;
int a[4] = {type_one_index_1, type_one_index_2, type_two_index_1, visited_cnt + 1};
int res = QCOUNT(1, a);
switch (res) {
case 0: {
// 0 0 1 || 1
type_two_cnt += 1;
break;
}
case 2: {
// 0 0 1 || 0
type_one_cnt += 1;
break;
}
}
}
}
void update_cnt(int n, int* array_two, int* array_four, int cnt_two, int cnt_four) {
if (cnt_four > 0) update_cnt_four(n, array_four, cnt_four);
if (cnt_two > 0) update_cnt_two(n, array_two, cnt_two);
}
void forward_check_by_four(int n) {
int leftover = n - visited_cnt;
int remainder = leftover % 4;
int groups = leftover / 4;
int groups_with_start_index[groups];
memset(groups_with_start_index, -1, sizeof(groups_with_start_index));
for (int i = visited_cnt, j = 0; i + 4 <= n; i += 4, ++j) {
groups_with_start_index[j] = i + 1;
}
int array_two[groups];
int array_four[groups];
memset(array_two, -1, sizeof(array_two));
memset(array_four, -1, sizeof(array_four));
int cnt_two = 0, cnt_four = 0;
for (int i = 0; i < groups; ++i) {
int a[4] = {groups_with_start_index[i], groups_with_start_index[i] + 1,
groups_with_start_index[i] + 2, groups_with_start_index[i] + 3};
int res = QCOUNT(1, a);
switch (res) {
case 0: {
type_one_cnt += 2;
type_two_cnt += 2;
break;
}
case 2: {
array_two[cnt_two++] = a[0];
break;
}
case 4: {
array_four[cnt_four++] = a[0];
break;
}
default:
break;
}
if (test_if_a_winner()) return;
}
update_cnt(n, array_two, array_four, cnt_two, cnt_four);
visited_cnt = n - remainder;
}
/* * * * * * * * * * * * * * *
* * * * MYSUB FUNCTION * * *
* * * * * * * * * * * * * * */
int mysub(int n) {
depth = 0;
mid = n / 2 + 1;
init();
classify(n, depth);
forward_check_by_four(n);
if (type_one_cnt < mid && type_two_cnt < mid && visited_cnt < n) {
update_cnt_leftover(n);
}
if (type_one_cnt > type_two_cnt) {
return type_one_index_1;
}
if (type_one_cnt < type_two_cnt) {
return type_two_index_1;
}
return 0;
}