-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind_Max_Path.c
89 lines (78 loc) · 2.41 KB
/
Find_Max_Path.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
#include "Knight_Tour.h"
/******************* Function prototypes *******************/
static Move* calcMoveCell(boardPos node1, boardPos node2);
movesList* findPathCoveringAllBoard(boardPos start, movesArray** moves, char** board);
static bool findPathCoveringAllBoardAux(treeNode* node, movesList* list, int max);
void convertToMove(int* row, int* col, boardPos pos);
int countEmptyCells(char** board);
/*********************************************************/
/******************* Function Implementation *******************/
movesList* findPathCoveringAllBoard(boardPos start, movesArray** moves, char** board)
{
pathTree tr;
movesList list;
bool res;
makeEmptyList(&list);
int max = countEmptyCells(board);
tr = findAllPossiblePaths(start, moves, board);
res = findPathCoveringAllBoardAux(tr.head, &list, 5);
freeTree(&tr);
if (res)
return &list;
return NULL;
}
static bool findPathCoveringAllBoardAux(treeNode* node, movesList* list, int max)
{
moveCell* newCell = NULL;
int row, col;
row = col = 0;
bool res;
Move* move;
if (node->next_possible_positions == NULL && max == 0)
return true;
else if (node->next_possible_positions == NULL && max != 0)
return false;
while (node->next_possible_positions != NULL) {
res = findPathCoveringAllBoardAux(node->next_possible_positions->node, list, max - 1);
if (res == true) {
move = calcMoveCell(&newCell, node->position, node->next_possible_positions->node);
addToBeginningDoublyList(list, createNewCell(*move, NULL, NULL));
return true;
}
node->next_possible_positions = node->next_possible_positions->next;
}
return false;
}
static Move* calcMoveCell(boardPos node1, boardPos node2)
{
Move* move;
int row1, row2, col1, col2, row3, col3;
convertToMove(&row1, &col1, node1);
convertToMove(&row2, &col2, node2);
row3 = (-1) * row1 + row2 - 65;
col3 = (-1) * col1 + col2;
move = (Move*)malloc(INIT_SIZE * sizeof(Move));
CheckMemoryAllocation(move);
move->rows = row3;
move->cols = col3;
return move;
}
void convertToMove(int* row, int* col, boardPos pos)
{
*row = pos[0];
if ((pos[1] % COLS) != 0)
*col = (pos[1] % COLS) - 1;
else
*col = COLS - 1;
}
int countEmptyCells(char** board)
{
int j, i, count = 0;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (isEmptyCell(getCell(i + 1, j + 1, board)))
count++;
}
}
return count;
}