-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
378 lines (341 loc) · 8.23 KB
/
main.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
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
368
369
370
371
372
373
374
375
376
377
378
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#define SIZE 100
using namespace std;
struct Point
{
int x;
int y;
};
//GENERATE 1 & 2 ()
int** Create_board(int n, int m)
{
int** board = new int* [n];
for (int i = 0; i < n; i++)
board[i] = new int[m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &board[i][j]);
return board;
}
int Null_counts(int** board, int n, int m)
{
int s = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (board[i][j] == 0) s++;
return s;
}
int GetOponent(int n)
{
if (n == 1) return 2;
if (n == 2) return 1;
return 0;
}
void Display_board(int** board, int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
printf("%d ", board[i][j]);
printf("\n");
}
}
void Display_all_moves(int** board, int n, int m, int p, int moves)
{
printf("%d \n", moves);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (board[i][j] == 0)
{
board[i][j] = p; //DISPLAY ALL MOVES
Display_board(board, n, m);
board[i][j] = 0;
}
}
}
}
bool line(int** board, int n, int m, int k, int s, int l, int s1, int s2)
{
if (l == k || n == s1 - 1)
{
if (s == k) return true;
else return false;
}
l++;
if (board[n][m] == board[n + 1][m])
s++;
bool score = line(board, n + 1, m, k, s, l, s1, s2);
return score;
}
bool line2(int** board, int n, int m, int k, int s, int l, int s1, int s2)
{
if (l == k || m == s2 - 1)
{
if (s == k) return true;
else return false;
}
l++;
if (board[n][m] == board[n][m + 1])
s++;
bool score = line2(board, n, m + 1, k, s, l, s1, s2);
return score;
}
bool slant(int** board, int n, int m, int k, int s, int l, int s1, int s2)
{
if (l == k || n == s1 - 1 || m == s2 - 1)
{
if (s == k) return true;
else return false;
}
l++;
if (board[n][m] == board[n + 1][m + 1])
s++;
bool score = slant(board, n + 1, m + 1, k, s, l, s1, s2);
return score;
}
bool slant2(int** board, int n, int m, int k, int s, int l, int s1, int s2)
{
if (l == k || n == 0 || m == s2 - 1)
{
if (s == k) return true;
else return false;
}
l++;
if (board[n][m] == board[n - 1][m + 1])
s++;
bool score = slant2(board, n - 1, m + 1, k, s, l, s1, s2);
return score;
}
void Clearboard(int** board, int n)
{
for (int i = 0; i < n; i++)
delete[] board[i];
delete[] board;
}
int GameOver(int** board,int n, int m, int k)
{
bool row = false, col = false, sl = false, sl2 = false;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (board[i][j] != 0)
{
row = line2(board, i, j, k, 1, 1, n, m);
col = line(board, i, j, k, 1, 1, n, m);
sl = slant(board, i, j, k, 1, 1, n, m);
sl2 = slant2(board, i, j, k, 1, 1, n, m);
}
if (row == true || col == true || sl == true || sl2 == true)
{
if (board[i][j] == 1) return 1;
if (board[i][j] == 2) return -1;
}
}
}
return 0;
}
void Generate()
{
int n, m, k, p;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &k);
scanf("%d", &p);
int** board = Create_board(n, m);
int sum = Null_counts(board, n, m);
int score = GameOver(board, n, m, k);
if (score != 0)
{
printf("%d \n", 0);
Clearboard(board, n);
return;
}
Display_all_moves(board, n, m, p, sum);
Clearboard(board, n);
}
int Cut_if_end(int** board, int n, int m, int k,int p,int version)
{
bool row = false, col = false, sl = false, sl2 = false;
for (int x = 0; x < n; x++)
{
for (int y = 0; y < m; y++)
{
if (board[x][y] == 0)
{
board[x][y] = p;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (board[i][j] == p)
{
row = line2(board, i, j, k, 1, 1, n, m);
col = line(board, i, j, k, 1, 1, n, m);
sl = slant(board, i, j, k, 1, 1, n, m);
sl2 = slant2(board, i, j, k, 1, 1, n, m);
}
if (row == true || col == true || sl == true || sl2 == true)
{
if (version==1) board[x][y] = 0;
if (p==1) return 1;
if (p==2) return -1;
}
}
}
board[x][y] = 0;
}
}
}
return 0;
}
void Generate2()
{
int n, m, k, p;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &k);
scanf("%d", &p);
int** board = Create_board(n, m);
int sum = Null_counts(board, n, m);
int score = GameOver(board, n, m, k);
if (score != 0)
{
printf("%d \n", 0);
Clearboard(board, n);
return;
}
score=Cut_if_end(board, n, m, k, p,0);
if (score != 0)
{
printf("%d \n", 1);
Display_board(board, n, m);
Clearboard(board, n); //CUT THEN
return;
}
Display_all_moves(board, n, m, p, sum);
Clearboard(board, n);
}
//SOLVE()
Point* Get_Moves_Array(int** board, int n, int m)
{
int sum = Null_counts(board, n, m);
Point* table = new Point[sum];
int s = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (board[i][j] == 0)
{
table[s].x = i;
table[s].y = j;
s++;
}
}
}
return table;
}
int min(int a, int b)
{
if (a <= b) return a;
else return b;
}
int max(int a, int b)
{
if (a >= b) return a;
else return b;
}
int Minmax(int** board, int p, int n, int m, int k,int p_s)
{
int best;
Point* tab = Get_Moves_Array(board, n, m);
int score = GameOver(board, n, m, k);
if (score == 1)
{
delete[] tab;
return 1;
}
if (score == -1)
{
delete[] tab;
return -1;
}
score = Cut_if_end(board, n, m, k, p,1);
if (score == 1)
{
delete[] tab;
return 1;
}
if (score == -1)
{
delete[] tab;
return -1;
}
int sum = Null_counts(board, n, m);
if (sum == 0)
{
delete[] tab;
return 0;
}
if (p == 1)
{
best = -10;
for (int i = 0; i < sum; i++)
{
board[tab[i].x][tab[i].y] = p;
best = max(best, Minmax(board, GetOponent(p), n, m, k,p_s));
board[tab[i].x][tab[i].y] = 0;
if (best == 1) break;
}
delete[] tab;
return best;
}
else
{
best = 10;
for (int i = 0; i < sum; i++)
{
board[tab[i].x][tab[i].y] = p;
best = min(best, Minmax(board, GetOponent(p), n, m, k,p_s));
board[tab[i].x][tab[i].y] = 0;
if (best == -1) break;
}
delete[] tab;
return best;
}
}
void Solve()
{
int n, m, k, p;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &k);
scanf("%d", &p);
int** board = Create_board(n, m);
int score = Minmax(board, p, n, m, k, p);
if (score == 1) printf("FIRST_PLAYER_WINS\n");
else if (score == -1) printf("SECOND_PLAYER_WINS\n");
else if (score == 0) printf("BOTH_PLAYERS_TIE\n");
Clearboard(board, n);
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
char command[SIZE];
while (true)
{
scanf("%s", command);
if (feof(stdin) != 0)
{
break;
}
if (strcmp(command, "GEN_ALL_POS_MOV") == 0) Generate();
if (strcmp(command, "GEN_ALL_POS_MOV_CUT_IF_GAME_OVER") == 0) Generate2();
if (strcmp(command, "SOLVE_GAME_STATE") == 0) Solve();
}
}