迷宫问题 (c++)

 实验题目(共10题, 第3题)


标题: 迷宫问题
时 限: 100000 ms
内存限制: 100000 K
总时限: 300000 ms
描述:
迷宫问题
 
迷宫是一个二维矩阵,其中1为墙,0为路,3为入口,4为出口.要求从入口开始,从出口结束,按照 下,左,上,右 的顺序来搜索路径.
输入:
迷宫宽度w 迷宫高度h
迷宫第一行
迷宫第二行
...
迷宫第h 行
输出:
入口横坐标1  入口纵坐标1
横坐标2       纵坐标2
横坐标3       纵坐标3
横坐标4       纵坐标4
...
横坐标n-1    纵坐标n-1
出口横坐标n 出口纵坐标n
输入样例:
8 10
1 1 1 1 1 1 1 1
1 0 1 1 0 1 0 1
1 0 1 0 0 1 0 1
1 1 0 3 1 0 1 1
1 0 0 1 0 0 4 1
1 0 0 0 0 1 1 1
1 0 1 0 0 1 0 1
1 0 1 0 0 0 1 1
1 1 1 1 0 0 0 1
1 1 1 1 1 1 1 1
输出样例:
3 3
2 3
2 4
2 5
3 5
3 6
3 7
4 7
4 6
4 5
4 4
5 4
6 4
 
提示:

使用栈

参见教材 50 页

来源:
View Code
  1 #include<stdio.h>
