forked from Dwayne-Phillips/CIPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd.c
executable file
·497 lines (388 loc) · 12.2 KB
/
d.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
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
/*
d.c
Dwayne Phillips
This program illustrates the depth first
search algorithm.
April 1999
*/
#include <stdio.h>
#include <stdlib.h>
/** #include <malloc.h> **/
#undef DEBUG
#define COUNT 2
#define TABLE -1
#define MAX_DEPTH 99
/************************************************/
struct node {
int number;
int blocks[COUNT+1];
int depth;
struct node *parent;
struct node *next;
};
/************************************************/
struct node * remove_from_list(struct node **);
struct node * create_new_node(int, int [], int);
struct node * move_block(struct node *,
int , int , int );
void add_to_list(struct node **, struct node **);
void print_node(struct node *);
void free_nodes_in_list(struct node *);
void traverse_list(struct node *);
int is_empty(struct node *);
int is_a_goal_node(struct node *, int []);
int no_solution_exists(int *);
int solution_found(struct node *, int *);
int is_uncovered(struct node *, int );
int is_the_same(struct node *, int []);
int state_doesnt_exist(struct node *,
struct node *,
struct node *,
int, int);
/************************************************/
int main(int argc, char *argv[])
{
char response[80];
int stupid = 99;
int b[COUNT+1],
goal[COUNT+1],
i,
j,
node_number = 0,
searching = 1;
struct node *CLOSED,
*n,
*new,
*OPEN;
/******************************************
*
* Initialize this problem.
*
* 0
* 1 2
*
******************************************/
OPEN = NULL;
CLOSED = NULL;
b[0] = 1;
b[1] = TABLE;
b[2] = TABLE;
n = create_new_node(node_number, b, 0);
node_number++;
add_to_list(&OPEN, &n);
/******************************************
*
* Make up a goal
*
* 0
* 1
* 2
*
******************************************/
goal[0] = 1;
goal[1] = 2;
goal[2] = TABLE;
/******************************************
*
* Search through the state space for
* a solution.
*
******************************************/
while(searching){
if(is_empty(OPEN))
no_solution_exists(&searching);
else{ /* OPEN list is not empty */
/************************************
*
* Take the first node on OPEN list
* and put it on the CLOSED list.
*
* Try to expand the node. Three
* rules for expanding (moving a
* block to another place).
*
* 1. The block must be uncovered,
* i.e. no blocks sitting on it.
* 2. Cannot put block a on block b
* is b has something on it, i.e.
* block b must be uncovered.
* 3. Cannot repeat a state, i.e.
* the new state cannot be in
* the OPEN or CLOSED list.
*
************************************/
n = remove_from_list(&OPEN);
add_to_list(&CLOSED, &n);
if(n->depth < MAX_DEPTH){
/*********************************
*
* Expand node n.
* Look at each block in node n.
*
*********************************/
for(i=0; i<=COUNT; i++){
/* RULE 1 */ if(is_uncovered(n, i)){
/* Special case
Try to move block i
to the TABLE */
if(n->blocks[i] != TABLE){
/* RULE 3 */ if(state_doesnt_exist(OPEN,
CLOSED,
n, i,
TABLE)){
new = move_block(n,
node_number,
i, TABLE);
if(is_a_goal_node(new,
goal)){
solution_found(new,
&searching);
}
node_number++;
add_to_list(&OPEN, &new);
} /* ends state_doesnt_exist */
} /* ends if TABLE */
/* Try to move block i
onto block j */
for(j=0; j<=COUNT; j++){
if(i != j){
/* RULE 2 */ if(is_uncovered(n, j)){
/* RULE 3 */ if(state_doesnt_exist(
OPEN,
CLOSED,
n, i, j)){
new = move_block(n,
node_number,
i, j);
if(is_a_goal_node(new,
goal)){
solution_found(new,
&searching);
}
node_number++;
add_to_list(&OPEN,
&new);
} /* statedoesntexist */
} /* ends if j is uncovered */
} /* ends if i != j */
} /* ends loop over j */
} /* ends if is_uncovered */
} /* ends loop over i */
} /* ends if depth < MAX_DEPTH */
} /* ends else OPEN is not empty */
} /* ends while searching */
free_nodes_in_list(OPEN);
free_nodes_in_list(CLOSED);
} /* ends main */
/************************************************/
/*
Add a node to the start of the linked list
pointed to be the head pointer.
*/
void add_to_list(struct node **head,
struct node **new)
{
struct node *temp;
if((*head) == NULL){
(*head) = (*new);
(*new)->next = NULL;
} /* ends if head is NULL */
else{
temp = (*head);
(*head) = (*new);
(*new)->next = temp;
} /* ends else */
} /* ends add_to_list */
/************************************************/
void free_nodes_in_list(struct node *head)
{
struct node *temp;
printf("\n\nFreeing nodes in a list");
while(head != NULL){
temp = head;
print_node(temp);
head = temp->next;
free(temp);
} /* ends while */
} /* ends free_nodes_in_list */
/************************************************/
int is_empty(struct node *head)
{
int result = 0;
if(head == NULL)
result = 1;
return(result);
} /* ends is_empty */
/************************************************/
int no_solution_exists(int *searching)
{
*searching = 0;
printf("\nThe OPEN list is empty, "
"no solution exists");
return(1);
} /* ends no_solution_exists */
/************************************************/
void print_node(struct node *a)
{
int i;
printf("\nNode number=%d depth=%d",
a->number, a->depth);
printf("\nWhat is under the %d blocks\n", COUNT+1);
for(i=0; i<=COUNT; i++)
printf("%4d", a->blocks[i]);
} /* ends print_node */
/************************************************/
/*
Remove the node from the start of the list
pointed to by head.
*/
struct node * remove_from_list(struct node **head)
{
struct node *result;
if(*head == NULL){
printf("\nEMPTY LIST CANNOT REMOVE ");
exit(1);
} /* ends if NULL */
result = (*head);
(*head) = (*head)->next ;
return(result);
} /* ends remove_from_list */
/************************************************/
int is_uncovered(struct node *n, int block)
{
int i, result = 1;
for(i=0; i<=COUNT; i++){
if(n->blocks[i] == block)
result = 0;
}
return(result);
} /* ends is_uncovered */
/************************************************/
/*
If we put block on place, does that state
already exist in the list pointed to by open
or the list pointed to by closed. Return 1
if neither list contains that state.
*/
int state_doesnt_exist(struct node *open,
struct node *closed,
struct node *n,
int block,
int place)
{
int i,
tempb[COUNT+1],
result=1;
struct node *tempp;
for(i=0; i<=COUNT; i++)
tempb[i] = n->blocks[i];
tempb[block] = place;
tempp = open;
while(tempp != NULL){
if(is_the_same(tempp, tempb))
result = 0;
tempp = tempp->next;
}
tempp = closed;
while(tempp != NULL){
if(is_the_same(tempp, tempb))
result = 0;
tempp = tempp->next;
}
return(result);
} /* ends state_doesnt_exist */
/************************************************/
int is_the_same(struct node *n, int b[])
{
int i, result=1;
for(i=0; i<=COUNT; i++){
if(n->blocks[i] != b[i])
result = 0;
}
return(result);
} /* ends is_the_same */
/************************************************/
/************************************************/
/************************************************/
/************************************************/
/************************************************/
int is_a_goal_node(struct node *n, int g[])
{
int i,
result = 1;
for(i=0; i<=COUNT; i++){
if(n->blocks[i] != g[i])
result = 0;
}
return(result);
} /* ends is_a_goal_node */
/************************************************/
int solution_found(struct node *end,
int *searching)
{
struct node *temp;
temp = end;
*searching = 0;
printf("\n\n\nHere is the solution");
while(temp != NULL){
print_node(temp);
temp = temp->parent;
} /* ends while */
return(1);
} /* ends solution_found */
/************************************************/
/************************************************/
/************************************************/
/************************************************/
/*
This function creates a new node. The new
node has the same block positions as node n,
with the change that block is on place.
The parent of the new node is n and the depth
of the new node is n's depth plus 1.
*/
struct node * move_block(struct node *n,
int number,
int block,
int place)
{
int i, b[COUNT+1];
struct node *result;
for(i=0; i<=COUNT; i++)
b[i] = n->blocks[i];
b[block] = place;
result = create_new_node(number, b, n->depth+1);
result->parent = n;
return(result);
} /* ends move_block */
/************************************************/
struct node * create_new_node(int number,
int b[],
int depth)
{
int i;
struct node *new;
new = (struct node *)
calloc(1, sizeof(struct node));
new->number = number;
new->depth = depth;
new->next = NULL;
new->parent = NULL;
for(i=0; i<=COUNT; i++)
new->blocks[i] = b[i];
return(new);
} /* ends create_new_node */
/************************************************/
/************************************************/
/************************************************/
void traverse_list(struct node *head)
{
struct node *temp;
printf("\n\nTraversing list");
while(head != NULL){
temp = head;
print_node(temp);
head = temp->next;
} /* ends while */
printf("\n\n");
} /* ends free_nodes_in_list */