A Algorithm Implementation for Maze Issue
Here is my simple implementation for maze issue.
1
#include
<
stdio.h
>
2 #include < stdlib.h >
3 #include < stdext.h >
4 #include < stdbool.h >
5 #define ROW_NUM 8
6 #define COL_NUM 8
7
8 typedef struct {
9 int x;
10 int y;
11 struct position * next;
12 } position;
13
14 static int maze[ROW_NUM][COL_NUM] = {{ 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 },
15 { 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 },
16 { 0 , 1 , 0 , 1 , 1 , 0 , 1 , 0 },
17 { 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 },
18 { 0 , 1 , 1 , 0 , 1 , 1 , 1 , 1 },
19 { 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 },
20 { 1 , 1 , 0 , 1 , 0 , 1 , 1 , 0 },
21 { 0 , 0 , 1 , 0 , 1 , 1 , 0 , 0 }};
22
23 bool isvalid( int x, int y)
24 {
25 if (x >= 0 && x < ROW_NUM && y >= 0 && y < COL_NUM && maze[x][y] == 0 ) return true ;
26 return false ;
27 }
28
29 bool inpath( int x, int y, position * pos)
30 {
31 while (pos != NULL) {
32 if (pos -> x == x && pos -> y == y) return true ;
33 pos = (position * )(pos -> next);
34 }
35 return false ;
36 }
37
38
39 static bool pathfound = false ;
40
41 void walkmaze( int x, int y, position * pos)
42 {
43 if (pathfound || ! isvalid(x, y) || inpath(x, y, pos)) return ;
44 if (x == ROW_NUM - 1 && y == COL_NUM - 1 ) {
45 printf( " (%d,%d) " , x, y);
46 while (pos != NULL) {
47 printf( " <- (%d,%d) " , pos -> x, pos -> y);
48 pos = (position * )pos -> next;
49 }
50 puts( "" ); pathfound = true ; return ;
51 }
52 position pos6; pos6.x = x, pos6.y = y; pos6.next = pos; walkmaze(x + 1 , y + 1 , & pos6);
53 position pos8; pos8.x = x, pos8.y = y; pos8.next = pos; walkmaze(x , y + 1 , & pos8);
54 position pos5; pos5.x = x, pos5.y = y; pos5.next = pos; walkmaze(x + 1 , y , & pos5);
55 position pos1; pos1.x = x, pos1.y = y; pos1.next = pos; walkmaze(x - 1 , y + 1 , & pos1);
56 position pos2; pos2.x = x, pos2.y = y; pos2.next = pos; walkmaze(x - 1 , y , & pos2);
57 position pos4; pos4.x = x, pos4.y = y; pos4.next = pos; walkmaze(x + 1 , y - 1 , & pos4);
58 position pos7; pos7.x = x, pos7.y = y; pos7.next = pos; walkmaze(x , y - 1 , & pos7);
59 position pos3; pos3.x = x, pos3.y = y; pos3.next = pos; walkmaze(x - 1 , y - 1 , & pos3);
60 }
61
62 int main()
63 {
64 walkmaze( 0 , 0 , NULL);
65 return 0 ;
66 }
67
2 #include < stdlib.h >
3 #include < stdext.h >
4 #include < stdbool.h >
5 #define ROW_NUM 8
6 #define COL_NUM 8
7
8 typedef struct {
9 int x;
10 int y;
11 struct position * next;
12 } position;
13
14 static int maze[ROW_NUM][COL_NUM] = {{ 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 },
15 { 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 },
16 { 0 , 1 , 0 , 1 , 1 , 0 , 1 , 0 },
17 { 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 },
18 { 0 , 1 , 1 , 0 , 1 , 1 , 1 , 1 },
19 { 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 },
20 { 1 , 1 , 0 , 1 , 0 , 1 , 1 , 0 },
21 { 0 , 0 , 1 , 0 , 1 , 1 , 0 , 0 }};
22
23 bool isvalid( int x, int y)
24 {
25 if (x >= 0 && x < ROW_NUM && y >= 0 && y < COL_NUM && maze[x][y] == 0 ) return true ;
26 return false ;
27 }
28
29 bool inpath( int x, int y, position * pos)
30 {
31 while (pos != NULL) {
32 if (pos -> x == x && pos -> y == y) return true ;
33 pos = (position * )(pos -> next);
34 }
35 return false ;
36 }
37
38
39 static bool pathfound = false ;
40
41 void walkmaze( int x, int y, position * pos)
42 {
43 if (pathfound || ! isvalid(x, y) || inpath(x, y, pos)) return ;
44 if (x == ROW_NUM - 1 && y == COL_NUM - 1 ) {
45 printf( " (%d,%d) " , x, y);
46 while (pos != NULL) {
47 printf( " <- (%d,%d) " , pos -> x, pos -> y);
48 pos = (position * )pos -> next;
49 }
50 puts( "" ); pathfound = true ; return ;
51 }
52 position pos6; pos6.x = x, pos6.y = y; pos6.next = pos; walkmaze(x + 1 , y + 1 , & pos6);
53 position pos8; pos8.x = x, pos8.y = y; pos8.next = pos; walkmaze(x , y + 1 , & pos8);
54 position pos5; pos5.x = x, pos5.y = y; pos5.next = pos; walkmaze(x + 1 , y , & pos5);
55 position pos1; pos1.x = x, pos1.y = y; pos1.next = pos; walkmaze(x - 1 , y + 1 , & pos1);
56 position pos2; pos2.x = x, pos2.y = y; pos2.next = pos; walkmaze(x - 1 , y , & pos2);
57 position pos4; pos4.x = x, pos4.y = y; pos4.next = pos; walkmaze(x + 1 , y - 1 , & pos4);
58 position pos7; pos7.x = x, pos7.y = y; pos7.next = pos; walkmaze(x , y - 1 , & pos7);
59 position pos3; pos3.x = x, pos3.y = y; pos3.next = pos; walkmaze(x - 1 , y - 1 , & pos3);
60 }
61
62 int main()
63 {
64 walkmaze( 0 , 0 , NULL);
65 return 0 ;
66 }
67