-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path栈的应用_迷宫_.cpp
314 lines (276 loc) · 4.73 KB
/
栈的应用_迷宫_.cpp
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
#include <stdio.h>
#include <stdlib.h>
#define ROW 10
#define COL 10
#define Max 100
#define True 1
#define False 0
typedef struct
{
int pos_x;
int pos_y;
}pos;
typedef struct
{
pos data[Max];
int top;
}stack,*stk;
int init_stack(stack &s)
{
s.top=-1;
return 0;
}
int push_stack(stack &s,pos &e)
{
if(s.top>Max-1)
{
return False;
}
s.data[++s.top]=e;
//printf("-->%d,%d\n",e.pos_x,e.pos_y);
return True;
}
pos pop_stack(stack &s,pos &e)
{
if(s.top>-1)
{
e=s.data[s.top--];
//printf("-->%d,%d\n",e.pos_x,e.pos_y);
return e;
}
}
pos top_stack(stack s)
{
if(s.top>-1)
{
return s.data[s.top];
}
}
int empty_stack(stack s)
{
if(s.top==-1)
{
return NULL;
}
return True;
}
int miwu[12][12]=
{
1,1,1,1,1,1,1,1,1,1,1,1,
1,0,1,1,1,1,1,1,1,1,0,1,
1,0,1,0,1,0,1,0,1,1,0,1,
1,0,0,1,1,0,1,0,0,1,0,1,
1,0,0,0,1,0,0,0,0,0,0,1,
1,0,0,1,1,0,1,0,0,1,0,1,
1,1,0,0,1,0,1,0,1,1,0,1,
1,0,1,0,0,0,1,0,1,1,0,1,
1,1,0,1,0,0,1,0,1,0,0,1,
1,1,1,1,0,0,1,0,0,1,0,1,
1,1,1,1,1,1,1,1,1,1,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,
};
int curstep=1;
int migong()
{
int i,j;
for(i=0;i<12;i++)
{
for(j=0;j<12;j++)
{
if(miwu[i][j]==1)
{
printf("■" );
}
else
{
printf(" " );
}
}
printf("\n");
}
/*printf("\n\n");
printf("----------------------------------\n");
printf("----------------------------------\n");
printf("该迷宫起始坐标(%d,%d).\n",1,1);
printf("该迷宫终点坐标(%d,%d).\n",10,10);
printf("----------------------------------\n");
printf("----------------------------------\n");*/
printf("\n\n");
}
int migong1()
{
int i,j;
for(i=0;i<12;i++)
{
for(j=0;j<12;j++)
{
if(miwu[i][j]==1)
{
printf("■" );
}
else if(miwu[i][j]==0)
{
printf(" " );
}
else
{
//printf("%2d" ,miwu[i][j]-1);
printf("〇" );
}
}
printf("\n");
}
printf("\n\n");
}
int migong2()
{
int i,j;
for(i=0;i<12;i++)
{
for(j=0;j<12;j++)
{
if(miwu[i][j]==1)
{
printf("■" );
}
else if(miwu[i][j]==0)
{
printf(" " );
}
else
{
printf("%2d" ,miwu[i][j]-1);
//printf("〇" );
}
}
printf("\n");
}
printf("\n\n");
}
/*判断当前位置是否可行,即判断是路,还是墙*/
int pass(pos p)
{
if (miwu[p.pos_x][p.pos_y]==1)
return 0;
else
return 1;
}
/*将当前的步数填写在走的每一步正确的路上,当发现走不通时,会把当时写的信息
用‘1’抹掉,代表走不通。*/
void footPrint(pos p)
{
miwu[p.pos_x][p.pos_y] = curstep;
}
pos nextPos(pos &curpos, int e)
{
if(e==1)
{
curpos.pos_x+=0;
curpos.pos_y+=1;
}
else if(e==2)
{
curpos.pos_x+=1;
curpos.pos_y+=0;
}
else if(e==3)
{
curpos.pos_x-=0;
curpos.pos_y-=1;
}
else if(e==4)
{
curpos.pos_x-=1;
curpos.pos_y-=0;
}
return curpos;
}
void markPrint(pos p)
{
miwu[p.pos_x][p.pos_y] = 1;
}
int game_ways(pos start,pos end,stack &s)
{
int i,way=1,tmp=0;
pos cur_node;
stk correct_path;
push_stack(s,start);
curstep++;
footPrint(start);
//miwu[start.pos_x][start.pos_y]=curstep;
cur_node=start;
//printf("-->%d\n",miwu[start.pos_x][start.pos_y]);
while(s.top>=0)
{
cur_node=top_stack(s);
if(cur_node.pos_x == end.pos_x && cur_node.pos_y == end.pos_y)
{
while(s.top >= 0)
{
pop_stack(s, correct_path->data[s.top]);
}
//correct_path->top = i;
return 0;
}
else
{
way=1;
while(way<=4)
{
if(way==1 && miwu[cur_node.pos_x][cur_node.pos_y+1]==0)
{
cur_node.pos_y+=1;
tmp=1;
break;
}
else if(way==1 && miwu[cur_node.pos_x+1][cur_node.pos_y]==0)
{
cur_node.pos_x+=1;
tmp=1;
break;
}
else if(way==1 && miwu[cur_node.pos_x][cur_node.pos_y-1]==0)
{
cur_node.pos_y-=1;
tmp=1;
break;
}
else if(way==1 && miwu[cur_node.pos_x-1][cur_node.pos_y+1]==0)
{
cur_node.pos_x-=1;
tmp=1;
break;
}
way++;
}
if(tmp==1)
{
curstep++;
footPrint(cur_node);
//printf("--+++++>%d\n",miwu[cur_node.pos_x][cur_node.pos_y]);
push_stack(s,cur_node);
tmp=0;
}
else
{
//printf("--+++++>%d\n",miwu[cur_node.pos_x][cur_node.pos_y]);
//curstep++;
//footPrint(cur_node);
cur_node=pop_stack(s, correct_path->data[s.top]);
}
}
}
return 0;
}
int main()
{
migong();
pos start={1,1};
pos end ={10,10};
stack s;
init_stack(s);
game_ways(start,end,s);
migong1();
migong2();
return 0;
}