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
|
#define SIZE 6/* MazeMap是迷宫的地图,数值1表示当前区域可行,数值0表示当前区域不可行,
nDestRow表示目的行,nDestColumn表示目的列,在这里没有出栈之前,没
查询栈是否为空,是一个bug,毕竟不是所有的迷宫都有出路
迷宫的入口大致从坐标(1,1)开始,大致的走向如下:
<3------>1
|
|
*/
void FindMazePath( int MazeMap[SIZE][SIZE], int nDestRow, int nDestColumn)
{ node* pVisit=(node*) malloc (100* sizeof (node));
memset (pVisit,0x00,100* sizeof (node));
//根节点进栈 入口坐标(1,1),排除零的干扰
int nBoundRow=SIZE-1;
int nBoundColumn=SIZE-1;
const int nEndRow=nDestRow;
const int nEndColumn=nDestColumn;
CStack stack;
//初始化节点进栈的时候,最后的进栈方向应该是0,而不是1,因为它可以走四个方向,如果填写1,意味着不可能往3的方向走
stack.PushStack(1,1,1,0);
StoreFoot(pVisit,1,1);
int nRow,nColumn,nDirections;
int nNowRow,nNowColumn;
int nLastDirections;
while (stack.IsStackNotEmpty())
{
stack.PopStack(nRow,nColumn,nDirections,nLastDirections);
cout<< "start to pop stack and check end:" <<nRow<< " " <<nColumn<< " " <<nDirections<<endl;
//如果已经到达出口,退出循环
if ((nRow==nEndRow)&&(nEndColumn==nColumn))
{
cout<< "Finish" <<endl;
break ;
}
switch (nDirections)
{
case 1: //向右 nColumn+1
nNowRow=nRow;
nNowColumn=nColumn+1;
if ((nNowColumn>nBoundColumn)||(3==nLastDirections))
{
nDirections++; //越界,找下一个方向,或者不允许往回走
cout<< "choose another directions" <<endl;
}
else
{
//判断当前的区域是否可通行,并且当前区域是没有走过的
if ((0!=MazeMap[nNowRow][nNowColumn])&&( false ==IsVisit(pVisit,nNowRow,nNowColumn)))
{
nDirections=1;
cout<< "Push Stack1:" <<nRow<< " " <<nColumn<<endl;
stack.PushStack(nRow,nColumn,1,nLastDirections);
stack.PushStack(nNowRow,nNowColumn,1,1);
cout<< "Push Stack1:" <<nNowRow<< " " <<nNowColumn<<endl;
StoreFoot(pVisit,nNowRow,nNowColumn);
break ; //退出循环
}
}
case 2: //
nNowRow=nRow+1;
nNowColumn=nColumn;
if ((nNowRow>nBoundRow)||(4==nLastDirections))
{
nDirections++; //越界,找下一个方向
}
else
{
//判断当前的区域是否可通行,并且当前区域是没有走过的
if ((0!=MazeMap[nNowRow][nNowColumn])&&( false ==IsVisit(pVisit,nNowRow,nNowColumn)))
{
cout<< "start to visit" <<endl;
nDirections=2;
cout<< "Push Stack:" <<nRow<< " " <<nColumn<<endl;
stack.PushStack(nRow,nColumn,2,nLastDirections);
stack.PushStack(nNowRow,nNowColumn,1,2);
cout<< "Push Stack:" <<nNowRow<< " " <<nNowColumn<<endl;
StoreFoot(pVisit,nNowRow,nNowColumn);
break ; //退出循环
}
}
case 3:
nNowColumn=nColumn-1;
nNowRow=nRow;
if ((nNowColumn<1)||(1==nLastDirections))
{
nDirections++;
}
else
{
//判断当前的区域是否可通行,并且当前区域是没有走过的
if ((0!=MazeMap[nNowRow][nNowColumn])&&( false ==IsVisit(pVisit,nNowRow,nNowColumn)))
{
nDirections=3;
cout<< "Push Stack:" <<nRow<< " " <<nColumn<<endl;
stack.PushStack(nRow,nColumn,2,nLastDirections);
stack.PushStack(nNowRow,nNowColumn,1,3);
cout<< "Push Stack:" <<nRow<< " " <<nColumn<<endl;
StoreFoot(pVisit,nNowRow,nNowColumn);
break ;
}
}
case 4:
nNowRow=nRow-1;
nNowColumn=nColumn;
if ((nNowRow<1)||2==nLastDirections)
{
stack.PopStack(nRow,nColumn,nDirections,nLastDirections);
}
else
{
//判断当前的区域是否可通行,并且当前区域是没有走过的
if ((0!=MazeMap[nNowRow][nNowColumn])&&( false ==IsVisit(pVisit,nNowRow,nNowColumn)))
{
nDirections=4;
cout<< "Push Stack:" <<nRow<< " " <<nColumn<<endl;
stack.PushStack(nRow,nColumn,4,nLastDirections);
stack.PushStack(nNowRow,nNowColumn,1,4);
cout<< "Push Stack:" <<nRow<< " " <<nColumn<<endl;
StoreFoot(pVisit,nNowRow,nNowColumn);
break ;
}
}
default :
break ;
}
}
while (stack.IsStackNotEmpty())
{
stack.PopStack(nRow,nColumn,nDirections,nLastDirections);
cout<< "nRow:" <<nRow<< " " << "nColumn:" <<nColumn<<endl;
}
} 如下是栈的定义: typedef struct SNode{
int nRow; //行
int nColumn; //列
int nDirections; //当前已经走过的方向
int nLastDirections; //上一个位置走到这个位置的方向,避免往回走
SNode* pNextNode;
}SNode; class CStack
{ private :
SNode* m_pCStackTop;
int m_nNodeCount;
public :
CStack();
~CStack();
//进栈
int PushStack( int nRow, int nColumn, int nDirections, int nLastDirections);
//获取栈顶元素
int GetTop( int & nRow, int & nColumn, int & nDirections, int nLastDirections);
//出栈
int PopStack( int & nRow, int & nColumn, int & nDirections, int nLastDirections);
int DisplayStack();
bool IsStackNotEmpty();
}; 如下是栈的实现: #include <stdio.h>#include <stdlib.h> #include <iostream> using namespace std;
#include "stack.h" CStack::CStack() { m_pCStackTop= new SNode;
if (NULL==m_pCStackTop)
{
exit (-1);
}
m_pCStackTop->pNextNode=NULL;
} CStack::~CStack() { SNode* pTmpNode=m_pCStackTop;
while (NULL!=pTmpNode)
{
m_pCStackTop=pTmpNode->pNextNode;
delete pTmpNode;
pTmpNode=m_pCStackTop;
}
} int CStack::PushStack( int nRow, int nColumn, int nDirections, int nLastDirections)
{ SNode* pNewNode= new SNode;
pNewNode->nColumn=nColumn;
pNewNode->nRow=nRow;
pNewNode->nDirections=nDirections;
pNewNode->nLastDirections=nLastDirections;
pNewNode->pNextNode=NULL;
if (NULL==pNewNode)
{
exit (-1);
}
pNewNode->pNextNode=m_pCStackTop->pNextNode;
m_pCStackTop->pNextNode=pNewNode;
return 0;
} int CStack::GetTop( int & nRow, int & nColumn, int & nDirections, int nLastDirections)
{ if (NULL==m_pCStackTop->pNextNode)
{
return -1;
}
nRow=m_pCStackTop->pNextNode->nRow;
nColumn=m_pCStackTop->pNextNode->nColumn;
nDirections=m_pCStackTop->pNextNode->nDirections;
nLastDirections=m_pCStackTop->pNextNode->nLastDirections;
return 0;
} int CStack::PopStack( int &nRow, int &nColumn, int &nDirections, int nLastDirections)
{ if (NULL==m_pCStackTop->pNextNode)
{
return -1;
}
nRow=m_pCStackTop->pNextNode->nRow;
nColumn=m_pCStackTop->pNextNode->nColumn;
nDirections=m_pCStackTop->pNextNode->nDirections;
nLastDirections=m_pCStackTop->pNextNode->nLastDirections;
SNode* pTmpNode=m_pCStackTop->pNextNode;
m_pCStackTop->pNextNode=pTmpNode->pNextNode;
delete pTmpNode;
return 0;
} int CStack::DisplayStack()
{ SNode* pTmpNode=m_pCStackTop->pNextNode;
while ((NULL!=pTmpNode))
{
cout<<pTmpNode->nColumn<<endl;
pTmpNode=pTmpNode->pNextNode;
}
} bool CStack::IsStackNotEmpty()
{ if (NULL!=m_pCStackTop->pNextNode)
{
return true ;
}
return false ;
} 保存遍历的足迹: typedef struct node{
int nRow;
int nColumn;
int nUsed;
}node; void StoreFoot(node* pVisit, int nRow, int nColumn)
{ node* pTmp=pVisit;
for ( int i=0;i<100;i++)
{
if ( false ==pTmp[i].nUsed)
{
pTmp[i].nColumn=nColumn;
pTmp[i].nRow=nRow;
pTmp[i].nUsed= true ;
return ;
}
}
cout<< "can not find enough room to search not use" <<endl;
} bool IsVisit(node* pVisit, int nRow, int nColumn)
{ node* pTmp=pVisit;
for ( int i=0;i<100;i++)
{
if ( true ==pTmp[i].nUsed)
{
if ((nRow==pTmp[i].nRow)&&(nColumn==pTmp[i].nColumn))
{
return true ;
}
}
}
return false ;
} 如下是简单构造的调用: int array[6][6]={0}; array[1][1]=1;
array[1][3]=1;
array[2][1]=1;
array[2][2]=1;
array[2][3]=1;
array[2][4]=1;
array[3][2]=1;
array[4][2]=1;
array[4][1]=1;
array[4][3]=1;
array[4][4]=1;
array[5][4]=1;
array[5][5]=1;
FindMazePath(array,5,4);
|
本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1583067,如需转载请自行联系原作者