-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
405 lines (361 loc) · 9.58 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <chrono>
#include <algorithm>
using namespace std;
struct BestMoveVector
{
int row = -1;
int column = -1;
};
struct BestMove
{
public:
int boardScore = 0;
vector<BestMoveVector> bestMoveV;
};
short GRID_SIZE = 0;
short NO_OF_FRUIT_TYPES = 0;
float TIME_REMAINING = 0.0;
short MINIMAX_DEPTH = 0;
int MAX_CON_COMP = 0;
vector<vector<char>> FRUIT_BOARD;
BestMove BEST_MOVE;
bool Ascending(BestMove& i, BestMove& j)
{
return i.boardScore > j.boardScore;
}
bool Descending(BestMove& i, BestMove& j)
{
return i.boardScore < j.boardScore;
}
void ReadFile()
{
string argumnets;
ifstream inputFile;
inputFile.open("input.txt");
int lineIndex = 0;
if (inputFile.is_open())
{
while (!inputFile.eof())
{
getline(inputFile, argumnets);
if (argumnets != "")
{
if (lineIndex == 0)
{
GRID_SIZE = stoi(argumnets);
FRUIT_BOARD.resize(GRID_SIZE, vector<char>(GRID_SIZE));
lineIndex += 1;
}
else if (lineIndex == 1)
{
NO_OF_FRUIT_TYPES = stoi(argumnets);
lineIndex += 1;
}
else if (lineIndex == 2)
{
TIME_REMAINING = stof(argumnets);
lineIndex += 1;
}
else
{
for (int j = 0; j < GRID_SIZE; j++)
{
if (lineIndex - 3 < GRID_SIZE)
{
FRUIT_BOARD[lineIndex - 3][j] = argumnets[j];
}
}
lineIndex += 1;
}
}
}
}
inputFile.close();
}
vector<vector<char>> RunGravity(vector<vector<char>> bestMove)
{
for (int row = GRID_SIZE - 1; row > 0; row--)
{
for (int column = 0; column < GRID_SIZE; column++)
{
if (bestMove[row][column] == '*')
{
int cell = row - 1;
while (bestMove[cell][column] == '*' && cell >= 0)
{
if (cell == 0)
break;
cell--;
}
bestMove[row][column] = bestMove[cell][column];
bestMove[cell][column] = '*';
}
}
}
return bestMove;
}
vector<vector<char>> GenerateFruitGrid(vector<vector<char>> fruitGrid, BestMoveVector bestMoveV)
{
queue<BestMoveVector> traverseLocations;
BestMoveVector nextMove;
vector<vector<bool>> vFruits(GRID_SIZE, vector<bool>(GRID_SIZE));
BestMoveVector newNextMove;
traverseLocations.push(bestMoveV);
while (traverseLocations.size() != 0)
{
nextMove = traverseLocations.front();
traverseLocations.pop();
char fruitType = fruitGrid[nextMove.row][nextMove.column];
if (nextMove.row + 1 < GRID_SIZE && (fruitGrid[nextMove.row + 1][nextMove.column] == fruitType && !vFruits[nextMove.row + 1][nextMove.column]))
{
newNextMove.row = nextMove.row + 1;
newNextMove.column = nextMove.column;
traverseLocations.push(newNextMove);
vFruits[newNextMove.row][newNextMove.column] = 1;
}
if (nextMove.row - 1 >= 0 && (fruitGrid[nextMove.row - 1][nextMove.column] == fruitType && !vFruits[nextMove.row - 1][nextMove.column]))
{
newNextMove.row = nextMove.row - 1;
newNextMove.column = nextMove.column;
traverseLocations.push(newNextMove);
vFruits[newNextMove.row][newNextMove.column] = 1;
}
if (nextMove.column + 1 < GRID_SIZE && (fruitGrid[nextMove.row][nextMove.column + 1] == fruitType && !vFruits[nextMove.row][nextMove.column + 1]))
{
newNextMove.row = nextMove.row;
newNextMove.column = nextMove.column + 1;
traverseLocations.push(newNextMove);
vFruits[newNextMove.row][newNextMove.column] = 1;
}
if (nextMove.column - 1 >= 0 && (fruitGrid[nextMove.row][nextMove.column - 1] == fruitType && !vFruits[nextMove.row][nextMove.column - 1]))
{
newNextMove.row = nextMove.row;
newNextMove.column = nextMove.column - 1;
traverseLocations.push(newNextMove);
vFruits[newNextMove.row][newNextMove.column] = 1;
}
vFruits[nextMove.row][nextMove.column] = 1;
fruitGrid[nextMove.row][nextMove.column] = '*';
}
fruitGrid = RunGravity(fruitGrid);
return fruitGrid;
}
vector<BestMove> ComputeConnectedComponents(BestMove bestMove, bool maxPlayer)
{
vector<vector<char>> iFBoard = FRUIT_BOARD;
vector<BestMove> childBestMoveVector;
vector<vector<bool>> vFruits(GRID_SIZE, vector<bool>(GRID_SIZE));;
BestMoveVector newNextMove;
int count = 0;
for (int i = 0; i < bestMove.bestMoveV.size(); i++)
{
if (bestMove.bestMoveV[i].row >= 0 && bestMove.bestMoveV[i].column >= 0)
{
iFBoard = GenerateFruitGrid(iFBoard, bestMove.bestMoveV[i]);
}
}
for (int row = 0; row < GRID_SIZE; row++)
{
for (int column = 0; column < GRID_SIZE; column++)
{
if (iFBoard[row][column] == '*')
vFruits[row][column] = 1;
if (vFruits[row][column] != 1)
{
int score = 0;
BestMove newBestMove;
BestMoveVector bestMoveV;
bestMoveV.row = row;
bestMoveV.column = column;
queue<BestMoveVector> nextMoves;
newBestMove.boardScore = 1;
newBestMove.bestMoveV.push_back(bestMoveV);
childBestMoveVector.push_back(newBestMove);
nextMoves.push(bestMoveV);
vFruits[row][column] = 1;
while (nextMoves.size() != 0)
{
score++;
char fruitType = iFBoard[row][column];
BestMoveVector nextMove = nextMoves.front();
nextMoves.pop();
if (nextMove.row + 1 < GRID_SIZE && (iFBoard[nextMove.row + 1][nextMove.column] == fruitType && !vFruits[nextMove.row + 1][nextMove.column]))
{
newNextMove.row = nextMove.row + 1;
newNextMove.column = nextMove.column;
nextMoves.push(newNextMove);
vFruits[nextMove.row + 1][nextMove.column] = 1;
}
if (nextMove.row - 1 >= 0 && (iFBoard[nextMove.row - 1][nextMove.column] == fruitType && !vFruits[nextMove.row - 1][nextMove.column]))
{
newNextMove.row = nextMove.row - 1;
newNextMove.column = nextMove.column;
nextMoves.push(newNextMove);
vFruits[nextMove.row - 1][nextMove.column] = 1;
}
if (nextMove.column + 1 < GRID_SIZE && (iFBoard[nextMove.row][nextMove.column + 1] == fruitType && !vFruits[nextMove.row][nextMove.column + 1]))
{
newNextMove.row = nextMove.row;
newNextMove.column = nextMove.column + 1;
nextMoves.push(newNextMove);
vFruits[nextMove.row][nextMove.column + 1] = 1;
}
if (nextMove.column - 1 >= 0 && (iFBoard[nextMove.row][nextMove.column - 1] == fruitType && !vFruits[nextMove.row][nextMove.column - 1]))
{
newNextMove.row = nextMove.row;
newNextMove.column = nextMove.column - 1;
nextMoves.push(newNextMove);
vFruits[nextMove.row][nextMove.column - 1] = 1;
}
}
childBestMoveVector[count].boardScore = score * score;
count++;
}
}
}
return childBestMoveVector;
}
vector<BestMove> AppendMovesAndCalCumScore(BestMove bestMove, vector<BestMove> bestMoveVector, bool maxPlayer)
{
for (int i = 0; i < bestMoveVector.size(); i++)
{
BestMove newBestMove;
newBestMove.bestMoveV = bestMove.bestMoveV;
newBestMove.boardScore = bestMove.boardScore;
if (maxPlayer)
newBestMove.boardScore += bestMoveVector[i].boardScore;
else
newBestMove.boardScore -= bestMoveVector[i].boardScore;
if (bestMoveVector[i].bestMoveV.size() > 0)
newBestMove.bestMoveV.push_back(bestMoveVector[i].bestMoveV[0]);
bestMoveVector[i] = newBestMove;
}
return bestMoveVector;
}
int RunMiniMaxAlgorithm(int depth, bool maxPlayer, BestMove bestMove, int alpha, int beta)
{
if (depth == 0)
return bestMove.boardScore;
vector<BestMove> allBestMoves = ComputeConnectedComponents(bestMove, maxPlayer);
if (allBestMoves.size() == 0)
return bestMove.boardScore;
allBestMoves = AppendMovesAndCalCumScore(bestMove, allBestMoves, maxPlayer);
for (int i = 0; i < allBestMoves.size(); i++)
{
if (maxPlayer)
{
sort(allBestMoves.begin(), allBestMoves.end(), Ascending);
int currentScore = RunMiniMaxAlgorithm(depth - 1, false, allBestMoves[i], alpha, beta);
if (currentScore > alpha)
{
alpha = currentScore;
if (depth == MINIMAX_DEPTH)
{
BEST_MOVE = allBestMoves[i];
BEST_MOVE.boardScore = currentScore;
}
}
if (alpha >= beta) {
break;
}
}
else
{
sort(allBestMoves.begin(), allBestMoves.end(), Descending);
int currentScore = RunMiniMaxAlgorithm(depth - 1, true, allBestMoves[i], alpha, beta);
beta = min(currentScore, beta);
if (alpha >= beta) {
break;
}
}
}
if (maxPlayer)
return alpha;
else
return beta;
}
void StartPlayingGame()
{
BestMove initialBestMove;
RunMiniMaxAlgorithm(MINIMAX_DEPTH, true, initialBestMove, INT_MIN, INT_MAX);
}
void WriteOutputFile()
{
ofstream file;
file.open("output.txt");
int bestRow = BEST_MOVE.bestMoveV[0].row;
int bestColumn = BEST_MOVE.bestMoveV[0].column;
vector<vector<char>> finalBest = GenerateFruitGrid(FRUIT_BOARD, BEST_MOVE.bestMoveV[0]);
file << (char)((bestColumn % 26) + 'A') << bestRow + 1 << endl;
for (int i = 0; i < finalBest.size(); i++)
{
for (int j = 0; j < finalBest.size(); j++)
{
file << finalBest[i][j];
}
file << endl;
}
file << endl;
}
int FindDepth()
{
int depth = 3;
if (TIME_REMAINING < 1)
{
return 1;
}
else if (TIME_REMAINING < 2)
{
return 2;
}
else
{
if (GRID_SIZE < 6) {
depth = 6;
}
else if (GRID_SIZE < 17) {
if (MAX_CON_COMP <= 100) {
depth = 5;
}
else if (MAX_CON_COMP <= 200) {
depth = 4;
}
}
else if (GRID_SIZE < 22) {
if (MAX_CON_COMP < 60) {
depth = 5;
}
else if (MAX_CON_COMP < 100) {
depth = 4;
}
}
else if (GRID_SIZE < 27) {
if (MAX_CON_COMP < 40) {
depth = 5;
}
else if (MAX_CON_COMP < 80) {
depth = 4;
}
}
}
return depth;
}
int main()
{
ReadFile();
BestMove initialBestMove;
MAX_CON_COMP = ComputeConnectedComponents(initialBestMove, true).size();
MINIMAX_DEPTH = FindDepth();
StartPlayingGame();
WriteOutputFile();
}