-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-3.c
More file actions
134 lines (125 loc) · 2.04 KB
/
3-3.c
File metadata and controls
134 lines (125 loc) · 2.04 KB
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
#define M 8
#define N 10
#define MAXS 100
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define OK 1
#define ERROR 0
typedef int Status;
typedef struct{
int x,y;
}PosType;
typedef char MazeType[M+2][N+2];
typedef struct {
int i;
int j;
int di;
}SElemType;
typedef struct
{
SElemType st[MAXS];
int top;
}Stack;
Status MazePath(MazeType maze,Stack *S,PosType start,PosType end)
{
int i,j,di,flag;
S->top++;
S->st[S->top].i=start.x;
S->st[S->top].j=start.y;
S->st[S->top].di=-1;
maze[start.x][start.y]=-1;
while(S->top>-1)
{
i=S->st[S->top].i;
j=S->st[S->top].j;
di=S->st[S->top].di;
if(i==end.x&&j==end.y)
return OK;
flag=0;
while(di<4&&flag==0)
{
di++;
switch(di)
{
case 0:
i=S->st[S->top].i+1;
j=S->st[S->top].j;
break;
case 1:
i=S->st[S->top].i;
j=S->st[S->top].j+1;
break;
case 2:
i=S->st[S->top].i-1;
j=S->st[S->top].j;
break;
case 3:
i=S->st[S->top].i;
j=S->st[S->top].j-1;
break;
}
if(maze[i][j]==0)
flag=1;
}
if(flag==1)
{
S->st[S->top].di=di;
S->top++;
S->st[S->top].i=i;
S->st[S->top].j=j;
S->st[S->top].di=-1;
maze[i][j]=-1;
}
else
{
maze[S->st[S->top].i][S->st[S->top].j]=0;
S->top--;
}
}
return ERROR;
}
void main()
{
MazeType maze;
int i,j;
PosType start={1,1},end={M,N};
Stack S;
S.top=-1;
while(1)
{
srand((unsigned)time(NULL));
for(i=1;i<=M;i++)
for(j=1;j<=N;j++)
{
maze[i][j]=rand()%2;//用随机数生成的迷宫
}
for(i=0;i<=M+1;i++)
maze[i][0]=maze[i][N+1]='1';//初始化迷宫外围的最上和最下的2行
for(i=1;i<=N;i++)
maze[i][0]=maze[i][N+1]='1';//初始化迷宫左右除上下的另外部分
maze[1][1]=maze[M][N]=0;
for(i=1;i<=M;i++)
{
for(j=1;j<=N;j++)
printf("%3d",maze[i][j]);
printf("\n");
}
if(MazePath(maze,&S,start,end))
{
printf("迷宫路径如下:\n");
for(i=0;i<=S.top;i++)
{
printf("(%d,%d)",S.st[i].i,S.st[i].j);
if((i+1)%6==0)
printf("\n");
}
printf("\n");
break;
}
else
{
printf("无路径!\n");
}
}
}