本篇文章中所有數據結構都是后期整理的,如有問題歡迎指正,轉載請注明出處http://www.cnblogs.com/a1982467767/p/8889583.html
求解迷宮問題
1,問題描述
以一個m*n的長方陣表示迷宮,0和1分別表示迷宮中的通路和障礙。迷宮問題要求求出從入口(1,1)到出口(m,n)的一條通路,或得出沒有通路的結論。 基本要求: 首先實現一個以鏈表作存儲結構的棧類型,然后編寫一個求迷宮問題的非遞歸程序,求得的通路,其中:(i,j)指示迷宮中的一個坐標, d表示走到下一坐標的方向。 左上角(1,1)為入口,右下角(m,n)為出口。
2.設計思路:
用棧實現迷宮問題的求解;
3.實驗代碼:
棧實現迷宮求解問題:
************************************************************************************************************
1 //maze_stack.cpp 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<windows.h> 5 #include"seqstack.h" 6 7 #define MAX_ROW 12 8 #define MAX_COL 14 9 10 int maze[12][14] = { 11 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 13 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 14 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 15 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 16 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 17 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 18 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 19 1, 0, 0, 0, 0, 1 ,0 ,0, 0 ,0 ,1 ,0 ,1 ,1, 20 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 21 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 22 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 23 }; 24 25 void print_line() 26 { 27 system("cls"); 28 printf("迷宮如下‘■’代表牆,數字 或者‘☆’表示路徑\n"); 29 int i, j; 30 for (i = 0; i < MAX_ROW; i++){ 31 for (j = 0; j < MAX_COL; j++) 32 if (maze[i][j] == 1) printf("■"); 33 else if (maze[i][j] >= 3){ 34 printf("%2d", maze[i][j] - 2); 35 /*if (i == MAX_ROW-2 && j == MAX_COL-2) printf("★"); 36 else printf("☆");*/ 37 } 38 else printf(" "); 39 printf("\n"); 40 } 41 printf("已到達出口...\n"); 42 printf("可見使用棧求出的路徑並非最優路徑,根據我依次探索的方向不同,結果也將不同\n"); 43 } 44 45 void visit(mark p,int sign, PSeqStack S) 46 { 47 Push_SeqStack(S,p); 48 switch (sign) 49 { 50 case 1: p.col++; Push_SeqStack(S, p); maze[p.row][p.col] = 2; break;//向右 51 case 2: p.row++; Push_SeqStack(S, p); maze[p.row][p.col] = 2; break;//向下 52 case 3: p.col--; Push_SeqStack(S, p); maze[p.row][p.col] = 2; break;//向左 53 case 4: p.row--; Push_SeqStack(S, p); maze[p.row][p.col] = 2; break;//向上 54 } 55 } 56 57 int main() 58 { 59 struct point p = { 1, 1 }; 60 maze[p.row][p.col] = 2;//遍歷過的點設置為2 61 PSeqStack S = Init_SeqStack(); 62 Push_SeqStack(S,p); 63 while (!Empty_SeqStack(S)) 64 { 65 Pop_SeqStack(S, &p); 66 if (p.row == MAX_ROW - 2 && p.col == MAX_COL - 2) 67 break; 68 if (p.col + 1 < MAX_COL - 1 && maze[p.row][p.col + 1] == 0)//向右 69 { 70 visit(p, 1, S); 71 continue; 72 } 73 if (p.row + 1 < MAX_ROW - 1 && maze[p.row + 1][p.col] == 0)//向下 74 { 75 visit(p, 2, S); 76 continue; 77 } 78 if (p.col - 1 >= 1 && maze[p.row][p.col - 1] == 0)//向左 79 { 80 visit(p, 3, S); 81 continue; 82 } 83 if (p.row - 1 >= 1 && maze[p.row - 1][p.col] == 0)//向上 84 { 85 visit(p, 4, S); 86 continue; 87 }//以上是對迷宮的四個方向進行操作 88 } 89 if (p.row == MAX_ROW - 2 && p.col == MAX_COL - 2)//是否為出口 90 { 91 int count = GetLength_SeqStack(S)+3;//為了與迷宮0,1,2的區別所以基數要以3開始 92 printf("成功找到出口,路徑倒序輸出:\n"); 93 printf("(%d,%d)\n", p.row, p.col); 94 maze[p.row][p.col] = count; 95 while (!Empty_SeqStack(S))//按照前驅進行查找 96 { 97 count--; 98 Pop_SeqStack(S, &p); 99 maze[p.row][p.col] = count; 100 printf("(%d,%d)\n", p.row, p.col); 101 } 102 printf("3秒后打印路徑......"); 103 Sleep(3000); 104 print_line(); 105 } 106 else { 107 printf("沒有出路\n"); 108 } 109 system("pause"); 110 return 0; 111 } 112 //end maze_stack.cpp
*************************************************************************************************************
1 //seqstack.h 2 #include<stdio.h> 3 #include<stdlib.h> 4 #define MAXSIZE 100 5 6 typedef struct point{ 7 int row, col; 8 }mark; 9 10 typedef mark DataType; 11 12 typedef struct { 13 DataType data[MAXSIZE]; 14 int top; 15 }SeqStack, * PSeqStack; 16 17 PSeqStack Init_SeqStack (void) 18 { 19 PSeqStack S; 20 S = (PSeqStack)malloc(sizeof(SeqStack)); 21 if (S) 22 S->top = -1; 23 else 24 exit(-1); 25 return S; 26 } 27 28 int Empty_SeqStack(PSeqStack S) 29 { 30 //return (S->top==-1); 31 if (S->top == -1) 32 return 1; 33 else 34 return 0; 35 } 36 37 int Push_SeqStack(PSeqStack S,DataType x) 38 { 39 if (S->top == MAXSIZE - 1) 40 { 41 printf("棧滿不能入棧\n"); 42 return 0; 43 } 44 else 45 { 46 S->top++; 47 S->data[S->top] = x; 48 return 1; 49 } 50 } 51 52 int Pop_SeqStack(PSeqStack S,DataType *x) 53 { 54 if(Empty_SeqStack(S)) 55 return 0; 56 else 57 { 58 *x = S->data[S->top]; 59 S->top--; 60 return 1; 61 } 62 } 63 64 int GetTop_SeqStack(PSeqStack S ,DataType *x) 65 { 66 if(Empty_SeqStack(S)) 67 return 0; 68 else 69 { 70 *x = S->data[S->top]; 71 return 1; 72 } 73 } 74 int GetLength_SeqStack(PSeqStack S) 75 { 76 return S->top + 1; 77 } 78 79 void Distory_SeqStack(PSeqStack *S) 80 { 81 if(*S) 82 free(*S); 83 *S = NULL; 84 }//end seqstack.h
4.運行結果:
棧求解迷宮: