-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.c
1206 lines (1180 loc) · 41.2 KB
/
maze.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
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Description: The input is expected to consist of y_dim lines of x_dim *
* members of {0, 1, 2, 3}, where x_dim and y_dim are at least *
* equal to 2 and at most equal to MAX_X_DIM and MAX_Y_DIM, *
* respectively, with possibly lines consisting of spaces only *
* that will be ignored and with possibly spaces anywhere on the *
* lines with digits. *
* The xth digit n of the yth line, with 0 <= x < x_dim and *
* 0 <= y < y_dim, is to be associated with a point situated *
* x * 0.5 cm to the right and y * 0.5 cm below an origin, is to *
* be connected to the point 0.5 cm to its right *
* if n = 1 or n = 3, and is to be connected to the point *
* 0.5 cm below itself if n = 2 or n = 3. *
* The last digit on every line cannot be equal to 1 or 3, and *
* the digits on the last line cannot be equal to 2 or 3, which *
* ensures that the input encodes a maze, that is, a grid of *
* width (x_dim - 1) * 0.5 cm and of height (y_dim - 1) * 0.5 cm, *
* with possibly gaps on the sides and inside. A point not *
* connected to any of its neighbours is thought of as a pillar; *
* a point connected to at least one of its neighbours is thought *
* of as part of a wall. *
* We talk about "inner point" to refer to a point that lies *
* (x + 0.5) * 0.5 cm to the right of and (y + 0.5) * 0.5 cm *
* below the origin with 0 <= x < x_dim - 1 and *
* 0 <= y < y_dim - 1. *
* *
* Practically, the input will be stored in a file and its *
* contents redirected to standard input. The program will be run *
* with either co command-line argument or with "print" as unique *
* command line argument; otherwise it will exit. The program *
* will also exit immediately if the input is not as expected. *
* *
* When provided with no command-line argument, the program does *
* the following. *
* - It outputs the number of gates, that is, the number of *
* consecutive points on one of the four sides of the maze *
* that are not connected. *
* - It outputs the number of sets of connected walls. *
* - It outputs the number of inner points that cannot be *
* accessed from any gate, starting from the point in the *
* middle of a gate and going from inner points to neighbouring *
* inner points. *
* - It outputs the number of maximal areas that can be accessed *
* from at least one gate. *
* - It outputs the number of accessible cul-de-sacs. *
* - It outputs the number of paths consisting of inner points *
* that go from a gate to another gate and such that the *
* resulting path is choice-free, that is, such that leaving *
* the path, at any inner point where that is possible, *
* immediately leads into a cul-de-sac. *
* *
* When provided with "print" as unique command-line argument, *
* the program outputs some .tex code to depict the maze *
* as a tiz picture. *
* - Walls are drawn in blue. There is a command for every *
* longest segment that is part of a wall. Horizontal segments *
* are drawn starting with the topmost leftmost segment and *
* finishing with the bottommost rightmost segment. Then *
* vertical segments are drawn starting with the topmost *
* leftmost segment and finishing with the bottommost rightmost *
* segment. *
* - Pillars are drawn as green circles. *
* - Inner points in accessible cul-de-sacs are drawn as red *
* crosses. *
* - The choice-free paths are drawn as dashed yellow lines. *
* There is a command for every longest segment on such a *
* path. Horizontal segments are drawn starting with the *
* topmost leftmost segment and finishing with the bottommmost *
* rightmost segment, with those segments that end at a gate *
* sticking out by 0.25 cm. Then vertical segments are drawn *
* starting with the topmost leftmost segment and finishing *
* with the bottommmost rightmost segment, with those segments *
* that end at a gate sticking out by 0.25 cm. *
* *
* Written by Zhang Xi (3472528) for COMP9021 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#define MAX_X_DIM 31
#define MAX_Y_DIM 41
int size_of_line = 0;
int size_of_column = 0;
int number[MAX_X_DIM * MAX_Y_DIM] = {-1};//store the original input into an array
int grid[MAX_Y_DIM][MAX_X_DIM]; //rewrite the original input from array to matrix
int grid_transformed[MAX_Y_DIM - 1][MAX_X_DIM - 1]; //to transform the original matrix
/*Here I declare my functions which are used in the programe.*/
bool get_input(void);
void rebuild_grid(void); //to rebuild the original matrix when needed
int looking_for_gates(void);
int looking_for_walls(void);
void the_wall(int i, int j);
void trans_grid(void); // to build a transformed grid indicates the number of paths
void adjust_grid(void); // to adjust the modified transformed grid into a proper form
int get_element(int a, int b, int c);
void find_route(int i, int j);
int looking_for_inner_points(void);
int looking_for_access_areas(void);
int looking_for_dead_ends(void);
void mark_ends(int i, int j);
void count_ends(int i, int j);
int looking_for_one_way_paths(void);
void find_path(int i, int j);
void count_path(int i, int j);
void draw_horizontal_lines(void);
void draw_vertical_lines(void);
void draw_pillars(void);
void draw_cross(void);
void draw_horizontal_dash(void);
void draw_vertical_dash(void);
bool is_gate(int i, int j, int dir);
int main(int argc, char **argv) {
if (argc > 2 || (argc == 2 && strcmp(argv[1], "print"))) {
printf("I expect no command line argument or \"print\" as unique command line argument.\n");
return EXIT_FAILURE;
}
if (!get_input()) {
printf("Incorrect input.\n");
return EXIT_FAILURE;
}
if (argc == 2) {
printf("\\documentclass[10pt]{article}\n");
printf("\\usepackage{tikz}\n");
printf("\\usetikzlibrary{shapes.misc}\n");
printf("\\usepackage[margin=0cm]{geometry}\n");
printf("\\pagestyle{empty}\n");
printf("\\tikzstyle{every node}=[cross out, draw, red]\n\n");
printf("\\begin{document}\n\n");
printf("\\vspace*{\\fill}\n");
printf("\\begin{center}\n");
printf("\\begin{tikzpicture}[x=0.5cm, y=-0.5cm, ultra thick, blue]\n");
rebuild_grid();
printf("%% Walls\n");
draw_horizontal_lines();
draw_vertical_lines();
rebuild_grid();
draw_pillars();
rebuild_grid();
trans_grid();
looking_for_access_areas();
looking_for_inner_points();
looking_for_dead_ends();
printf("%% Inner points in accessible cul-de-sacs\n");
draw_cross();
looking_for_one_way_paths();
adjust_grid();
printf("%% Entry-exit paths without intersections\n");
draw_horizontal_dash();
draw_vertical_dash();
printf("\\end{tikzpicture}\n");
printf("\\end{center}\n");
printf("\\vspace*{\\fill}\n\n");
printf("\\end{document}\n");
return EXIT_SUCCESS;
}
rebuild_grid();
int num_of_gates = looking_for_gates();
if (num_of_gates == 0)
printf("The maze has no gate.\n");
if (num_of_gates == 1)
printf("The maze has a single gate.\n");
if (num_of_gates > 1)
printf("The maze has %d gates.\n", num_of_gates);
rebuild_grid();
int num_of_walls = looking_for_walls();
if (num_of_walls == 0)
printf("The maze has no wall.\n");
if (num_of_walls == 1)
printf("The maze has walls that are all connected.\n");
if (num_of_walls > 1)
printf("The maze has %d sets of walls that are all connected.\n", num_of_walls);
rebuild_grid();
trans_grid();
int num_of_accessible_areas = looking_for_access_areas();
int num_of_inner_points = looking_for_inner_points();
if (num_of_inner_points == 0)
printf("The maze has no inaccessible inner point.\n");
if (num_of_inner_points == 1)
printf("The maze has a unique inaccessible inner point.\n");
if (num_of_inner_points > 1)
printf("The maze has %d inaccessible inner points.\n", num_of_inner_points);
if (num_of_accessible_areas == 0)
printf("The maze has no accessible area.\n");
if (num_of_accessible_areas == 1)
printf("The maze has a unique accessible area.\n");
if (num_of_accessible_areas > 1)
printf("The maze has %d accessible areas.\n", num_of_accessible_areas);
int num_of_dead_ends = looking_for_dead_ends();
if (num_of_dead_ends == 0)
printf("The maze has no accessible cul-de-sac.\n");
if (num_of_dead_ends == 1)
printf("The maze has accessible cul-de-sacs that are all connected.\n");
if (num_of_dead_ends > 1)
printf("The maze has %d sets of accessible cul-de-sacs that are all connected.\n", num_of_dead_ends);
int num_of_paths = looking_for_one_way_paths();
if (num_of_paths == 0)
printf("The maze has no entry-exit path with no intersection not to cul-de-sacs.\n");
if (num_of_paths == 1)
printf("The maze has a unique entry-exit path with no intersection not to cul-de-sacs.\n");
if (num_of_paths > 1)
printf("The maze has %d entry-exit paths with no intersections not to cul-de-sacs.\n", num_of_paths);
}
/*Read the original input array. If satisfied, return true, otherwise return false. Then trun
* the inputs into an executable form.*/
bool get_input(void) {
int ch;
bool indicator = true;
int count_of_one_line = 0;
int tmp;
int k = 0;
while((ch = getc(stdin)) != EOF) {
if ((ch == '\n' || ch == ' ') && !count_of_one_line)
continue;
if (ch < '4' && ch >= '0') {
count_of_one_line++;
if (count_of_one_line > MAX_X_DIM) {
indicator = false;
break;
}
tmp = ch;
number[k] = ch - '0';
k++;
}
if ((ch >= '4' || ch < '0') && ch != ' ' && ch != '\n') {
indicator = false;
break;
}
if (ch == '\n') {
if (size_of_column == 0 && (tmp == '1' || tmp == '3')) {
indicator = false;
break;
}
if (size_of_column == 0 && tmp != '1' && tmp != '3')
size_of_line = count_of_one_line;
if (size_of_column != 0 && (tmp == '1' || tmp == '3' || count_of_one_line != size_of_line)) {
indicator = false;
break;
}
size_of_column++;
count_of_one_line = 0;
}
}
if (indicator) {
if (size_of_column > MAX_Y_DIM || size_of_column < 2 || size_of_line < 2)
indicator = false;
for (int i = size_of_column * size_of_line - 1; i >= (size_of_column - 1) * size_of_line; --i)
if (number[i] == 2 || number[i] == 3) {
indicator = false;
break;
}
}
return indicator;
}
void rebuild_grid(void) {
for (int i = 0; i < size_of_column; ++i)
for (int j = 0; j < size_of_line; ++j)
grid[i][j] = number[size_of_line * i + j];
}
void trans_grid(void) {
for (int i = 0; i < size_of_column - 1; ++i)
for (int j = 0; j < size_of_line - 1; ++j) {
int a = grid[i][j];
int b = grid[i][j+1];
int c = grid[i+1][j];
grid_transformed[i][j] = get_element(a, b, c);
}
}
/*Here I decompose the big matrix into 2*2 small matrix, and count the number of gates of small matrix
* , which is the number of paths at the same position of original matrix.*/
int get_element(int a, int b, int c) {
int count = 0;
if (!a)
count += 2;
if (a == 2 || a == 1)
count++;
if (b == 0 || b == 1)
count++;
if (c == 0 || c == 2)
count++;
return count;
}
int looking_for_gates(void) {
int num_of_gates = 0;
for (int j = 0; j < size_of_line - 1; ++j) {
if (grid[0][j] == 0 || grid[0][j] == 2)
num_of_gates++;
}
for (int i = 0; i < size_of_column - 1; ++i) {
if (grid[i][0] == 0 || grid[i][0] == 1)
num_of_gates++;
}
for (int j = 0; j < size_of_line - 1; ++j) {
if (grid[size_of_column-1][j] == 0)
num_of_gates++;
}
for (int i = 0; i < size_of_column - 1; ++i) {
if (grid[i][size_of_line-1] == 0)
num_of_gates++;
}
return num_of_gates;
}
int looking_for_walls(void) {
int num_of_walls = 0;
for (int i = 0; i < size_of_column; ++i)
for (int j = 0; j < size_of_line; ++j)
if (grid[i][j]) {
the_wall(i, j);
num_of_walls++;
}
return num_of_walls;
}
/*I use this function to go alongside the wall and demolish it by replacing the number by 0.*/
void the_wall(int i, int j) {
int i1, j1;
switch (grid[i][j]) {
case 1:
grid[i][j] = 0;
if ((grid[i-1][j] == 2 || grid[i-1][j] == 3) && i - 1 >= 0) {
i1 = i - 1;
j1 = j;
the_wall(i1, j1);
}
if ((grid[i][j-1] == 1 || grid[i][j-1] == 3) && j - 1 >= 0) {
i1 = i;
j1 = j - 1;
the_wall(i1, j1);
}
if (grid[i][j+1] && j + 1 < size_of_line) {
i1 = i;
j1 = j + 1;
the_wall(i1, j1);
}
if ((grid[i-1][j+1] == 2 || grid[i-1][j+1] == 3)
&& i - 1 >= 0
&& j + 1 < size_of_line) {
i1 = i - 1;
j1 = j + 1;
the_wall(i1, j1);
}
break;
case 2:
grid[i][j] = 0;
if ((grid[i-1][j] == 2 || grid[i-1][j] == 3) && i - 1 >= 0) {
i1 = i - 1;
j1 = j;
the_wall(i1, j1);
}
if ((grid[i][j-1] == 1 || grid[i][j-1] == 3) && j - 1 >= 0) {
i1 = i;
j1 = j - 1;
the_wall(i1, j1);
}
if (grid[i+1][j] && i + 1 < size_of_column) {
i1 = i + 1;
j1 = j;
the_wall(i1, j1);
}
if ((grid[i+1][j-1] == 1 || grid[i+1][j-1] == 3)
&& j - 1 >= 0
&& i + 1 < size_of_column) {
i1 = i + 1;
j1 = j - 1;
the_wall(i1, j1);
}
break;
case 3:
grid[i][j] = 0;
if ((grid[i-1][j] == 2 || grid[i-1][j] == 3) && i - 1 >= 0) {
i1 = i - 1;
j1 = j;
the_wall(i1, j1);
}
if ((grid[i][j-1] == 1 || grid[i][j-1] == 3) && j - 1 >= 0) {
i1 = i;
j1 = j - 1;
the_wall(i1, j1);
}
if (grid[i][j+1] && j + 1 < size_of_line) {
i1 = i;
j1 = j + 1;
the_wall(i1, j1);
}
if (grid[i+1][j] && i + 1 < size_of_column) {
i1 = i + 1;
j1 = j;
the_wall(i1, j1);
}
if ((grid[i-1][j+1] == 2 || grid[i-1][j+1] == 3)
&& i - 1 >= 0
&& j + 1 < size_of_line) {
i1 = i - 1;
j1 = j + 1;
the_wall(i1, j1);
}
if ((grid[i+1][j-1] == 1 || grid[i+1][j-1] == 3)
&& j - 1 >= 0
&& i + 1 < size_of_column) {
i1 = i + 1;
j1 = j - 1;
the_wall(i1, j1);
}
break;
}
}
/*I use this function after finishing using the function "looking_for_access_area()".*/
int looking_for_inner_points(void) {
int num_of_inner_points = 0;
for (int i = 0; i < size_of_column -1; ++i)
for (int j = 0; j < size_of_line - 1; ++j) {
if (grid[i][j] != 4)
num_of_inner_points++;
}
return num_of_inner_points;
}
int looking_for_access_areas(void) {
int num_of_accessible_areas = 0;
for (int i = 0, j = 0; j < size_of_line - 1; ++j)
if (grid[i][j] == 0 || grid[i][j] == 2) {
find_route(i, j);
num_of_accessible_areas++;
}
for (int i = 0, j = 0; i < size_of_column - 1; ++i)
if (grid[i][0] == 0 || grid[i][0] == 1) {
find_route(i, j);
num_of_accessible_areas++;
}
for (int i = size_of_column - 1, j = 0; j < size_of_line - 1; ++j)
if (grid[size_of_column-1][j] == 0) {
find_route(i, j);
num_of_accessible_areas++;
}
for (int i = 0, j = size_of_line - 1; i < size_of_column - 1; ++i)
if (grid[i][size_of_line-1] == 0) {
find_route(i, j);
num_of_accessible_areas++;
}
return num_of_accessible_areas;
}
void find_route(int i, int j) {
int i1, j1;
if (grid[i][j] == 0) {
grid[i][j] = 4;
if (grid[i][j-1] < 4
&& j-1 >= 0
&& i < size_of_column - 1) {
i1 = i;
j1 = j-1;
find_route(i1, j1);
}
if (grid[i-1][j] < 4
&& i-1 >= 0
&& j < size_of_line - 1) {
i1 = i-1;
j1 = j;
find_route(i1, j1);
}
if (grid[i][j+1] < 2
&& j+1 < size_of_line
&& i < size_of_column - 1) {
i1 = i;
j1 = j+1;
find_route(i1, j1);
}
if (i+1 < size_of_column
&& j < size_of_line - 1
&& (grid[i+1][j] == 0 || grid[i+1][j] == 2)) {
i1 = i+1;
j1 = j;
find_route(i1, j1);
}
}
if (grid[i][j] == 1) {
grid[i][j] = 4;
if (grid[i][j-1] < 4
&& j-1 >= 0
&& i < size_of_column - 1) {
i1 = i;
j1 = j-1;
find_route(i1, j1);
}
if (grid[i][j+1] < 2
&& j+1 < size_of_line
&& i < size_of_column - 1) {
i1 = i;
j1 = j+1;
find_route(i1, j1);
}
if ((grid[i+1][j] == 0 || grid[i+1][j] == 2)
&& i+1 < size_of_column
&& j < size_of_line - 1) {
i1 = i+1;
j1 = j;
find_route(i1, j1);
}
}
if (grid[i][j] == 2) {
grid[i][j] = 4;
if (grid[i-1][j] < 4
&& i-1 >= 0
&& j < size_of_line - 1) {
i1 = i-1;
j1 = j;
find_route(i1, j1);
}
if (grid[i][j+1] < 2
&& j+1 < size_of_line
&& i < size_of_column - 1) {
i1 = i;
j1 = j+1;
find_route(i1, j1);
}
if ((grid[i+1][j] == 0 || grid[i+1][j] == 2)
&& i+1 < size_of_column
&& j < size_of_line - 1) {
i1 = i+1;
j1 = j;
find_route(i1, j1);
}
}
if (grid[i][j] == 3) {
grid[i][j] = 4;
if (grid[i][j+1] < 2
&& j+1 < size_of_line
&& i < size_of_column - 1) {
i1 = i;
j1 = j+1;
find_route(i1, j1);
}
if ((grid[i+1][j] == 0 || grid[i+1][j] == 2)
&& i+1 < size_of_column
&& j < size_of_line - 1) {
i1 = i+1;
j1 = j;
find_route(i1, j1);
}
}
}
int looking_for_dead_ends(void) {
int num_of_dead_ends = 0;
for (int i = 0; i < size_of_column - 1; ++i)
for (int j = 0; j < size_of_line - 1; ++j)
if (grid[i][j] != 4)
grid_transformed[i][j] = 0;
rebuild_grid();
for (int i = 0; i < size_of_column - 1; ++i)
for (int j = 0; j < size_of_line - 1; ++j)
if (grid_transformed[i][j] == 1)
mark_ends(i, j);
for (int i = 0; i < size_of_column - 1; ++i)
for (int j = 0; j < size_of_line - 1; ++j)
if (grid_transformed[i][j] == 6) {
count_ends(i, j);
num_of_dead_ends++;
}
return num_of_dead_ends;
}
void mark_ends(int i, int j) {
int i1, j1;
grid_transformed[i][j] = 6;
if (grid[i][j] == 0) {
if (grid[i][j-1] < 4
&& grid_transformed[i][j-1] != 1
&& grid_transformed[i][j-1] != 6
&& j-1 >= 0) {
grid_transformed[i][j-1]--;
i1 = i;
j1 = j-1;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
if (grid[i-1][j] < 4
&& grid_transformed[i-1][j] != 1
&& grid_transformed[i-1][j] != 6
&& i-1 >= 0) {
grid_transformed[i-1][j]--;
i1 = i-1;
j1 = j;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] != 1
&& grid_transformed[i][j+1] != 6
&& j+1 < size_of_line - 1) {
grid_transformed[i][j+1]--;
i1 = i;
j1 = j+1;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] != 1
&& grid_transformed[i+1][j] != 6
&& i+1 < size_of_column - 1) {
grid_transformed[i+1][j]--;
i1 = i+1;
j1 = j;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
}
if (grid[i][j] == 1) {
if (grid[i][j-1] < 4
&& grid_transformed[i][j-1] != 1
&& grid_transformed[i][j-1] != 6
&& j-1 >= 0) {
grid_transformed[i][j-1]--;
i1 = i;
j1 = j-1;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] != 1
&& grid_transformed[i][j+1] != 6
&& j+1 < size_of_line - 1) {
grid_transformed[i][j+1]--;
i1 = i;
j1 = j+1;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] != 1
&& grid_transformed[i+1][j] != 6
&& i+1 < size_of_column - 1) {
grid_transformed[i+1][j]--;
i1 = i+1;
j1 = j;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
}
if (grid[i][j] == 2) {
if (grid[i-1][j] < 4
&& grid_transformed[i-1][j] != 1
&& grid_transformed[i-1][j] != 6
&& i-1 >= 0) {
grid_transformed[i-1][j]--;
i1 = i-1;
j1 = j;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] != 1
&& grid_transformed[i][j+1] != 6
&& j+1 < size_of_line - 1) {
grid_transformed[i][j+1]--;
i1 = i;
j1 = j+1;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] != 1
&& grid_transformed[i+1][j] != 6
&& i+1 < size_of_column - 1) {
grid_transformed[i+1][j]--;
i1 = i+1;
j1 = j;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
}
if (grid[i][j] == 3) {
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] != 1
&& grid_transformed[i][j+1] != 6
&& j+1 < size_of_line - 1) {
grid_transformed[i][j+1]--;
i1 = i;
j1 = j+1;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
if ((grid[i+1][j] == 0 || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] != 1
&& grid_transformed[i+1][j] != 6
&& i+1 < size_of_column - 1) {
grid_transformed[i+1][j]--;
i1 = i+1;
j1 = j;
if (grid_transformed[i1][j1] == 1)
mark_ends(i1, j1);
}
}
}
void count_ends(int i, int j) {
int i1, j1;
grid_transformed[i][j] = 7;
if (grid[i][j] == 0) {
if (grid[i][j-1] < 4
&& grid_transformed[i][j-1] == 6
&& j-1 >= 0) {
i1 = i;
j1 = j-1;
count_ends(i1, j1);
}
if (grid[i-1][j] < 4
&& grid_transformed[i-1][j] == 6
&& i-1 >= 0) {
i1 = i-1;
j1 = j;
count_ends(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] == 6
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
count_ends(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] == 6
&& i+1 < size_of_column - 1) {
i1 = i+1;
j1 = j;
count_ends(i1, j1);
}
}
if (grid[i][j] == 1) {
if (grid[i][j-1] < 4
&& grid_transformed[i][j-1] == 6
&& j-1 >= 0) {
i1 = i;
j1 = j-1;
count_ends(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] == 6
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
count_ends(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] == 6
&& i+1 < size_of_column - 1) {
i1 = i+1;
j1 = j;
count_ends(i1, j1);
}
}
if (grid[i][j] == 2) {
if (grid[i-1][j] < 4
&& grid_transformed[i-1][j] == 6
&& i-1 >= 0) {
i1 = i-1;
j1 = j;
count_ends(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] == 6
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
count_ends(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] == 6
&& i+1 < size_of_column - 1) {
i1 = i+1;
j1 = j;
count_ends(i1, j1);
}
}
if (grid[i][j] == 3) {
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] == 6
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
count_ends(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] == 6
&& i+1 < size_of_column - 1) {
i1 = i+1;
j1 = j;
count_ends(i1, j1);
}
}
}
int looking_for_one_way_paths(void) {
int num_of_paths = 0;
for (int i = 0; i < size_of_column - 1; ++i)
for (int j = 0; j < size_of_line -1; ++j)
if (grid_transformed[i][j] == 4)
find_path(i, j);
for (int i = 0; i < size_of_column - 1; ++i)
for (int j = 0; j < size_of_line -1; ++j)
if (grid_transformed[i][j] == 3)
find_path(i, j);
for (int i = 0; i < size_of_column - 1; ++i)
for (int j = 0; j < size_of_line -1; ++j)
if (grid_transformed[i][j] == 2) {
count_path(i, j);
num_of_paths++;
}
return num_of_paths;
}
void find_path(int i, int j) {
int i1, j1;
grid_transformed[i][j] = 8;
if (grid[i][j] == 0) {
if (grid[i][j-1] < 4
&& grid_transformed[i][j-1] != 7
&& grid_transformed[i][j-1] != 8
&& j-1 >= 0) {
i1 = i;
j1 = j-1;
find_path(i1, j1);
}
if (grid[i-1][j] < 4
&& grid_transformed[i-1][j] != 7
&& grid_transformed[i-1][j] != 8
&& i-1 >= 0) {
i1 = i-1;
j1 = j;
find_path(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] != 7
&& grid_transformed[i][j+1] != 8
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
find_path(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] != 7
&& grid_transformed[i+1][j] != 8
&& i+1 < size_of_column - 1) {
i1 = i+1;
j1 = j;
find_path(i1, j1);
}
}
if (grid[i][j] == 1) {
if (grid[i][j-1] < 4
&& grid_transformed[i][j-1] != 7
&& grid_transformed[i][j-1] != 8
&& j-1 >= 0) {
i1 = i;
j1 = j-1;
find_path(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] != 7
&& grid_transformed[i][j+1] != 8
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
find_path(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] != 7
&& grid_transformed[i+1][j] != 8
&& i+1 < size_of_column - 1) {
i1 = i+1;
j1 = j;
find_path(i1, j1);
}
}
if (grid[i][j] == 2) {
if (grid[i-1][j] < 4
&& grid_transformed[i-1][j] != 7
&& grid_transformed[i-1][j] != 8
&& i-1 >= 0) {
i1 = i-1;
j1 = j;
find_path(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] != 7
&& grid_transformed[i][j+1] != 8
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
find_path(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] != 7
&& grid_transformed[i+1][j] != 8
&& i+1 < size_of_column -1) {
i1 = i+1;
j1 = j;
find_path(i1, j1);
}
}
if (grid[i][j] == 3) {
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] != 7
&& grid_transformed[i][j+1] != 8
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
find_path(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] != 7
&& grid_transformed[i+1][j] != 8
&& i+1 < size_of_column - 1) {
i1 = i+1;
j1 = j;
find_path(i1, j1);
}
}
}
void count_path(int i, int j) {
int i1, j1;
grid_transformed[i][j] = -2;
if (grid[i][j] == 0) {
if (grid[i][j-1] < 4
&& grid_transformed[i][j-1] == 2
&& j-1 >= 0) {
i1 = i;
j1 = j-1;
count_path(i1, j1);
}
if (grid[i-1][j] < 4
&& grid_transformed[i-1][j] == 2
&& i-1 >= 0) {
i1 = i-1;
j1 = j;
count_path(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] == 2
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
count_path(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] == 2
&& i+1 < size_of_column - 1) {
i1 = i+1;
j1 = j;
count_path(i1, j1);
}
}
if (grid[i][j] == 1) {
if (grid[i][j-1] < 4
&& grid_transformed[i][j-1] == 2
&& j-1 >= 0) {
i1 = i;
j1 = j-1;
count_path(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] == 2
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
count_path(i1, j1);
}
if ((!grid[i+1][j] || grid[i+1][j] == 2)
&& grid_transformed[i+1][j] == 2
&& i+1 < size_of_column - 1) {
i1 = i+1;
j1 = j;
count_path(i1, j1);
}
}
if (grid[i][j] == 2) {
if (grid[i-1][j] < 4
&& grid_transformed[i-1][j] == 2
&& i-1 >= 0) {
i1 = i-1;
j1 = j;
count_path(i1, j1);
}
if (grid[i][j+1] < 2
&& grid_transformed[i][j+1] == 2
&& j+1 < size_of_line - 1) {
i1 = i;
j1 = j+1;
count_path(i1, j1);