-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
600 lines (524 loc) · 16 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
NAME: Mitch Worsey
STUDENT ID: 8370927291
*/
#include <cstdlib>
#include <iostream>
#include <sys/time.h>
#include <stdio.h>
#include "Timer.h"
#include <vector>
#include <cmath>
#include <limits>
#include <ctime>
using namespace std;
struct Edge {
int r, c, depth, jump, parentR, parentC;
Edge(int row = 0, int col = 0, int dDepth = 0, int jJump = 0, int parR = 0, int parC = 0) :
r(row), c(col), depth(dDepth), jump(jJump), parentR(parR), parentC(parC) { }
};
class Grid {
int height, width;
vector< vector <int> > tiles;
public:
Grid(int, int, int**); // construct the grid
int BFS();
vector<Edge> reachableBFS();
vector<Edge> reachingBFS();
};
Grid::Grid(int h, int w, int **puzz){
height = h;
width = w;
tiles.resize(height, vector<int>(width, 0));
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
tiles[i][j] = puzz[i][j];
}
vector<Edge> Grid::reachableBFS(){
vector<Edge> reachable;
vector< vector<int> > origTiles;
origTiles.resize(height, vector<int>(width, 0));
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
origTiles[i][j] = tiles[i][j];
for(int row = 0; row < height; row++){
for(int col = 0; col < width; col++){
if(row != height-1 || col != width-1){
vector<Edge> openList, closedList;
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
tiles[i][j] = origTiles[i][j];
openList.push_back(Edge(0, 0));
do{
closedList.push_back(openList[0]);
openList.erase(openList.begin());
int r = closedList.back().r;
int c = closedList.back().c;
int depth = closedList.back().depth;
if(r == row && c == col){
reachable.push_back(Edge(r, c, depth, tiles[r][c]));
break;
}
int jump = tiles[r][c];
tiles[r][c] = 0;
if(r + jump < height && tiles[r+jump][c] != 0){
openList.push_back(Edge(r+jump, c, depth+1, jump));
}
if(c + jump < width && tiles[r][c+jump] != 0){
openList.push_back(Edge(r, c+jump, depth+1, jump));
}
if(r - jump > -1 && tiles[r-jump][c] != 0){
openList.push_back(Edge(r-jump, c, depth+1, jump));
}
if(c - jump > -1 && tiles[r][c-jump] != 0){
openList.push_back(Edge(r, c-jump, depth+1, jump));
}
}
while(openList.size() > 0);
}
}
}
return reachable;
}
vector<Edge> Grid::reachingBFS(){
vector<Edge> reaching;
vector< vector<int> > origTiles;
origTiles.resize(height, vector<int>(width, 0));
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
origTiles[i][j] = tiles[i][j];
for(int row = 0; row < height; row++){
for(int col = 0; col < width; col++){
if(row != height-1 || col != width-1){
vector<Edge> openList, closedList;
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
tiles[i][j] = origTiles[i][j];
openList.push_back(Edge(row, col));
do{
closedList.push_back(openList[0]);
openList.erase(openList.begin());
int r = closedList.back().r;
int c = closedList.back().c;
int depth = closedList.back().depth;
if(r == height-1 && c == width-1){
reaching.push_back(Edge(row, col));
break;
}
int jump = tiles[r][c];
tiles[r][c] = 0;
if(r + jump < height && tiles[r+jump][c] != 0){
openList.push_back(Edge(r+jump, c, depth+1, jump));
}
if(c + jump < width && tiles[r][c+jump] != 0){
openList.push_back(Edge(r, c+jump, depth+1, jump));
}
if(r - jump > -1 && tiles[r-jump][c] != 0){
openList.push_back(Edge(r-jump, c, depth+1, jump));
}
if(c - jump > -1 && tiles[r][c-jump] != 0){
openList.push_back(Edge(r, c-jump, depth+1, jump));
}
if(r + jump == height - 1 && c == width-1){
openList.push_back(Edge(r+jump, c, depth+1, jump));
}
if(c + jump == width - 1 && r == height-1){
openList.push_back(Edge(r, c+jump, depth+1, jump));
}
}
while(openList.size() > 0);
}
}
}
return reaching;
}
int Grid::BFS(){
vector<Edge> openList, closedList;
openList.push_back(Edge(0, 0));
do{
closedList.push_back(openList[0]);
openList.erase(openList.begin());
int r = closedList.back().r;
int c = closedList.back().c;
int depth = closedList.back().depth;
if(r == height-1 && c == width-1){
return depth;
}
int jump = tiles[r][c];
//cout << "R="<<r<<" C="<<c<<" val="<<jump<<" depth="<<depth<<endl;
tiles[r][c] = 0;
if(r + jump < height && tiles[r+jump][c] != 0){
openList.push_back(Edge(r+jump, c, depth+1, jump));
}
if(c + jump < width && tiles[r][c+jump] != 0){
openList.push_back(Edge(r, c+jump, depth+1, jump));
}
if(r - jump > -1 && tiles[r-jump][c] != 0){
openList.push_back(Edge(r-jump, c, depth+1, jump));
}
if(c - jump > -1 && tiles[r][c-jump] != 0){
openList.push_back(Edge(r, c-jump, depth+1, jump));
}
if(r + jump == height - 1 && c == width-1){
openList.push_back(Edge(r+jump, c, depth+1, jump));
}
if(c + jump == width - 1 && r == height-1){
openList.push_back(Edge(r, c+jump, depth+1, jump));
}
}
while(openList.size() > 0);
return 0;
}
class Puzzle {
int nRows, nCols, minVal, maxVal;
int solLength, nBHoles, nWHoles, nFFM, nFBM, score;
bool unique;
int **puzzle;
public:
Puzzle(int, int, int, int);
Puzzle& operator=(Puzzle&);
void createPuzzle();
void print();
int solveAndScore();
int getScore();
void setValue(int, int, int);
int getValue(int, int);
};
Puzzle::Puzzle (int rows, int cols, int min, int max){
nRows = rows;
nCols = cols;
minVal = min;
maxVal = max;
puzzle = new int *[nRows];
for(int i = 0; i < nRows; i++)
puzzle[i] = new int[nCols];
solLength = 0;
nBHoles = 0;
nWHoles = 0;
nFFM = 0;
nFBM = 0;
score = 0;
unique = false;
}
Puzzle& Puzzle::operator=(Puzzle& other){
nRows = other.nRows;
nCols = other.nCols;
minVal = other.minVal;
maxVal = other.maxVal;
puzzle = new int *[nRows];
for(int i = 0; i < nRows; i++)
puzzle[i] = new int[nCols];
for(int r = 0; r < nRows; r++){
for(int c = 0; c < nCols; c++){
puzzle[r][c] = other.puzzle[r][c];
}
}
//puzzle = other.puzzle;
solLength = other.solLength;
nBHoles = other.nBHoles;
nWHoles = other.nWHoles;
nFFM = other.nFFM;
nFBM = other.nFBM;
score = other.score;
unique = other.unique;
return *this;
}
void Puzzle::createPuzzle(){
for(int r = 0; r < nRows; r++){
for(int c = 0; c < nCols; c++){
if(r == nRows-1 && c == nCols-1)
puzzle[r][c] = 0;
else
puzzle[r][c] = rand() % (maxVal-minVal+1) + minVal;
}
}
}
void Puzzle::print(){
cout << "Puzzle:" << endl;
for(int r = 0; r < nRows; r++){
for(int c = 0; c < nCols; c++){
cout << puzzle[r][c] << " ";
}
cout << endl;
}
cout << endl;
cout << "Solution: ";
if(solLength == 0)
cout << "No" << endl;
else
cout << "Yes" << endl;
cout << "Unique: ";
if(unique)
cout << "Yes" << endl;
else
cout << "No" << endl;
cout << "Solution length: " << solLength << endl;
cout << "# of black holes: " << nBHoles << endl;
cout << "# of white holes: " << nWHoles << endl;
cout << "# of forced forward moves: " << nFFM << endl;
cout << "# of forced backward moves: " << nFBM << endl;
cout << "Puzzle score: " << score << endl << endl;
}
int Puzzle::solveAndScore(){
Grid g(nRows, nCols, puzzle);
Grid gReachable(nRows, nCols, puzzle);
Grid gReaching(nRows, nCols, puzzle);
vector<Edge> reachable, reaching;
nFFM = 0;
nFBM = 0;
nBHoles = 0;
nWHoles = 0;
solLength = g.BFS();
reachable = gReachable.reachableBFS();
reaching = gReaching.reachingBFS();
if(solLength>0){
unique = true;
for(int x = 0; x < reachable.size(); x++){
vector<Edge> parents;
for(int y = 0; y < reachable.size(); y++){
if(x!=y){
if(reachable[y].r + reachable[y].jump == reachable[x].r)
if(reachable[y].c == reachable[x].c)
parents.push_back(reachable[y]);
else if(reachable[y].r - reachable[y].jump == reachable[x].r)
if(reachable[y].c == reachable[x].c)
parents.push_back(reachable[y]);
else if(reachable[y].c + reachable[y].jump == reachable[x].c)
if(reachable[y].r == reachable[x].r)
parents.push_back(reachable[y]);
else if(reachable[y].c - reachable[y].jump == reachable[x].c)
if(reachable[y].r == reachable[x].r)
parents.push_back(reachable[y]);
}
}
if(parents.size()>0){
//int parDepth = parents[0].depth;
int parDepth = reachable[x].depth-1;
int parCount = 0;
for(int z = 0; z < parents.size(); z++){
//cout << "Child r="<<reachable[x].r<<" c="<<reachable[x].c<<" jump="<<reachable[x].jump<<endl;
//cout << "Parent r="<<parents[z].r<<" c="<<parents[z].c<<" jump="<<parents[z].jump<<endl;
if(parents[z].depth == parDepth)
parCount++;
if(parents[z].depth <= parDepth && parCount > 1){
//cout << "Not Unique r="<<parents[z].r<<" c="<<parents[z].c<<" jump="<<parents[z].jump<<endl;
unique = false;
}
}
}
}
}
for(int x = 0; x < reachable.size(); x++){
bool match = false;
for(int y =0; y < reaching.size(); y++){
if(reachable[x].r == reaching[y].r && reachable[x].c == reaching[y].c)
match = true;
}
if(!match){
//cout << "Black Hole r="<<reachable[x].r<<" c="<<reachable[x].c<<endl;
nBHoles++;
}
}
for(int x = 0; x < reaching.size(); x++){
bool match = false;
for(int y =0; y < reachable.size(); y++){
if(reaching[x].r == reachable[y].r && reaching[x].c == reachable[y].c)
match = true;
}
if(!match){
//cout << "White Hole r="<<reaching[x].r<<" c="<<reaching[x].c<<endl;
nWHoles++;
}
}
for(int x = 0; x < reachable.size(); x++){
//cout << "ReachABLE r="<<reachable[x].r<<" c="<<reachable[x].c<<endl;
int nMoves = 0;
if(reachable[x].r+reachable[x].jump<nRows)
nMoves++;
if(reachable[x].c+reachable[x].jump<nCols)
nMoves++;
if(reachable[x].r-reachable[x].jump>-1)
nMoves++;
if(reachable[x].c-reachable[x].jump>-1)
nMoves++;
if(nMoves == 1){
//cout<<"FFM r="<<reachable[x].r<<" c="<<reachable[x].c<<endl;
nFFM++;
}
}
for(int x = 0; x < reaching.size(); x++){
//cout << "ReachING r="<<reaching[x].r<<" c="<<reaching[x].c<<endl;
int nMoves = 0;
for(int i = minVal; i <= maxVal; i++){
if(reaching[x].r-i>-1){
if(puzzle[reaching[x].r-i][reaching[x].c]==i){
//cout << "This cell: r="<<reaching[x].r<<" c="<<reaching[x].c<<endl;
//cout << "Reached by r="<<reaching[x].r-i<<" c="<<reaching[x].c<<" jump="<<puzzle[reaching[x].r-i][reaching[x].c]<<" i="<<i<<endl;
nMoves++;
}
}
if(reaching[x].r+i<nRows){
if(puzzle[reaching[x].r+i][reaching[x].c]==i){
//cout << "This cell r="<<reaching[x].r<<" c="<<reaching[x].c<<endl;
//cout << "Reached by r="<<reaching[x].r+i<<" c="<<reaching[x].c<<" jump="<<puzzle[reaching[x].r+i][reaching[x].c]<<" i="<<i<<endl;
nMoves++;
}
}
if(reaching[x].c-i>-1){
if(puzzle[reaching[x].r][reaching[x].c-i]==i){
//cout << "This cell r="<<reaching[x].r<<" c="<<reaching[x].c<<endl;
//cout << "Reached by r="<<reaching[x].r<<" c="<<reaching[x].c-i<<" jump="<<puzzle[reaching[x].r][reaching[x].c-i]<<" i="<<i<<endl;
nMoves++;
}
}
if(reaching[x].c+i<nCols){
if(puzzle[reaching[x].r][reaching[x].c+i]==i){
//cout << "This cell r="<<reaching[x].r<<" c="<<reaching[x].c<<endl;
//cout << "Reached by r="<<reaching[x].r<<" c="<<reaching[x].c+i<<" jump="<<puzzle[reaching[x].r][reaching[x].c+i]<<" i="<<i<<endl;
nMoves++;
}
}
}
if(nMoves == 1){
//cout<<"FBM r="<<reaching[x].r<<" c="<<reaching[x].c<<endl;
nFBM++;
}
}
score = (5*solLength) - (2*(nBHoles+nWHoles)) - (2*(nFFM+nFBM));
if(unique)
score += (nRows*nCols);
return score;
}
int Puzzle::getScore(){
return score;
}
void Puzzle::setValue(int r, int c, int val){
puzzle[r][c] = val;
}
int Puzzle::getValue(int r, int c){
return puzzle[r][c];
}
void hillClimb(Puzzle& puzz, int nRows, int nCols, int minVal, int maxVal, Timer t){
int bestScore = puzz.solveAndScore();
//cout << "original" << endl;
//puzz.print();
Puzzle tempPuzz (nRows, nCols, minVal, maxVal);
srand( time (NULL) );
while(t.GetElapsedTime()<59.9){
//cout << "temp BEFORE" <<endl;
//tempPuzz.print();
tempPuzz = puzz;
if(t.GetElapsedTime()>14.9 && t.GetElapsedTime()<15.0 ||
t.GetElapsedTime()>29.9 && t.GetElapsedTime()<30.0 ||
t.GetElapsedTime()>44.9 && t.GetElapsedTime()<45.0)
tempPuzz.createPuzzle();
//cout << "temp AFTER" <<endl;
//tempPuzz.print();
int randRow = rand() % nRows;
int randCol = rand() % nCols;
if(randRow != nRows-1 || randCol != nCols-1){
int randVal = minVal;
while(randVal <= maxVal){
if(randVal != puzz.getValue(randRow, randCol)){
tempPuzz.setValue(randRow, randCol, randVal);
int tempScore = tempPuzz.solveAndScore();
//cout << "temp" <<endl;
//tempPuzz.print();
if(tempScore > bestScore){
bestScore = tempScore;
puzz = tempPuzz;
//puzz.setValue(randRow, randCol, randVal);
//cout << "better" <<endl;
//puzz.print();
break;
}
}
randVal++;
}
/*int randVal = rand() % (maxVal-minVal+1) + minVal;//minVal;
vector<int> checkedVals;
checkedVals.push_back(puzz.getValue(randRow, randCol));
while(checkedVals.size() < (maxVal-minVal+1)){
bool alreadyChecked = false;
for(int ch = 0; ch < checkedVals.size(); ch++){
if(randVal == checkedVals[ch])
alreadyChecked = true;
}
if(!alreadyChecked){
tempPuzz.setValue(randRow, randCol, randVal);
int tempScore = tempPuzz.solveAndScore();
//cout << "temp" <<endl;
//tempPuzz.print();
if(tempScore > bestScore){
bestScore = tempScore;
puzz = tempPuzz;
//puzz.setValue(randRow, randCol, randVal);
//cout << "better" <<endl;
//puzz.print();
//break;
}
checkedVals.push_back(randVal);
}
randVal = rand() % (maxVal-minVal+1) + minVal;
cout <<"rand "<<randVal<<endl;
}
checkedVals.clear();
cout << "new rand"<<endl;
*/
}
}
/*
int imax = numeric_limits<int>::max();
double numGrids = pow((maxVal-minVal+1), (nRows*nCols));
cout << "Total number of Grid combinations: " << numGrids << endl;
if(numGrids > imax)
numGrids = imax;
int nGrids = int (numGrids);
int nGridsAttempted = 1;
for(int i = 0; i < nGrids; i++){
if(t.GetElapsedTime() >59.9 && t.GetElapsedTime() < 60){
cout << "Number of Grids attempted: " << nGridsAttempted << endl;
break;
}
tempPuzz.createPuzzle();
int tempScore = tempPuzz.solveAndScore();
//cout << "temp" <<endl;
//tempPuzz.print();
if(tempScore > bestScore){
bestScore = tempScore;
puzz = tempPuzz;
}
if(i == nGrids-1)
i = 0;
nGridsAttempted++;
}*/
}
void GeneratePuzzle(int nRows, int nCols, int minVal, int maxVal, Timer t){
// YOUR CODE HERE
Puzzle puzz (nRows, nCols, minVal, maxVal);
puzz.createPuzzle();
hillClimb(puzz, nRows, nCols, minVal, maxVal, t);
puzz.print();
}
int main(int argc, char **argv)
{
// Process the arguments
if (argc != 5)
{
printf("Please specify the number of rows and columns and the minimum and maximum values for each cell (requires 4 parameters)\n");
exit(0);
}
int nRows = atoi(argv[1]);
int nColumns = atoi(argv[2]);
int minVal = atoi(argv[3]);
int maxVal = atoi(argv[4]);
// Start the timer
Timer t;
t.StartTimer();
// Generate the puzzle
printf("Generating a %dx%d puzzle with values in range [%d-%d]\n", nRows, nColumns, minVal, maxVal);
GeneratePuzzle(nRows, nColumns, minVal, maxVal, t);
// Print the elapsed time
printf("Total time: %.6lf seconds\n", t.GetElapsedTime());
return 0;
}