迷宫源代码
1 #include <stdio.h>
2 #include"stack.h"
3 #define ROW 6
4 #define LOW 6
5
6 //typedef struct point
7 //{
8 // int row;
9 // int low;
10 //}point;
11
12 typedef struct maze
13 {
14 int map[ROW][LOW];
15 }Maze;
16
17 void mazeinit(Maze *maze)
18 {
19 int map[ROW][LOW]={
20 {0,1,0,0,0,0},
21 {0,1,1,1,0,0},
22 {0,1,0,1,0,0},
23 {0,1,0,1,1,0},
24 {0,1,0,0,0,0},
25 {0,1,0,0,0,0},
26 };
27 int i,j;
28 for(i=0;i<ROW;i++)
29 for(j=0;j<LOW;j++)
30 {
31 maze->map[i][j]=map[i][j];
32 }
33 }
34 void print(Maze *maze)
35 {
36 int i,j;
37 for(i=0;i<ROW;i++)
38 {
39 for(j=0;j<LOW;j++)
40 {
41 printf("%2d ",maze->map[i][j]);
42 }
43 printf("\n");
44 }
45 }
46 int canmove(Maze *maze,point cur)
47 {
48 if(cur.row<0||cur.row>ROW-1||cur.low<0||cur.low>LOW-1)
49 return 0;
50 if(maze->map[cur.row][cur.low]==1)
51 return 1;
52 return 0;
53 }
54 void makemark(Maze *maze,point cur)
55 {
56 maze->map[cur.row][cur.low]=2;
57 }
58 int isexitt(Maze *maze,Stack *s,point cur,point entry)
59 {
60 if(cur.row==entry.row && cur.low==entry.low)
61 return 0;
62 if(cur.row==ROW-1||cur.low==LOW-1||cur.row==0||cur.low==0)
63 {
64 printf("zhaodaoyitiaochulu\n");
65 return 0;
66 }
67 return 0;
68 }
69 void _getpath(Maze *maze,Stack *s,point cur,point entry)
70 {
71 //非法输入
72 if(maze==NULL)
73 return;
74 //如果入口点不能走,就非法点
75 if(!canmove(maze,cur))
76 return ;
77 //如果能走做一个标记,并且入栈
78 makemark(maze,cur);
79 stackpush(s,cur);
80 //如果栈不为空取栈顶元素,已该点为基准进行判断
81 while(stacktop(s,&cur))
82 {
83 //如果该点为出口点,找到一条路径
84 if(isexitt(maze,s,cur,entry))
85 {
86 return;
87 }
88 //如果该点不是出口点,则依次进行上左下右判断
89 point up=cur;
90 up.row -=1;
91 if(canmove(maze,up))
92 {
93 makemark(maze,cur);
94 stackpush(s,up);
95 continue;
96 }
97
98
99 point right=cur;
100 right.low +=1;
101 if(canmove(maze,right))
102 {
103 makemark(maze,right);
104 stackpush(s,right);
105 continue;
106 }
107
108 point down=cur;
109 down.row +=1;
110 if(canmove(maze,down))
111 {
112 makemark(maze,down);
113 stackpush(s,down);
114 continue;
115 }
116
117
118 point left=cur;
119 left.low -=1;
120 if(canmove(maze,left))
121 {
122 makemark(maze,left);
123 stackpush(s,left);
124 continue;
125 }
126 //如果没有路可以走,则出栈,继续探测
127 stackpop(s);
128 }
129 }
130 int main()
131 {
132 Stack s;
133 stackinit(&s);
134 point entry={0,1};
135 Maze maze;
136 mazeinit(&maze);
137 _getpath(&maze,&s,entry,entry);
138 print(&maze);
139 return 0;
140 }
栈头文件
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define MAX_SIZE 1
4 typedef struct point{
5 int row;
6 int low;
7 }point;
8
9 typedef point stackType;
10 typedef struct StackNode
11 {
12 stackType *data;
13 int size;
14 }Stack,stacknode;
15
16 void stackinit(Stack *stack);
17 void resize(Stack *stack);
18 void stackdostory(Stack *stack);
19 void stackpush(Stack *stack,stackType value);
20 void stackpop(Stack *stack);
21 int stacktop(Stack *stack,stackType *value);
栈源文件
1 #include "stack.h"
2
3 void stackinit(Stack *stack)
4 {
5 if(stack==NULL)
6 return;
7 stack->data=(stackType*)malloc(MAX_SIZE*sizeof(stackType));
8 if(stack==NULL)
9 {
10 exit(1);
11 }
12 stack->size=0;
13 }
14 void stackdostroy(Stack *stack)
15 {
16 if(stack==NULL)
17 return;
18 free(stack->data);
19 stack->size=0;
20 }
21 void resize(Stack *stack)
22 {
23 Stack new;
24 if(stack==NULL)
25 return;
26 if(stack->size<MAX_SIZE)
27 return ;
28 // stack->capacity = capacity*2+1;
29 (&new)->data = (stackType*)malloc((MAX_SIZE+MAX_SIZE)*sizeof(stackType));
30 int n=stack->size;
31 int i=0;
32 while(n--)
33 {
34 (&new)->data[i++]=stack->data[i++];
35 }
36 stack = &new;
37 }
38 void stackpush(Stack *stack,stackType value)
39 {
40 if(stack==NULL)
41 return ;
42 if(stack->size>=MAX_SIZE)
43 {
44 resize(stack);
45 }
46 stack->data[stack->size++]=value;
47 }
48 void stackpop(Stack *stack)
49 {
50 if(stack==NULL)
51 return;
52 stack->size--;
53
54 }
55 int stacktop(Stack *stack,stackType *value)
56 {
57 if (stack==NULL) {
58 return;
59 }
60 if(stack->size==0)
61 return 0;
62 *value= stack->data[stack->size-1];
63 return 1;
64 }
makefile
1 .PHONY:clean
2 maze_stack:maze_stack.c stack.c
3 gcc -g -o $@ $^
4 clean:
5 rm -f maze_stack
~