Stack.h #ifndef STACK_H_H #define OK 0 #define ERROR -1 typedef struct Point { int x; int y; }Point; typedef struct Stack { Point pt; //保存坐标点 struct Stack *next; }*LinkStack; int init_linkStack(LinkStack*); int make_node(Stack**); int clear_linkStack(LinkStack); int destroy_linkStack(LinkStack*); int push(LinkStack, Point); int pop(LinkStack, Point*); int create_linkStack(LinkStack); int print_linkStack(LinkStack); int get_top(LinkStack, Stack**); int stack_empty(LinkStack); #endif Stack.cpp #include<stdio.h> #include<stdlib.h> #include "Stack.h" int init_linkStack(LinkStack *lstack) { if(!((*lstack) = (LinkStack)malloc(sizeof(Stack)))) return ERROR; (*lstack)->pt.x = 0; (*lstack)->pt.y = 0; (*lstack)->next = NULL; return OK; } int make_node(Stack **node) { if(!((*node) = (LinkStack)malloc(sizeof(Stack)))) return ERROR; (*node)->pt.x = 0; (*node)->pt.y = 0; return OK; } int clear_linkStack(LinkStack lstack) { if(lstack == NULL || lstack->next == NULL) return ERROR; Stack *tmp = lstack->next; Stack *tmp1; while(tmp != NULL) { tmp1 = tmp; tmp = tmp->next; free(tmp1); } lstack->next = NULL; return OK; } int destroy_linkStack(LinkStack *lstack) { if((*lstack) == NULL) return ERROR; Stack *tmp; while((*lstack) != NULL) { tmp = *lstack; (*lstack) = (*lstack)->next; free(tmp); } (*lstack) = NULL; return OK; } int push(LinkStack lstack, Point pt) { if(lstack == NULL) return ERROR; Stack *node; if(make_node(&node)) return ERROR; node->pt = pt; node->next = lstack->next; lstack->next = node; return OK; } int pop(LinkStack lstack, Point *pt) { if(lstack == NULL || lstack->next == NULL) return ERROR; Stack *tmp; *pt = lstack->next->pt; tmp = lstack->next; lstack->next = tmp->next; free(tmp); return OK; } int create_linkStack(LinkStack lstack) { if(lstack == NULL || lstack->next != NULL) return ERROR; Point pt; printf("input x, y: "); scanf("%d,%d",&pt.x, &pt.y); while(0 != pt.x && 0 != pt.y) { push(lstack,pt); scanf("%d,%d",&pt.x, &pt.y); } return OK; } int print_linkStack(LinkStack lstack) { if(lstack == NULL || lstack->next == NULL) return ERROR; Stack *tmp = lstack->next; while(tmp != NULL) { printf("(%d,%d), ",tmp->pt.x, tmp->pt.y); tmp = tmp->next; } printf("/n"); return OK; } int get_top(LinkStack lstack, Stack **node) { if(lstack == NULL || lstack->next == NULL) return ERROR; *node = lstack->next; return OK; } int stack_empty(LinkStack lstack) { if(lstack == NULL) return ERROR; if(lstack->next == NULL) return true; return false; } mg.h #ifndef MG_H_H #define MAX_SIZE 10 #define OK 0 #define ERROR -1 int create_map(int map[MAX_SIZE][MAX_SIZE], int); int print_map(int map[MAX_SIZE][MAX_SIZE]); int maze_path(int map[MAX_SIZE][MAX_SIZE], Point, Point); #endif mg.cpp #include<stdlib.h> #include<stdio.h> #include<time.h> #include "Stack.h" #include"mg.h" int create_map(int map[MAX_SIZE][MAX_SIZE], int num) { //初始化地图 for(int i = 0 ; i < MAX_SIZE ; i++) { for(int j = 0 ; j < MAX_SIZE ; j++) { if(j == 0 || i == 0 || j == MAX_SIZE - 1 || i == MAX_SIZE - 1) { map[i][j] = 1; } else map[i][j] = 0; } } //产生地图障碍 srand(time(0)); while(num > 0) { int m = rand() % MAX_SIZE; int n = rand() % MAX_SIZE; map[m][n] = 1; num--; } return OK; } int print_map(int map[MAX_SIZE][MAX_SIZE]) { for(int i = 0 ; i < MAX_SIZE ; i++) { for(int j = 0 ; j < MAX_SIZE ; j++) printf("%d ",map[i][j]); printf("/n"); } return OK; } int maze_path(int map[MAX_SIZE][MAX_SIZE], Point start, Point end) { LinkStack lstack; Stack *node; init_linkStack(&lstack); Point people = start; push(lstack,people); do { //行走的路径放在栈中 if(people.x == end.x && people.y == end.y) break; if(map[people.x][people.y + 1] != 1) { people.y += 1; push(lstack,people); //走过的路径做下标记 map[people.x][people.y] = 1; } else if(map[people.x + 1][people.y] != 1) { people.x += 1; push(lstack,people); map[people.x][people.y] = 1; } else if(map[people.x][people.y - 1] != 1) { people.y -= 1; push(lstack,people); map[people.x][people.y] = 1; } else if(map[people.x - 1][people.y] != 1) { people.x -= 1; push(lstack,people); map[people.x][people.y] = 1; } else { //没有路走,就回退 pop(lstack,&people); } }while(!stack_empty(lstack)); if(!stack_empty(lstack)) { while(!stack_empty(lstack)) { pop(lstack,&people); printf("(%d,%d) ", people.x, people.y); } printf("/n"); } else { printf("not found the outlet./n"); } return OK; } Main.cpp #include<stdio.h> #include<stdlib.h> #include "Stack.h" #include "mg.h" void main() { int map[MAX_SIZE][MAX_SIZE]; Point start; Point end; create_map(map,30); print_map(map); printf("input start point (x,y): "); scanf("%d,%d", &start.x, &start.y); printf("input end point (x,y): "); scanf("%d,%d", &end.x, &end.y); maze_path(map, start, end); }