2 #include<malloc.h>
3 #include<stdlib.h>
4
5 #define DOWN 1
6 #define LEFT 2
7 #define UP 3
8 #define RIGHT 4
9 #define INITSTACKSIZE 100
10 #define INCREMENTSIZE 10
11 #define MARK 5
12 #define BLIND_ALLEY 6
13
14 typedef int ** MazeType;
15 MazeType Maze;
16 int Maze_Line,Maze_Column;
17
18 typedef struct
19 {
20 int x;
21 int y;
22 } PosType,*PtrtoPos;
23 PtrtoPos start,end;
24
25 typedef struct
26 {
27 int ord;
28 PosType seat;
29 int direction;
30 } Record,*PtrtoRecord;
31
32 typedef struct
33 {
34 PtrtoRecord base;
35 int top;
36 int stacksize;
37 int length;
38 } SqStack,*PtrtoStack;
39
40 MazeType CreatMaze(void);
41 PtrtoStack MazePath(MazeType maze,PtrtoPos start,PtrtoPos end);
42 void Print_Path(PtrtoStack path);
43 PtrtoStack InitStack(void);
44 void Push(PtrtoStack ,PtrtoRecord );
45 void Pop(PtrtoStack ,PtrtoRecord );
46 void FootPrint(PtrtoPos );
47 void NextPos(PtrtoPos ,int );
48 void MarkPrint(PtrtoRecord );
49 int StackEmpty(PtrtoStack);
50 void DestroyStack(PtrtoStack);
51 int Pass(PtrtoPos);
52 void FreeMaze(MazeType);
53
54 int main()
55 {
56 PtrtoStack Path;
57 Maze=CreatMaze();
58 Path=MazePath(Maze,start,end);
59 Print_Path(Path);
60 DestroyStack(Path);
61 FreeMaze(Maze);
62
63 return 0;
64 }
65
66 MazeType CreatMaze(void)
67 {
68 start=(PtrtoPos)malloc(sizeof(PosType));
69 end=(PtrtoPos)malloc(sizeof(PosType));
70 int i,x,y;
71 scanf("%d%d",&Maze_Column,&Maze_Line);
72 MazeType maze;
73 maze=(MazeType)malloc(sizeof(int *)*Maze_Line);
74 for(i=0; i<Maze_Line; i++)
75 maze[i]=(int *)malloc(sizeof(int)*Maze_Column);
76 for(y=0; y<Maze_Line; y++)
77 for(x=0; x<Maze_Column; x++)
78 {
79 scanf("%d",&maze[y][x]);
80 if(maze[y][x]==3)
81 {
82 start->x=x;
83 start->y=y;
84 }
85 if(maze[y][x]==4)
86 {
87 end->x=x;
88 end->y=y;
89 }
90 }
91
92 return maze;
93 }
94
95 PtrtoStack MazePath(MazeType Maze,PtrtoPos start,PtrtoPos end)
96 {
97 int curstep=1;
98 PtrtoPos curpos;
99 PtrtoRecord e;
100 PtrtoStack Path;
101
102 curpos=(PtrtoPos)malloc(sizeof(PosType));
103 e=(PtrtoRecord)malloc(sizeof(Record));
104
105 curpos->x=start->x;
106 curpos->y=start->y;
107
108 Path=InitStack();
109 do
110 {
111 if(Pass(curpos)) //当前位置可以通过
112 {
113 FootPrint(curpos); //留下足迹
114 e->ord=curstep;
115 e->seat.x=curpos->x;
116 e->seat.y=curpos->y;
117 e->direction=DOWN;
118 Push(Path,e); //加入路径
119 if(curpos->x==end->x&&curpos->y==end->y)
120 return Path; //到达出口
121 NextPos(curpos,DOWN); //下一位置变为当前位置
122 curstep++;
123 }
124 else //当前位置不可通
125 {
126 if(!StackEmpty(Path))
127 {
128 Pop(Path,e);
129 while(e->direction==RIGHT&&!StackEmpty(Path))
130 {
131 MarkPrint(e);
132 Pop(Path,e);
133 }
134 if(e->direction<RIGHT)
135 {
136 e->direction++;
137 Push(Path,e);
138 curpos->x=Path->base[Path->top-1].seat.x;
139 curpos->y=Path->base[Path->top-1].seat.y;
140 NextPos(curpos,e->direction);
141 }
142 }
143 }
144 }
145 while(!StackEmpty(Path));
146
147 return 0;
148 }
149
150 PtrtoStack InitStack(void)
151 {
152 PtrtoStack p;
153 p=(PtrtoStack)malloc(sizeof(SqStack));
154 p->base=(PtrtoRecord)malloc(sizeof(Record)*INITSTACKSIZE);
155 p->top=0;
156 p->stacksize=INITSTACKSIZE;
157 p->length=0;
158 return p;
159 }
160 void Push(PtrtoStack Path,PtrtoRecord e)
161 {
162 if(Path->length>=Path->stacksize)
163 {
164 Path->base=(PtrtoRecord)realloc(Path->base,sizeof(Record)*(Path->stacksize+INCREMENTSIZE));
165 Path->stacksize+=INCREMENTSIZE;
166 if(!(Path->base))
167 {
168 printf("error\n");
169 exit(0);
170 }
171 }
172 Path->base[Path->top].ord=e->ord;
173 Path->base[Path->top].seat.x=e->seat.x;
174 Path->base[Path->top].seat.y=e->seat.y;
175 Path->base[Path->top].direction=e->direction;
176 Path->top++;
177 Path->length++;
178 }
179 void Pop(PtrtoStack Path,PtrtoRecord e)
180 {
181 if(StackEmpty(Path))
182 {
183 printf("Empty Stack\n");
184 exit(0);
185 }
186 Path->top--;
187 e->ord=Path->base[Path->top].ord;
188 e->seat.x=Path->base[Path->top].seat.x;
189 e->seat.y=Path->base[Path->top].seat.y;
190 e->direction=Path->base[Path->top].direction;
191 }
192
193 void FootPrint(PtrtoPos point)
194 {
195 Maze[point->y][point->x]=MARK;
196 }
197
198 void NextPos(PtrtoPos curpos,int direction)
199 {
200 switch(direction)
201 {
202 case DOWN :
203 curpos->y++;
204 break;
205 case LEFT :
206 curpos->x--;
207 break;
208 case UP :
209 curpos->y--;
210 break;
211 case RIGHT :
212 curpos->x++;
213 break;
214 }
215 }
216
217 void MarkPrint(PtrtoRecord e)
218 {
219 Maze[e->seat.y][e->seat.x]=BLIND_ALLEY;
220 }
221
222 int Pass(PtrtoPos point)
223 {
224 if(Maze[point->y][point->x]==0||Maze[point->y][point->x]==3||Maze[point->y][point->x]==4)
225 return 1;
226 else
227 return 0;
228 }
229
230 void Print_Path(PtrtoStack Path)
231 {
232 if(Path==NULL)
233 {
234 printf("no path\n");
235 exit(0);
236 }
237 int i=0;
238 for(; i<Path->top; i++)
239 printf("%d %d\n",Path->base[i].seat.x,Path->base[i].seat.y);
240 }
241
242 void DestroyStack(PtrtoStack Path)
243 {
244 free(Path->base);
245 free(Path);
246 }
247
248 int StackEmpty(PtrtoStack Path)
249 {
250 if(Path->top==0)
251 return 1;
252 else
253 return 0;
254 }
255
256 void FreeMaze(MazeType Maze)
257 {
258 int i;
259 for(i=0;i<Maze_Line;i++)
260 free(Maze[i]);
261 free(Maze);
262 }
posted @ 2011-05-24 17:52  itbird  Views(3308)  Comments(3Edit  收藏  举报