-
Notifications
You must be signed in to change notification settings - Fork 0
/
frogHop.c
268 lines (252 loc) · 6 KB
/
frogHop.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
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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int startX, startY;
typedef struct PondStruct {
int **grid;
int length;
int maxIndex;
} Pond;
typedef struct PadSetStruct {
struct PadNodeStruct *first;
struct PadNodeStruct *last;
} PadSet;
typedef struct PadNodeStruct{
int x;
int y;
struct PadNodeStruct *next;
} PadNode;
Pond* initializePond(int size);
void print_pond(Pond *P);
void find_path(Pond *P, int x, int y, int i, int p);
PadSet* initializePadSet();
PadNode* initializePadNode();
void print_set(PadSet *pSet);
void addNode(PadSet *P, PadNode *N);
PadSet *padSetPopulatedWithNextPads(Pond *P, int x, int y);
void addNewNode(PadSet *P, int x, int y);
PadNode* removeNode(PadSet *P);
void freePadSet(PadSet *P) ;
int main( int argc, char *argv[] )
{
if(argv[1]==NULL || argv[2]==NULL || argv[3]==NULL) {
printf("Invalid arguments... exiting.\n");
return 1;
}
int size = atoi(argv[1]);
int xPos = atoi(argv[2]);
int yPos = atoi(argv[3]);
printf("The grid will be %dX%d\n",size, size);
printf("The starting position is: %d, %d\n", xPos, yPos);
Pond *pond;
printf("%p => ", pond);
pond=initializePond(size);
printf("%p\n", pond);
print_pond(pond);
find_path(pond, xPos, yPos, 1, 0);
printf("No path found.");
return 1;
}
Pond* initializePond(int size)
{
int i,j;
Pond *P=malloc(sizeof(Pond));
P->length=size;
P->maxIndex=size-1;
P->grid=calloc(size, sizeof *P->grid);
for(i=0; i<P->length; i++) {
P->grid[i]=calloc(size, sizeof(int));
}
return P;
}
void print_pond(Pond *P)
{
int i,j;
for(i=0; i<P->length; i++) {
for(j=0; j<P->length; j++) {
printf("%2d ", P->grid[i][j]);
}
putchar('\n');
}
}
void find_path(Pond *P, int x, int y, int c, int p)
{
/*
find_path ( Pond, x, y, i , prevKind)
Pond[x][y] = i //the ith pad along the path
if i==SIZE*SIZE //reached the end of the path
print_pond(Pond)
exit
else
let nextValidPadSet be the set of next valid pads
for nextPad in nextValidPadSet do
let [Px][Py] be nextPad's coordinates
find_path ( Pond, Px, Py, i+1, thisKind )
Pond[x][y] = 0 //backtrack
*/
P->grid[x][y] = c;
if(c == P->length*P->length) {
//print_pond(P);
printf("Path found.\n");
print_pond(P);
exit(0);
} else {
PadSet *nextValidPadSet = padSetPopulatedWithNextPads(P, x, y);
PadNode *tempPadNode = nextValidPadSet->first;
while(tempPadNode!=NULL) {
find_path(P, tempPadNode->x, tempPadNode->y, c+1, c);
tempPadNode=tempPadNode->next;
}
freePadSet(nextValidPadSet);
P->grid[x][y]=0;
}
}
PadSet* initializePadSet()
{
PadSet *returnPadSet;
returnPadSet = malloc(sizeof(PadSet));
returnPadSet->first=NULL;
return returnPadSet;
}
PadNode* initializePadNode()
{
PadNode *returnPadNode;
returnPadNode = malloc(sizeof(PadNode));
returnPadNode->x=-1;
returnPadNode->y=-1;
returnPadNode->next=NULL;
return returnPadNode;
}
PadSet *padSetPopulatedWithNextPads(Pond *P, int x, int y)
{
int tempX, tempY;
PadNode *temp;
PadSet *returnPadSet;
returnPadSet = initializePadSet();
//1 o'clock
if(x>=2 && y <= P->maxIndex-1) {
tempX=x-2; tempY= y+1;
if(P->grid[tempX][tempY]!=0) {
} else {
//printf("1\n");
addNewNode(returnPadSet, tempX, tempY);
}
}
//2 o'clock
if(x>=1 && y<=P->maxIndex-2) {
tempX=x-1; tempY=y+2;
if(P->grid[tempX][tempY]!=0) {
} else {
//printf("2\n");
addNewNode(returnPadSet, tempX, tempY);
}
}
//4 o'clock
if(x<=P->maxIndex-1 && y<=P->maxIndex-2) {
//printf("%d, %d\n", x, y);
tempX=x+1; tempY=y+2;
if(P->grid[tempX][tempY]!=0) {
} else {
//printf("4 o'clock added\n");
addNewNode(returnPadSet, tempX, tempY);
}
}
//5 o'clock
if(x<=P->maxIndex-2 && y<=P->maxIndex-1) {
tempX=x+2; tempY=y+1;
if(P->grid[tempX][tempY]!=0) {
} else {
//printf("5 o'clock added\n");
addNewNode(returnPadSet, tempX, tempY);
}
}
//7 o'clock
if(x<=P->maxIndex-2 && y>=1) {
tempX=x+2; tempY=y-1;
if(P->grid[tempX][tempY]!=0) {
} else {
//printf("7\n");
addNewNode(returnPadSet, tempX, tempY);
}
}
//8 o'clock
if(x<=P->maxIndex-1 && y>=2) {
tempX=x+1; tempY=y-2;
if(P->grid[tempX][tempY]!=0) {
} else {
//printf("8\n");
addNewNode(returnPadSet, tempX, tempY);
}
}
//10 o'clock
if(x>=1 && y>=2) {
tempX=x-1; tempY=y-2;
if(P->grid[tempX][tempY]!=0) {
} else {
//printf("10\n");
addNewNode(returnPadSet, tempX, tempY);
}
}
//11 o'clock
if(x>=2 && y >= 1) {
tempX=x-2; tempY=y-1;
if(P->grid[tempX][tempY]!=0) {
} else {
//printf("11\n");
addNewNode(returnPadSet, tempX, tempY);
}
}
//print_set(returnPadSet);
//sleep(1);
return returnPadSet;
}
void print_set(PadSet *pSet)
{
PadNode *P = pSet->first;
printf("(%d, %d) ", P->x, P->y);
while(P->next != NULL) {
printf("-> (%d, %d) ", P->x, P->y);
P=P->next;
}
putchar('\n');
}
void addNode(PadSet *P, PadNode *N)
{
if(P->first==NULL) {
P->first=N;
P->last=N;
} else if (P->first==P->last) {
P->first->next=N;
P->last=N;
} else {
P->last->next=N;
P->last=N;
}
//printf("%d, %d added\n", N->x, N->y);
}
void addNewNode(PadSet *P, int x, int y)
{
PadNode *nodeToAdd = initializePadNode();
nodeToAdd->x=x;
nodeToAdd->y=y;
addNode(P, nodeToAdd);
}
PadNode* removeNode(PadSet *P)
{
PadNode *temp = P->first;
P->first=P->first->next;
free(temp);
return temp;
}
void freePadSet(PadSet *P)
{
PadNode *pNode;
PadNode *nodeToFree;
pNode=P->first;
while(pNode!=NULL) {
nodeToFree=pNode;
pNode=pNode->next;
free(nodeToFree);
}
free(P);
}