View difference between Paste ID: rJanh33P and D08NDXC5
SHOW: | | - or go back to the newest paste.
1
/******
2
MAIN3.C
3
******/
4
5
#include <stdio.h>
6
#include <stdlib.h>
7
8
#define C1 1		/* C1   E1   C2		*/
9
#define C2 2		/*			*/
10
#define C3 3		/* E2   M    E3		*/
11
#define C4 4		/*			*/
12
			/* C3   E4   C4		*/
13
#define E1 5
14
#define E2 6
15
#define E3 7
16
#define E4 8
17
18
#define  M 9
19
//-----------
20
21
22
#define DEPTH 8
23
24
25
typedef struct path
26
{
27
	char path[DEPTH+1];
28
	char taken[10];
29
	int index;
30
31
}t_path;
32
33
34
35
/* PROTOTYPES */
36
void drawpattern(t_path*);
37
void pathfinder(t_path* current_path, int depth);
38
void addToPath(t_path* p, char toAdd);
39
void moveCursor(int, int);
40
void drawTakenMap(t_path* p);
41
void updateTakenMap(t_path* p);
42
43
44
//------- NEEEWWWWWW-------
45
char iHaveBeenThere[10000];
46
47
48
int main(){
49
50
51
	t_path* my_path = (t_path*)malloc(sizeof(t_path));
52
	int i;
53
	for(i=1;i<=9;i++){
54
		my_path->taken[i] = 0;
55
	}
56
	my_path->index = 0;
57
58
59
	my_path->path[0] = C1;
60
	my_path->taken[C1] = 1;
61
62
63
64
	//t_path** pathList = malloc(10000*sizeof(t_path*));
65
66
67
	pathfinder(my_path, 0);
68
69
	drawpattern(my_path);
70
	return 0;
71
}
72
73
74
75
void pathfinder(t_path* current_path, int depth){
76
	int k;
77
78
	/*printf("\n");
79
	for(k=0; k<depth; k++){
80
		printf("  ");
81
	}
82
	printf("Called : depth:%d  | ", depth);
83
	drawpattern(current_path);
84
	for(k=0; k<depth; k++){
85
		printf("  ");
86
	}
87
	drawTakenMap(current_path);*/
88
89
	if(depth==DEPTH){ //Depth Touched !
90
		//printf("---------\nDepth touched\n");	
91
		drawpattern(current_path);
92
		//drawTakenMap(current_path);
93
		current_path->index--;
94
		current_path->taken[current_path->path[current_path->index]]=0;
95
				
96
		return;
97
	}
98
99
100
	//printf("called index:%d\n", current_path->index);
101
102
	int i = 0;
103
	int check = 0;
104
105
	char init = current_path->path[current_path->index];//The init part is the last element of the path
106
107
	if(init==C1){
108
109
			if(current_path->taken[E1]==0) //If E1 is not taken
110
			{
111
				addToPath(current_path, E1);
112
				pathfinder(current_path, depth+1);
113
114
			}
115
			if (current_path->taken[E2]==0) //	E1 is taken so we check if E2 is taken
116
			{
117
				addToPath(current_path, E2);
118
				pathfinder(current_path, depth+1);
119
120
			}
121
			if (current_path->taken[E3]==0) //	E2 is taken so we check if E3 is taken
122
			{
123
				addToPath(current_path, E3);
124
				pathfinder(current_path, depth+1);
125
126
			}
127
			if (current_path->taken[E4]==0) //	E2 is taken so we check if E3 is taken
128
			{
129
				addToPath(current_path, E4);
130
				pathfinder(current_path, depth+1);
131
132
			}
133
			if(current_path->taken[M]==0)
134
			{
135
				addToPath(current_path, M);
136
				pathfinder(current_path, depth+1);
137
138
			}
139
			if(current_path->taken[E1]==1 && current_path->taken[C2]==0) //Jump condition
140
			{
141
				addToPath(current_path, C2);
142
				pathfinder(current_path, depth+1);
143
144
			}
145
			if(current_path->taken[E2]==1 && current_path->taken[C3]==0) //Jump condition
146
			{
147
				addToPath(current_path, C3);
148
				pathfinder(current_path, depth+1);
149
150
			}
151
			if(current_path->taken[M]==1 && current_path->taken[C4]==0) //Jump condition M->C4
152
			{
153
				addToPath(current_path, C4);
154
				pathfinder(current_path, depth+1);
155
156
			}
157
			//	there is no way possible, STOP (In fact we can take into account the jump)
158
			
159
				//printf("No possible from C1\n");
160
				//drawpattern(current_path);
161
162
				current_path->path[current_path->index] = 0;
163
				current_path->index--;
164
				updateTakenMap(current_path);
165
166
167
				return;
168
			
169
		}
170
			
171
		if(init==C2){
172
			
173
			if(current_path->taken[E1]==0) //If E1 is not taken
174
			{
175
				addToPath(current_path, E1);
176
				pathfinder(current_path, depth+1);
177
178
			}
179
			if (current_path->taken[E3]==0) //	E1 is taken so we check if E3 is taken
180
			{
181
				addToPath(current_path, E3);
182
				pathfinder(current_path, depth+1);
183
184
			}
185
			if (current_path->taken[E2]==0) 
186
			{
187
				addToPath(current_path, E2);
188
				pathfinder(current_path, depth+1);
189
190
			}
191
			if (current_path->taken[E4]==0) 
192
			{
193
				addToPath(current_path, E4);
194
				pathfinder(current_path, depth+1);
195
196
			}
197
			if(current_path->taken[M]==0)
198
			{
199
				addToPath(current_path, M);
200
				pathfinder(current_path, depth+1);
201
202
			}
203
			if(current_path->taken[E2]==1 && current_path->taken[C1]==0) //Jump condition
204
			{
205
				addToPath(current_path, C1);
206
				pathfinder(current_path, depth+1);
207
208
			}
209
			if(current_path->taken[E4]==1 && current_path->taken[C4]==0) //Jump condition
210
			{
211
				addToPath(current_path, C4);
212
				pathfinder(current_path, depth+1);
213
214
			}
215
			if(current_path->taken[M]==1 && current_path->taken[C3]==0) //Jump condition M->C3
216
			{
217
				addToPath(current_path, C3);
218
				pathfinder(current_path, depth+1);
219
220
			}
221
			//	there is no way possible, STOP (In fact we can take into account the jump)
222
			
223
224
				//drawpattern(current_path);
225
				//printf("No possible from C2\n");
226
				//drawpattern(current_path);
227
228
				current_path->path[current_path->index] = 0;
229
				current_path->index--;
230
				updateTakenMap(current_path);
231
				return;
232
			
233
234
235
236
			//END OF C2 CASE
237
238
		}
239
		
240
		if(init==C3){
241
242
243
			if(current_path->taken[E2]==0) //If E1 is not taken
244
			{
245
				addToPath(current_path, E2);
246
				pathfinder(current_path, depth+1);
247
248
			}
249
			if (current_path->taken[E4]==0) //	E1 is taken so we check if E3 is taken
250
			{
251
				addToPath(current_path, E4);
252
				pathfinder(current_path, depth+1);
253
254
			}
255
			if (current_path->taken[E3]==0) 
256
			{
257
				addToPath(current_path, E3);
258
				pathfinder(current_path, depth+1);
259
260
			}
261
			if (current_path->taken[E1]==0) 
262
			{
263
				addToPath(current_path, E1);
264
				pathfinder(current_path, depth+1);
265
266
			}
267
			if(current_path->taken[M]==0)
268
			{
269
				addToPath(current_path, M);
270
				pathfinder(current_path, depth+1);
271
272
			}
273
			if(current_path->taken[E1]==1 && current_path->taken[C1]==0) //Jump condition
274
			{
275
				addToPath(current_path, C1);
276
				pathfinder(current_path, depth+1);
277
278
			}
279
			if(current_path->taken[E3]==1 && current_path->taken[C4]==0) //Jump condition
280
			{
281
				addToPath(current_path, C4);
282
				pathfinder(current_path, depth+1);
283
284
			}
285
			if(current_path->taken[M]==1 && current_path->taken[C2]==0) //Jump condition M->C2
286
			{
287
				addToPath(current_path, C2);
288
				pathfinder(current_path, depth+1);
289
290
			}
291
			//	there is no way possible, STOP (In fact we can take into account the jump)
292
293
294
				//printf("No possible from C3\n");
295
				current_path->path[current_path->index] = 0;
296
				current_path->index--;
297
				updateTakenMap(current_path);
298
				return;
299
			
300
			
301
		}
302
		if(init==C4){
303
304
			if(current_path->taken[E3]==0) //Classic 
305
			{
306
				addToPath(current_path, E3);
307
				pathfinder(current_path, depth+1);
308
309
			}
310
			if (current_path->taken[E4]==0) //Classic
311
			{
312
				addToPath(current_path, E4);
313
				pathfinder(current_path, depth+1);
314
315
			}
316
			if (current_path->taken[E1]==0) //Long 
317
			{
318
				addToPath(current_path, E1);
319
				pathfinder(current_path, depth+1);
320
321
			}
322
			if (current_path->taken[E2]==0) //Long 
323
			{
324
				addToPath(current_path, E2);
325
				pathfinder(current_path, depth+1);
326
327
			}
328
			if(current_path->taken[M]==0)
329
			{
330
				addToPath(current_path, M);
331
				pathfinder(current_path, depth+1);
332
333
			}
334
			if(current_path->taken[E1]==1 && current_path->taken[C1]==0) //Jump condition
335
			{
336
				addToPath(current_path, C1);
337
				pathfinder(current_path, depth+1);
338
339
			}
340
			if(current_path->taken[E3]==1 && current_path->taken[C4]==0) //Jump condition
341
			{
342
				addToPath(current_path, C4);
343
				pathfinder(current_path, depth+1);
344
345
			}
346
			if(current_path->taken[M]==1 && current_path->taken[C1]==0) //Jump condition M->C1
347
			{
348
				addToPath(current_path, C1);
349
				pathfinder(current_path, depth+1);
350
351
			}
352
			//	there is no way possible, STOP (In fact we can take into account the jump)
353
354
				//printf("No possible from C4\n");
355
				//drawpattern(current_path);
356
				current_path->path[current_path->index] = 0;
357
				current_path->index--;
358
				updateTakenMap(current_path);
359
				return;
360
			
361
362
363
			
364
			}
365
366
			//----------------------------------------------------
367
			//END OF CORNER CASES
368
			//------------------------------------------------------
369
370
			if(init==E1){
371
				if(current_path->taken[C1]==0) //Corner
372
				{
373
					addToPath(current_path, C1);
374
					pathfinder(current_path, depth+1);
375
376
				}
377
				if(current_path->taken[C2]==0)//Corner
378
				{
379
					addToPath(current_path, C2);
380
					pathfinder(current_path, depth+1);
381
382
				}
383
				if(current_path->taken[E2]==0)//Edge
384
				{
385
					addToPath(current_path, E2);
386
					pathfinder(current_path, depth+1);
387
388
				}
389
				if(current_path->taken[E3]==0)//Edge
390
				{
391
					addToPath(current_path, E3);
392
					pathfinder(current_path, depth+1);
393
394
				}
395
				if(current_path->taken[M]==0)//Middle
396
				{
397
					addToPath(current_path, M);
398
					pathfinder(current_path, depth+1);
399
400
				}
401
				if(current_path->taken[M]==1 && current_path->taken[E4]==0)//Jump over middle
402
				{
403
					addToPath(current_path, E4);
404
					pathfinder(current_path, depth+1);
405
406
				}
407
				//NO PATH
408
409
				//printf("No possible from E1\n");
410
				//drawpattern(current_path);
411
				current_path->path[current_path->index] = 0;
412
				current_path->index--;
413
				updateTakenMap(current_path);
414
				return;
415
				
416
			}
417
418
			if(init==E2){
419
				if(current_path->taken[C1]==0) //Corner
420
				{
421
					addToPath(current_path, C1);
422
					pathfinder(current_path, depth+1);
423
424
				}
425
				if(current_path->taken[C3]==0)//Corner
426
				{
427
					addToPath(current_path, C3);
428
					pathfinder(current_path, depth+1);
429
430
				}
431
				if(current_path->taken[E1]==0)//Edge
432
				{
433
					addToPath(current_path, E1);
434
					pathfinder(current_path, depth+1);
435
436
				}
437
				if(current_path->taken[E4]==0)//Edge
438
				{
439
					addToPath(current_path, E4);
440
					pathfinder(current_path, depth+1);
441
442
				}
443
				if(current_path->taken[M]==0)//Middle
444
				{
445
					addToPath(current_path, M);
446
					pathfinder(current_path, depth+1);
447
448
				}
449
				if(current_path->taken[M]==1 && current_path->taken[E3]==0)//Jump over middle
450
				{
451
					addToPath(current_path, E3);
452
					pathfinder(current_path, depth+1);
453
454
				}
455
				
456
				//NO PATH
457
458
				//printf("No possible from E2\n");
459
				//drawpattern(current_path);
460
				current_path->path[current_path->index] = 0;
461
				current_path->index--;
462
				updateTakenMap(current_path);
463
					
464
			}
465
				
466
			
467
468
469
			if(init==E3){
470
				if(current_path->taken[C2]==0) //Corner
471
				{
472
					addToPath(current_path, C2);
473
					pathfinder(current_path, depth+1);
474
475
				}
476
				if(current_path->taken[C4]==0)//Corner
477
				{
478
					addToPath(current_path, C4);
479
					pathfinder(current_path, depth+1);
480
481
				}
482
				if(current_path->taken[E1]==0)//Edge
483
				{
484
					addToPath(current_path, E1);
485
					pathfinder(current_path, depth+1);
486
487
				}
488
				if(current_path->taken[E4]==0)//Edge
489
				{
490
					addToPath(current_path, E4);
491
					pathfinder(current_path, depth+1);
492
493
				}
494
				if(current_path->taken[M]==0)//Middle
495
				{
496
					addToPath(current_path, M);
497
					pathfinder(current_path, depth+1);
498
499
				}
500
				if(current_path->taken[M]==1 && current_path->taken[E2]==0)//Jump over middle
501
				{
502
					addToPath(current_path, E2);
503
					pathfinder(current_path, depth+1);
504
505
				}
506
				//NO PATH
507
508
				//printf("No possible from E3\n");
509
				//drawpattern(current_path);
510
				current_path->path[current_path->index] = 0;
511
				current_path->index--;
512
				updateTakenMap(current_path);
513
				return;
514
				
515
			}
516
517
			if(init==E4){
518
				if(current_path->taken[C3]==0) //Corner
519
				{
520
					addToPath(current_path, C3);
521
					pathfinder(current_path, depth+1);
522
523
				}
524
				if(current_path->taken[C4]==0)//Corner
525
				{
526
					addToPath(current_path, C4);
527
					pathfinder(current_path, depth+1);
528
529
				}
530
				if(current_path->taken[E2]==0)//Edge
531
				{
532
					addToPath(current_path, E2);
533
					pathfinder(current_path, depth+1);
534
535
				}
536
				if(current_path->taken[E3]==0)//Edge
537
				{
538
					addToPath(current_path, E3);
539
					pathfinder(current_path, depth+1);
540
541
				}
542
				if(current_path->taken[M]==0)//Middle
543
				{
544
					addToPath(current_path, M);
545
					pathfinder(current_path, depth+1);
546
547
				}
548
				if(current_path->taken[M]==1 && current_path->taken[E1]==0)//Jump over middle
549
				{
550
					addToPath(current_path, E1);
551
					pathfinder(current_path, depth+1);
552
553
				}
554
				
555
				//NO PATH
556
				
557
				//printf("No possible from E4\n");
558
				//drawpattern(current_path);
559
				current_path->path[current_path->index] = 0;
560
				current_path->index--;
561
				updateTakenMap(current_path);
562
				return;
563
				
564
			}
565
566
			//----------------------------------------------------
567
			//END OF EDGE CASES
568
			//------------------------------------------------------
569
570
		if(init==M){
571
572
			if(current_path->taken[E1]==0) //If E1 is not taken
573
			{
574
				addToPath(current_path, E1);
575
				pathfinder(current_path, depth+1);
576
577
			}
578
			if (current_path->taken[E2]==0) //	E1 is taken so we check if E2 is taken
579
			{
580
				addToPath(current_path, E2);
581
				pathfinder(current_path, depth+1);
582
583
			}
584
			if (current_path->taken[E3]==0) //	E2 is taken so we check if E3 is taken
585
			{
586
				addToPath(current_path, E3);
587
				pathfinder(current_path, depth+1);
588
589
			}
590
			if (current_path->taken[E4]==0) //	E2 is taken so we check if E3 is taken
591
			{
592
				addToPath(current_path, E4);
593
				pathfinder(current_path, depth+1);
594
595
			}
596
			if(current_path->taken[C1]==0) //If E1 is not taken
597
			{
598
				addToPath(current_path, C1);
599
				pathfinder(current_path, depth+1);
600
601
			}
602
			if (current_path->taken[C2]==0) //	E1 is taken so we check if E2 is taken
603
			{
604
				addToPath(current_path, C2);
605
				pathfinder(current_path, depth+1);
606
607
			}
608
			if (current_path->taken[C3]==0) //	E2 is taken so we check if E3 is taken
609
			{
610
				addToPath(current_path, C3);
611
				pathfinder(current_path, depth+1);
612
613
			}
614
			if (current_path->taken[C4]==0) //	E2 is taken so we check if E3 is taken
615
			{
616
				addToPath(current_path, C4);
617
				pathfinder(current_path, depth+1);
618
619
			}
620
621
622
			//NO PATH
623
624
				//printf("No possible from M\n");
625
				//drawpattern(current_path);
626
				current_path->path[current_path->index] = 0;
627
				current_path->index--;
628
				updateTakenMap(current_path);
629
				return; 
630
			
631
632
		}
633
634
		
635
636
	
637
	//pathfinder(current_path, depth+1);
638
639
}
640
641
642
643
644
void addToPath(t_path* p, char toAdd){
645
646
	if(toAdd<1 || toAdd>9){
647
		printf("Error: addToPath Function cannot take this parameter: toAdd =%d\n", toAdd);
648
		exit(-1);
649
	}
650
	else{
651
652
		
653
		if(p->taken[toAdd]==1){
654
			printf("Bug: not possible to add a taken to path...\n");
655
			exit(-1);
656
		}
657
		else{
658
659
		p->index++; //Add a step to the path
660
		p->path[p->index] = toAdd; //Add toAdd to current path
661
		updateTakenMap(p);
662
	
663
		}
664
665
	}
666
667
}
668
669
670
671
void drawpattern(t_path* p){
672
673
		
674
675
		int i;
676
		for(i=0; i<p->index+1; i++){
677
678
		switch(p->path[i]){
679
			case E1:
680
				printf("->E1");
681
				break;
682
			case E2:
683
				printf("->E2");
684
				break;
685
			case E3:
686
				printf("->E3");
687
				break;
688
			case E4:
689
				printf("->E4");
690
				break;
691
			case C1:
692
				printf("->C1");
693
				break;
694
			case C2:
695
				printf("->C2");
696
				break;
697
			case C3:
698
				printf("->C3");
699
				break;
700
			case C4:
701
				printf("->C4");
702
				break;
703
			case M:
704
				printf("->M");
705
				break;
706
			default :
707
				printf("default");
708
709
		}
710
	}
711
	printf("\n");
712
}
713
714
715
void moveCursor(int row, int col){
716
	printf("\033[2J");
717
	printf("\033[%d;%dH", row, col);
718
	printf("Coucou\n");
719
}
720
721
722
723
t_path* copyTPath(t_path* toCopy){
724
725
726
	t_path* toReturn = NULL;
727
	char p[DEPTH];
728
	char tak[9];
729
	int index;
730
	int i;
731
732
733
	toReturn = (t_path*)malloc(sizeof(t_path));
734
	
735
	for (i = 0; i < toCopy->index; ++i)
736
	{
737
		toReturn->path[i] = toCopy->path[i];
738
	}
739
740
	
741
	
742
	for (i = 1; i <=9; ++i)
743
	{
744
		toReturn->taken[i] = toCopy->taken[i];
745
	}
746
747
748
	
749
	toReturn->index = toCopy->index;
750
751
	return toReturn;
752
}
753
754
755
756
void drawTakenMap(t_path* p){
757
758
	int i;
759
	printf("taken:");
760
761
	for (i = 1; i <= 9; ++i)
762
	{
763
		if(p->taken[i] == 1){
764
765
			switch(i){
766
			case E1:
767
				printf(" E1 ");
768
				break;
769
			case E2:
770
				printf(" E2 ");
771
				break;
772
			case E3:
773
				printf(" E3 ");
774
				break;
775
			case E4:
776
				printf(" E4 ");
777
				break;
778
			case C1:
779
				printf(" C1 ");
780
				break;
781
			case C2:
782
				printf(" C2 ");
783
				break;
784
			case C3:
785
				printf(" C3 ");
786
				break;
787
			case C4:
788
				printf(" C4 ");
789
				break;
790
			case M:
791
				printf(" M ");
792
				break;
793
			default :
794
				printf("default : %d", i);
795
796
		}
797
798
		} 
799
	}
800
	printf("\n");
801
802
}
803
804
805
void updateTakenMap(t_path* p){
806
807
	int i = 0;
808
	//reset taken
809
	for(i = 1; i<=9; i++){
810
		p->taken[i] = 0;
811
	}
812
813
814
	for(i = 0; i<=p->index; i++){
815
816
		p->taken[p->path[i]] = 1;
817
	}
818
819
}