-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_MazePathScore.cpp
More file actions
654 lines (553 loc) · 15.8 KB
/
3_MazePathScore.cpp
File metadata and controls
654 lines (553 loc) · 15.8 KB
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct PathQueueNode
{
int RowIndex;
int ColIndex;
PathQueueNode* Next;
};
struct StackNode
{
int RowIndex;
int ColIndex;
StackNode* next;
PathQueueNode* SavedPathLastQPointer;
};
void GetMaze(char**, int, int);
bool CheckMaze(char**, int, int);
void PrintMaze(char**, int, int);
void ProcessMaze(char**, int , int);
void PushToStack(int, int, PathQueueNode*);
bool PopFromStack(int &, int &, PathQueueNode* &, char**);
void EnqueuePath(int, int);
void Dequeue(char**, int[], int);
void CopyMaze(char**, char**, int, int);
void DeleteMazeCopy(char**, int);
StackNode* TopPointer = NULL;
PathQueueNode* PathStartQPointer = NULL;
PathQueueNode* PathLastQPointer = NULL;
int main(int argc, char** argv)
{
char** Maze = NULL;
int iMaxRows, iMaxCols;
cout << "Enter the number of rows in the maze." << endl;
cin >> iMaxRows;
cout << "Enter the number of columns in the maze." << endl;
cin >> iMaxCols;
Maze = new char*[iMaxRows];
for (int i = 0; i < iMaxRows; i++)
{
Maze[i] = new char[iMaxCols];
}
GetMaze(Maze, iMaxRows, iMaxCols);
bool IsMazeCorrect = CheckMaze(Maze, iMaxRows, iMaxCols);
while (!IsMazeCorrect)
{
cout << "Maze Entered is Invalid." << endl;
cout << "Please enter it again." << endl;
GetMaze(Maze, iMaxRows, iMaxCols);
IsMazeCorrect = CheckMaze(Maze, iMaxRows, iMaxCols);
}
PrintMaze(Maze, iMaxRows, iMaxCols);
ProcessMaze(Maze, iMaxRows, iMaxCols);
// TODO: Delete Contents of Stack, if any, using TopPointer.
// TODO: Delete Contents of List, using PathStartQPointer.
system("pause");
return 0;
}
void GetMaze(char** Maze, int iMaxRows, int iMaxCols)
{
char ch;
cout << "Enter the contents of the Maze." << endl;
cout << "S indicates the Start of the Maze ." << endl;
cout << "E indicates the End of the Maze." << endl;
cout << "0 indicates the cell you can go on." << endl;
cout << "1 indicates the cell you cannot go on." << endl << endl;
int i = 0;
int j = 0;
while (cin.get(ch))
{
if (ch == EOF)
break;
if ((ch == '\n') || (ch == ' '))
continue;
Maze[i][j] = ch;
j++;
if (j > (iMaxCols-1)) // Max Possible Value for j is (iMaxCols-1)
{
i++;
j = 0;
}
if (i > (iMaxRows-1) ) // Max Possible Value for i is (iMaxRows-1)
break;
}
}
bool CheckMaze(char** Maze, int iMaxRows, int iMaxCols)
{
int StartCount = 0;
int EndCount = 0;
for (int i = 0; i < iMaxRows; i++)
{
for (int j = 0; j < iMaxCols; j++)
{
if (!((Maze[i][j] == '0') || (Maze[i][j] == '1') || (Maze[i][j] == 'S') || (Maze[i][j] == 'E')))
{
cout << "Character entered in the maze is invalid." << endl;
return false;
}
else if (Maze[i][j] == 'S')
{
StartCount++;
}
else if (Maze[i][j] == 'E')
{
EndCount++;
}
}
}
if (StartCount != 1)
{
if (StartCount == 0)
{
cout << "There is no entry point in the maze " << endl;
return false;
}
else
{
cout << "There cannot be more than one entry point in the maze" << endl;
return false;
}
}
if (EndCount != 1)
{
if (EndCount == 0)
{
cout << "There is no exit point in the maze " << endl;
return false;
}
else
{
cout << "There cannot be more than one exit point in the maze" << endl;
return false;
}
}
return true;
}
void PrintMaze(char** Maze, int iMaxRows, int iMaxCols)
{
cout << endl;
for (int i = 0; i < iMaxRows ; i++)
{
for (int j = 0; j < iMaxCols; j++)
{
cout << setw(2) << Maze[i][j] ;
}
cout << endl;
}
}
void ProcessMaze(char** Maze, int iMaxRows, int iMaxCols)
{
int i=-1, j=-1;
for (i = 0; i < iMaxRows; i++)
{
for (j = 0; j < iMaxCols; j++)
{
if (Maze[i][j] == 'S')
{
break;
}
}
if (Maze[i][j] == 'S')
{
break;
}
}
if ((i == -1) || (j == -1))
return;
PathQueueNode* NewQNode = new PathQueueNode;;
NewQNode->RowIndex = i;
NewQNode->ColIndex = j;
NewQNode->Next = NULL;
PathStartQPointer = NewQNode;
PathLastQPointer = NewQNode;
int PathScore[100000];
int PathScoreIndex = 0;
int NoOfExits = 0;
bool bKeepGoing = true;
while (bKeepGoing)
{
if (Maze[i][j] == 'E')
{
NoOfExits++;
cout << endl;
//cout << "Exit found at " << "Row: " << i << " Col: " << j << endl;
//cout << "Path of the maze is along:" << endl;
char** MazeCopy = new char*[iMaxRows];
for (int l = 0; l < iMaxRows; l++)
{
MazeCopy[l] = new char[iMaxCols];
}
CopyMaze(Maze, MazeCopy, iMaxRows, iMaxCols);
PathScore[PathScoreIndex] = 0;
Dequeue(MazeCopy, PathScore, PathScoreIndex); // This will change values in the path to '*'
//cout << endl << "After Reaching Exit: Maze is as follows: " << endl;
//PrintMaze(Maze, iMaxRows, iMaxCols);
//cout << "Row = " << i << " Col = " << j << endl; //Printing indices of Exit.
for (int m = 0; m < iMaxRows; m++)
{
for (int n = 0; n < iMaxCols; n++)
{
cout << setw(2) << MazeCopy[m][n];
}
cout << endl;
}
cout << "Score of the Path = " << PathScore[PathScoreIndex] << endl;
DeleteMazeCopy(MazeCopy, iMaxRows);
PathScoreIndex++;
if (! PopFromStack(i, j, PathLastQPointer, Maze) )
{
break;
}
//cout << endl << "Before Reaching Exit: Maze is as follows: " << endl;
//PrintMaze(Maze, iMaxRows, iMaxCols);
}
// Visiting (i,j)
if ((Maze[i][j] != 'S') && (Maze[i][j] != 'E') && (Maze[i][j] != '1') )
{
// Never change a Pushed Node to Visited Node UNLESS it is popped.
// Actually, we never traverse to a Pushed Node. We will only come there via a Pop. Then its ok to change 'P' to 'V'
// if (Maze[i][j] != 'P') // NOT REQURIED
// Maze[i][j] = 'V'; // SETTING TO V IS VERY IMPORTANT TO AVOID GOING IN CIRCLES, FOR A GIVEN PATH FROM 'S' TO 'E'. These V's should be set to ZERO at start of finding another alternative path.
Maze[i][j] = 'V'; // SETTING TO V IS VERY IMPORTANT TO AVOID GOING IN CIRCLES, FOR A GIVEN PATH FROM 'S' TO 'E'. These V's should be set to ZERO at start of finding another alternative path.
EnqueuePath(i, j);
}
bool bTopOK = false;
bool bBottomOK = false;
bool bLeftOK = false;
bool bRightOK = false;
// BugFix 2:
// Analyzing a Node can result in its Neighbouring Node and Pushed to Stack.
// Then the Pushed Node can be a neighbour of another Node that is being Analyzed.
// This represents an alternative unique path that traverses a pushed node.
// So in order to cover the alternative path, the node has to be pushed again.
// Whenever a node is pushed, the current is also pushed along with it.
// Here, even though the same node means (i, j), is pushed more than once, each push
// will have a different path associated with the pushed node.
// This bug was fixed below by adding the OR clause to include 'P' pushed nodes.
// The below flags are set, which mean, that we can move to a node which is already pushed
// OR the node can be pushed again.
if ( (i >= 1) && ((Maze[i-1][j] == '0') || (Maze[i-1][j] == 'P')) )
{
bTopOK = true;
}
if ( (i < (iMaxRows - 1)) && ((Maze[i+1][j] == '0') || (Maze[i+1][j] == 'P')) )
{
bBottomOK = true;
}
if ( (j >= 1) && ((Maze[i][j-1] == '0') || (Maze[i][j-1] == 'P')) )
{
bLeftOK = true;
}
if ( (j < (iMaxCols - 1)) && ((Maze[i][j+1] == '0') || (Maze[i][j+1] == 'P')) )
{
bRightOK = true;
}
// If neighbouring node is 'E', then it is necessary to analyze the others neighbours & push, before going to E.
// BugFix 1:
// When one of the neighbours is the Exit node, we were not analyzing the other neighbours for pushing to stack.
// This caused several paths to be missed out.
// This was fixed by adding the 3 if statements in each if block below and call to push to stack.
if ((i >= 1) && (Maze[i-1][j] == 'E'))
{
if (bRightOK)
{
Maze[i][j+1] = 'P';
PushToStack(i, j+1, PathLastQPointer);
}
if (bLeftOK)
{
Maze[i][j-1] = 'P';
PushToStack(i, j-1, PathLastQPointer);
}
if (bBottomOK)
{
Maze[i+1][j] = 'P';
PushToStack(i+1, j, PathLastQPointer);
}
i = i-1;
j = j;
continue;
}
else if ((i < (iMaxRows - 1)) && (Maze[i+1][j] == 'E'))
{
if (bRightOK)
{
Maze[i][j+1] = 'P';
PushToStack(i, j+1, PathLastQPointer);
}
if (bLeftOK)
{
Maze[i][j-1] = 'P';
PushToStack(i, j-1, PathLastQPointer);
}
if (bTopOK)
{
Maze[i-1][j] = 'P';
PushToStack(i-1, j, PathLastQPointer);
}
i = i+1;
j = j;
continue;
}
else if ((j >= 1) && (Maze[i][j-1] == 'E'))
{
if (bRightOK)
{
Maze[i][j+1] = 'P';
PushToStack(i, j+1, PathLastQPointer);
}
if (bBottomOK)
{
Maze[i+1][j] = 'P';
PushToStack(i+1, j, PathLastQPointer);
}
if (bTopOK)
{
Maze[i-1][j] = 'P';
PushToStack(i-1, j, PathLastQPointer);
}
i = i;
j = j-1;
continue;
}
else if ((j < (iMaxCols - 1)) && (Maze[i][j+1] == 'E'))
{
if (bLeftOK)
{
Maze[i][j-1] = 'P';
PushToStack(i, j-1, PathLastQPointer);
}
if (bBottomOK)
{
Maze[i+1][j] = 'P';
PushToStack(i+1, j, PathLastQPointer);
}
if (bTopOK)
{
Maze[i-1][j] = 'P';
PushToStack(i-1, j, PathLastQPointer);
}
i = i;
j = j+1;
continue;
}
// The below three variables is set when you Pop from the Stack. The values were originally pushed to the Stack and
// later read into the below three variables.
int newi = -1;
int newj = -1;
PathQueueNode* newPathLastQPointer = NULL;
if (bTopOK)
{
newi = i-1;
newj = j;
if (bRightOK)
{
Maze[i][j+1] = 'P';
PushToStack(i, j+1, PathLastQPointer);
}
if (bLeftOK)
{
Maze[i][j-1] = 'P';
PushToStack(i, j-1, PathLastQPointer);
}
if (bBottomOK)
{
Maze[i+1][j] = 'P';
PushToStack(i+1, j, PathLastQPointer);
}
}
else if (bBottomOK)
{
newi = i+1;
newj = j;
if (bRightOK)
{
Maze[i][j+1] = 'P';
PushToStack(i, j+1, PathLastQPointer);
}
if (bLeftOK)
{
Maze[i][j-1] = 'P';
PushToStack(i, j-1, PathLastQPointer);
}
if (bTopOK)
{
Maze[i-1][j] = 'P';
PushToStack(i-1, j, PathLastQPointer);
}
}
else if (bLeftOK)
{
newi = i;
newj = j-1;
if (bRightOK)
{
Maze[i][j+1] = 'P';
PushToStack(i, j+1, PathLastQPointer);
}
if (bBottomOK)
{
Maze[i+1][j] = 'P';
PushToStack(i+1, j, PathLastQPointer);
}
if (bTopOK)
{
Maze[i-1][j] = 'P';
PushToStack(i-1, j, PathLastQPointer);
}
}
else if (bRightOK)
{
newi = i;
newj = j+1;
if (bLeftOK)
{
Maze[i][j-1] = 'P';
PushToStack(i, j-1, PathLastQPointer);
}
if (bBottomOK)
{
Maze[i+1][j] = 'P';
PushToStack(i+1, j, PathLastQPointer);
}
if (bTopOK)
{
Maze[i-1][j] = 'P';
PushToStack(i-1, j, PathLastQPointer);
}
}
else
{
bool bPopSuccess = PopFromStack(newi, newj, newPathLastQPointer, Maze);
if (!bPopSuccess)
{
if (NoOfExits == 0)
{
cout << "Sorry There is no way out." << endl;
}
break;
}
}
if ( (newi != -1) && (newj != -1) ) // We do not have newPathLastQPointer in other cases, we have it only we do Pop
{
i = newi;
j = newj;
if (newPathLastQPointer != NULL) // Valid only when we do a Pop Operation.
PathLastQPointer = newPathLastQPointer;
}
else
{
cout << "Something went wrong, Impossible State." << endl;
break;
}
}
cout << endl << endl << "No of Paths to Exit = " << NoOfExits << endl;
cout << endl << "At End of Program the Input Maze is as follows: " << endl;
PrintMaze(Maze, iMaxRows, iMaxCols);
cout << endl << endl << "Exit found at " << "Row: " << i << " Col: " << j << endl;
}
void PushToStack(int RowIndex, int ColIndex, PathQueueNode* PathLastQPointer)
{
StackNode* NewNode = new StackNode;
NewNode->RowIndex = RowIndex;
NewNode->ColIndex = ColIndex;
NewNode->SavedPathLastQPointer = PathLastQPointer;
NewNode->next = NULL;
if (TopPointer == NULL)
TopPointer = NewNode;
else
NewNode->next = TopPointer;
TopPointer = NewNode;
}
bool PopFromStack(int &newi, int &newj, PathQueueNode* &newPathLastQPointer, char** Maze)
{
if (TopPointer == NULL)
return false;
newi = TopPointer->RowIndex;
newj = TopPointer->ColIndex;
newPathLastQPointer = TopPointer->SavedPathLastQPointer;
// Delete PathQueue Nodes from SavedLastQPointer to NULL. Delete the nodes in the Previous Path that are NOT a part of the Popped Node's Saved Path.
// The Deleted Path Nodes can be a dead end OR a previous path to success, given we are finding multiple paths to E (from S via Popped Node).
PathQueueNode* temp = NULL;
if ( (TopPointer->SavedPathLastQPointer != NULL) && (TopPointer->SavedPathLastQPointer->Next != NULL) )
temp = TopPointer->SavedPathLastQPointer->Next; // This is the starting node of the Previous Path we are discarding / deleting.
while (temp != NULL)
{
PathQueueNode* saveTemp = temp;
if (Maze[temp->RowIndex][temp->ColIndex] == 'V') // Change the Discarded or Not Required Segment of the nextious Successfull Path to '0'.
Maze[temp->RowIndex][temp->ColIndex] = '0';
temp = temp->Next;
delete saveTemp;
}
// Deleting the Old Top.
StackNode* tempTop = TopPointer;
TopPointer = TopPointer->next;
delete tempTop;
return true;
}
void EnqueuePath(int i, int j)
{
PathQueueNode* NewNode = new PathQueueNode;
NewNode->RowIndex = i;
NewNode->ColIndex = j;
NewNode->Next = NULL;
if (PathLastQPointer == NULL)
PathLastQPointer = NewNode;
else
PathLastQPointer->Next = NewNode;
PathLastQPointer = NewNode;
if (PathStartQPointer == NULL)
PathStartQPointer = NewNode;
}
void Dequeue(char** Maze, int PathScore[], int PathScoreIndex)
{
if (PathStartQPointer == NULL)
return;
PathScore[PathScoreIndex] = 1; // Includes taking the last step to 'E'. PathScore gives the number of steps taken from 'S' to 'E'.
PathQueueNode* tempPathStartQPointer = PathStartQPointer;
while (tempPathStartQPointer != NULL)
{
PathQueueNode* Temp = tempPathStartQPointer;
//cout << "Row = " << Temp->RowIndex << " Col = " << Temp->ColIndex << endl;
if ( (Maze[Temp->RowIndex][Temp->ColIndex] != 'S') && (Maze[Temp->RowIndex][Temp->ColIndex] != 'E') )
{
Maze[Temp->RowIndex][Temp->ColIndex] = '*';
PathScore[PathScoreIndex]++;
}
tempPathStartQPointer = tempPathStartQPointer->Next;
// We do not want to delete the Nodes, as this will be a shared path segment for another route.
// Note, the unwanted nodes (the ones that form a dead end or part of a successfull path) are deleted in the POP function.
//delete Temp;
}
}
void CopyMaze(char** Maze, char** MazeCopy, int iMaxRows, int iMaxCols)
{
for (int i = 0; i < iMaxRows; i++)
{
for (int j = 0; j < iMaxCols; j++)
{
MazeCopy[i][j] = Maze[i][j];
}
}
}
void DeleteMazeCopy(char** MazeCopy, int iMaxRows)
{
for (int i = 0; i < iMaxRows; i++)
{
if (MazeCopy[i] != NULL)
delete[] MazeCopy[i];
}
delete[] MazeCopy;
}