迷宫源码
1 #include <stdio.h>
2 #include<stdlib.h>
3 #include"stack.h"
4 #define ROW 6
5 #define LOW 6
6
7 typedef struct Maze
8 {
9 int map[ROW][LOW];
10 }Maze;
11
12 void mazeinit(Maze *maze)
13 {
14 int map[ROW][LOW]={
15 {0,1,0,1,0,0},
16 {0,1,1,1,0,0},
17 {1,1,0,1,0,0},
18 {0,1,0,1,1,1},
19 {0,1,0,1,0,1},
20 {0,1,0,0,0,0},
21 };
22 int i,j;
23 for(i=0;i<ROW;i++)
24 for(j=0;j<LOW;j++)
25 {
26 maze->map[i][j]=map[i][j];
27 }
28 }
29 void print(Maze *maze)
30 {
31 int i,j;
32 for(i=0;i<ROW;i++)
33 {
34 for(j=0;j<LOW;j++)
35 {
36 printf("%2d ",maze->map[i][j]);
37 }
38 printf("\n");
39 }
40 }
41 int canmove(Maze *maze,point p)
42 {
43 if(p.row<0||p.low>ROW-1||p.low<0||p.low>LOW-1)
44 return 0;
45 if(maze->map[p.row][p.low]==1){
46 return 1;
47 }
48 return 0;//zhe ge da keng
49 }
50 void makemark(Maze *maze,point p)
51 {
52 maze->map[p.row][p.low]=2;
53 }
54 int isexitt(Maze *maze,point p,point entry)
55 {
56 if(p.row == entry.row && p.low == entry.low)
57 return 0;
58 if(p.low == LOW-1 || p.low == 0 || p.row == 0 || p.row == ROW-1)
59 {
60 printf("找到一条出路!\n");
61 return 1;
62 }
63 return 0;//zhe ge keng
64 }
65 void fuzhi(Stack *from,Stack *to)
66 {
67 stackdostroy(to);
68 int i;
69 to->data=(stackType*)malloc(from->size*sizeof(stackType));
70 to->size=from->size;
71 for(i=0;i<from->size;i++)
72 {
73 to->data[i]=from->data[i];
74 }
75 }
76 void _getpath(Maze *maze,Stack *cur,Stack *_short,point p,point entry)
77 {
78 //printf("(%d %d)",p.row,p.low);
79 //判断当前点能不能落脚
80 if(!canmove(maze,p))
81 return;
82 //如果能落脚,就对当前点标记,并入栈
83 makemark(maze,p);
84 stackpush(cur,p);
85 //判断当前点是不是是不是出口点
86 if(isexitt(maze,p,entry))
87 {
88 //如果有比上一次出口更短的路径就更新_short栈的内容
89 if(cur->size < _short->size || _short->size == 0)
90 {
91 fuzhi(cur,_short);
92 printf("gengduande\n");
93 }
94 //如果找的出口没有比上次更短,就要回溯,cur栈也要出栈
95 stackpop(cur);
96 return;
97 }
98 //四个方向探测
99 point up=p;
100 up.row -=1;
101 _getpath(maze,cur,_short,up,entry);
102
103 point right=p;
104 right.low +=1;
105 _getpath(maze,cur,_short,right,entry);
106
107 point down=p;
108 down.row +=1;
109 _getpath(maze,cur,_short,down,entry);
110
111 point left=p;
112 left.low -=1;
113 _getpath(maze,cur,_short,left,entry);
114 //如果四个方向都不行,就函数栈帧出栈和cur出栈
115 stackpop(cur);
116 return ;
117 }
118
119 int main()
120 {
121 Stack cur_stack;
122 Stack short_stack;
123 stackinit(&cur_stack);
124 stackinit(&short_stack);
125 point entry={0,1};
126 Maze maze;
127 mazeinit(&maze);
128 print(&maze);
129 _getpath(&maze,&cur_stack,&short_stack,entry,entry);
130 printf("\n");
131 print(&maze);
132 return 0;
133 }
栈的头文件
1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef struct point
4 {
5 int row;
6 int low;
7 }point;
8 typedef point stackType;
9 #define MAX_SIZE 100
10 typedef struct StackNode
11 {
12 stackType *data;
13 int size;
14 }Stack,stacknode;
15 void stackinit(Stack *stack);
16 void resize(Stack *stack);
17 void stackdostroy(Stack *stack);
18 void stackpush(Stack *stack,stackType value);
19 void stackpop(Stack *stack);
20 void 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 void stacktop(Stack *stack,stackType *value)
56 {
57 if (stack==NULL) {
58 return;
59 }
60 *value= stack->data[stack->size-1];
61 }
Makefile
1 .PHONY:clean
2 maze_short:maze_short.c stack.c
3 gcc -g -o $@ $^
4 clean:
5 rm -f maze_short
~