数据结构-栈应用(迷宫问题)
算法:(此算法来自于<<算法与数据结构-C语言描述>>张乃孝 主编, 但书中的代码有错误,红色部分为修正部分,与各位看官共勉)
1 //栈运用-迷宫问题
2 //2009-06-22 by Larman Yuan
3
4 #include "stdafx.h"
5 #include <stdlib.h>
6 #define M 8
7 #define N 11
8 #define MAXNUM 54
9
10 typedef struct
11 {
12 int x,y,d;//d代表方向
13 }DataType;
14
15 struct SeqStack
16 {
17 DataType s[MAXNUM];
18 int t;
19 };
20
21 typedef struct SeqStack * PSeqStack;
22
23 PSeqStack createEmptyStack_seq(void)
24 {
25 PSeqStack pastack;
26 pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
27 if(pastack == NULL)
28 printf("Out of space!!\n");
29 else
30 pastack->t = -1;
31 return (pastack);
32 }
33
34 int isEmptyStack_seq(PSeqStack pastack)
35 {
36 return(pastack->t == -1);
37 }
38
39 void push_seq(PSeqStack pastack, DataType x)
40 {
41 if(pastack->t >= MAXNUM -1)
42 {
43 printf("Overflow!\n");
44 }
45 else
46 {
47 pastack->t = pastack->t + 1;
48 pastack->s[pastack->t] = x;
49 }
50 }
51
52 void pop_seq(PSeqStack pastack)
53 {
54 if(pastack->t == -1)
55 printf("Underflow! \n");
56 else
57 pastack->t = pastack->t - 1;
58 }
59
60 DataType top_seq(PSeqStack pastack)
61 {
62 return(pastack->s[pastack->t]);
63 }
64 //迷宫全局变量
65 int maze[M][N] = {
66 {1,1,1,1,1,1,1,1,1,1,1},
67 {1,0,1,0,0,1,1,1,0,0,1},
68 {1,0,0,0,0,0,1,0,0,1,1},
69 {1,0,1,1,1,0,0,0,1,1,1},
70 {1,0,0,0,1,0,1,1,0,1,1},
71 {1,1,0,0,1,0,1,1,0,0,1},
72 {1,1,1,0,0,0,0,0,0,0,1},
73 {1,1,1,1,1,1,1,1,1,1,1}
74 };
75
76 int direction[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
77 //迷宫算法
78 void mazePath(int * maze[], int * direction[], int x1, int y1, int x2, int y2)
79 {
80 int i,j,k;
81 int g,h;
82 PSeqStack st;
83 DataType element;
84 st = createEmptyStack_seq();
85 maze[x1][y1] = 2;
86 element.x = x1;
87 element.y = y1;
88 element.d = -1;
89 push_seq(st,element);
90 while(!isEmptyStack_seq(st))
91 {
92 element = top_seq(st);
93 pop_seq(st);
94 i = element.x;
95 j = element.y;
96 k = element.d + 1;
97 while(k <= 3)
98 {
99 g = i + direction[k][0];
100 h = j + direction[k][1];
101 if(maze[g][h] == 0)
102 {
103 maze[g][h] = 2;
104 element.x = i;
105 element.y = j;
106 element.d = k;
107 push_seq(st,element);
108 i = g;
109 j = h;
110 k = -1;
111 }
112 if(g == x2 && h == y2 && (maze[g][h] == 0||maze[g][h]==2))
113 {
114 printf("The reverse path is:\n");
115 element.x = g;
116 element.y = h;
117 push_seq(st,element);
118 while(!isEmptyStack_seq(st))
119 {
120 element = top_seq(st);
121 pop_seq(st);
122 printf("the node is:(%d %d)\n",element.x, element.y);
123 }
124 return;
125 }
126 k = k + 1;
127 }
128 }
129 printf("The path has not been found.\n");
130 }
131 int _tmain(int argc, _TCHAR* argv[])
132 {
133 int * m[M];
134 int ** p;
135 int *d[4];
136 int i;
137 for(i = 0; i < M; i++)
138 {
139 m[i] = maze[i];
140 }
141 p = m;
142 for(i = 0; i < 4; i++)
143 {
144 d[i] = direction[i];
145 }
146 mazePath(p, d, 1,1,6,9);
147 return 0;
148 }
149
2 //2009-06-22 by Larman Yuan
3
4 #include "stdafx.h"
5 #include <stdlib.h>
6 #define M 8
7 #define N 11
8 #define MAXNUM 54
9
10 typedef struct
11 {
12 int x,y,d;//d代表方向
13 }DataType;
14
15 struct SeqStack
16 {
17 DataType s[MAXNUM];
18 int t;
19 };
20
21 typedef struct SeqStack * PSeqStack;
22
23 PSeqStack createEmptyStack_seq(void)
24 {
25 PSeqStack pastack;
26 pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
27 if(pastack == NULL)
28 printf("Out of space!!\n");
29 else
30 pastack->t = -1;
31 return (pastack);
32 }
33
34 int isEmptyStack_seq(PSeqStack pastack)
35 {
36 return(pastack->t == -1);
37 }
38
39 void push_seq(PSeqStack pastack, DataType x)
40 {
41 if(pastack->t >= MAXNUM -1)
42 {
43 printf("Overflow!\n");
44 }
45 else
46 {
47 pastack->t = pastack->t + 1;
48 pastack->s[pastack->t] = x;
49 }
50 }
51
52 void pop_seq(PSeqStack pastack)
53 {
54 if(pastack->t == -1)
55 printf("Underflow! \n");
56 else
57 pastack->t = pastack->t - 1;
58 }
59
60 DataType top_seq(PSeqStack pastack)
61 {
62 return(pastack->s[pastack->t]);
63 }
64 //迷宫全局变量
65 int maze[M][N] = {
66 {1,1,1,1,1,1,1,1,1,1,1},
67 {1,0,1,0,0,1,1,1,0,0,1},
68 {1,0,0,0,0,0,1,0,0,1,1},
69 {1,0,1,1,1,0,0,0,1,1,1},
70 {1,0,0,0,1,0,1,1,0,1,1},
71 {1,1,0,0,1,0,1,1,0,0,1},
72 {1,1,1,0,0,0,0,0,0,0,1},
73 {1,1,1,1,1,1,1,1,1,1,1}
74 };
75
76 int direction[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
77 //迷宫算法
78 void mazePath(int * maze[], int * direction[], int x1, int y1, int x2, int y2)
79 {
80 int i,j,k;
81 int g,h;
82 PSeqStack st;
83 DataType element;
84 st = createEmptyStack_seq();
85 maze[x1][y1] = 2;
86 element.x = x1;
87 element.y = y1;
88 element.d = -1;
89 push_seq(st,element);
90 while(!isEmptyStack_seq(st))
91 {
92 element = top_seq(st);
93 pop_seq(st);
94 i = element.x;
95 j = element.y;
96 k = element.d + 1;
97 while(k <= 3)
98 {
99 g = i + direction[k][0];
100 h = j + direction[k][1];
101 if(maze[g][h] == 0)
102 {
103 maze[g][h] = 2;
104 element.x = i;
105 element.y = j;
106 element.d = k;
107 push_seq(st,element);
108 i = g;
109 j = h;
110 k = -1;
111 }
112 if(g == x2 && h == y2 && (maze[g][h] == 0||maze[g][h]==2))
113 {
114 printf("The reverse path is:\n");
115 element.x = g;
116 element.y = h;
117 push_seq(st,element);
118 while(!isEmptyStack_seq(st))
119 {
120 element = top_seq(st);
121 pop_seq(st);
122 printf("the node is:(%d %d)\n",element.x, element.y);
123 }
124 return;
125 }
126 k = k + 1;
127 }
128 }
129 printf("The path has not been found.\n");
130 }
131 int _tmain(int argc, _TCHAR* argv[])
132 {
133 int * m[M];
134 int ** p;
135 int *d[4];
136 int i;
137 for(i = 0; i < M; i++)
138 {
139 m[i] = maze[i];
140 }
141 p = m;
142 for(i = 0; i < 4; i++)
143 {
144 d[i] = direction[i];
145 }
146 mazePath(p, d, 1,1,6,9);
147 return 0;
148 }
149
运行结果:
The reverse path is:
the node is:(6 9)
the node is:(6 8)
the node is:(6 7)
the node is:(6 6)
the node is:(6 5)
the node is:(5 5)
the node is:(4 5)
the node is:(3 5)
the node is:(2 5)
the node is:(2 4)
the node is:(2 3)
the node is:(2 2)
the node is:(2 1)
the node is:(1 1)