-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo3-4.cpp
144 lines (131 loc) · 2.76 KB
/
algo3-4.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
// algo3-4.cpp 利用栈求解迷宫问题(只输出一个解,算法3.3)
#include"c1.h"
struct PosType
{ int x;
int y;
};
// 全局变量
PosType begin,end;
PosType direc[4]={{0,1},{1,0},{0,-1},{-1,0}};
// {行增量,列增量},移动方向依次为东南西北
#define MAXLENGTH 25
typedef int MazeType[MAXLENGTH][MAXLENGTH];
MazeType m;
int x,y;
void Print()
{
int i,j;
for(i=0;i<x;i++)
{ for(j=0;j<y;j++)
printf("%3d",m[i][j]);
printf("\n");
}
}
void Init()
{
int i,j,x1,y1;
printf("请输入迷宫的行数,列数(包括外墙):");
scanf("%d,%d",&x,&y);
for(i=0;i<y;i++)
{ m[0][i]=0;
m[x-1][i]=0;
}
for(i=1;i<x-1;i++)
{ m[i][0]=0;
m[i][y-1]=0;
}
for(i=1;i<x-1;i++)
for(j=1;j<y-1;j++)
m[i][j]=1;
printf("请输入迷宫内墙单元数:");
scanf("%d",&j);
printf("请依次输入迷宫内墙每个单元的行数,列数:\n");
for(i=1;i<=j;i++)
{ scanf("%d,%d",&x1,&y1);
m[x1][y1]=0;
}
printf("迷宫结构如下:\n");
Print();
printf("请输入入口的行数,列数:");
scanf("%d,%d",&begin.x,&begin.y);
printf("请输入出口的行数,列数:");
scanf("%d,%d",&end.x,&end.y);
}
int curstep=1;
struct SElemType
{ int ord;
PosType seat;
int di;
};
#include"c3-1.h"
#include"bo3-1.cpp"
// 定义墙元素值为0,可通过路径为1,经试探不可通过路径为-1,通过路径为足迹
Status Pass(PosType b)
{
if(m[b.x][b.y]==1)
return OK;
else
return ERROR;
}
void FootPrint(PosType b)
{
m[b.x][b.y]=curstep;
}
void NextPos(PosType &b,int di)
{
b.x+=direc[di].x;
b.y+=direc[di].y;
}
void MarkPrint(PosType b)
{
m[b.x][b.y]=-1;
}
Status MazePath(PosType start,PosType end)
{
PosType curpos=start;
SqStack S;
SElemType e;
InitStack(S);
do
{ if(Pass(curpos))
{ FootPrint(curpos);
e.ord=curstep;
e.seat=curpos;
e.di=0;
Push(S,e);
curstep++;
if(curpos.x==end.x&&curpos.y==end.y)
return TRUE;
NextPos(curpos,e.di);
}
else
{ if(!StackEmpty(S))
{ Pop(S,e);
curstep--;
while(e.di==3&&!StackEmpty(S))
{ MarkPrint(e.seat);
Pop(S,e);
curstep--;
}
if(e.di<3)
{ e.di++;
Push(S,e);
curstep++;
curpos=e.seat;
NextPos(curpos,e.di);
}
}
}
}while(!StackEmpty(S));
return FALSE;
}
void main()
{
Init();
if(MazePath(begin,end))
{ printf("此迷宫从入口到出口的一条路径如下:\n");
Print();
}
else
printf("此迷宫没有从入口到出口的路径\n");
}