近期做了一道經典的 C 語言迷宮求解題,把思路與實現分享出來。
迷宮布局用二維數組存放,1 表示牆(不可通行),0 表示通路,最終打印出通行走過的順序。
//0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
const int mizu[10][10] = {
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1, //0
1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1, //1
1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1, //2
1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1, //3
1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1, //4
1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1, //5
1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1, //6
1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1, //7
1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1, //8
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 //9
};
1、利用 C 語言的棧。起點為 (1,1),若該點可走通,就把坐標入棧,並在標記數組中標記為「已走過」。
2、以當前坐標 (x,y) 為中心,按上、右、下、左的順時針次序判斷四鄰是否可走。可走的條件是:該位置值為 0,且尚未走過。找到下一站即入棧。
3、若四個方向都走不通,則當前坐標出棧(回溯)。
4、棧非空則繼續搜索;棧空則說明全盤無路,結束。
5、一旦走到出口坐標,即找到可行通路。
這本質上是深度優先搜索,棧在這裡承擔了「記錄路徑並支持回溯」的職責。
#include <stdio.h>
#include <stdlib.h>
const int mizu[10][10] = {
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1,
1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1,
1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1,
1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1,
1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1,
1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1,
1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1,
1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1,
1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1
};
int flag[10][10] = {0};
typedef int TypeData;
typedef struct node{
TypeData datax;
TypeData datay;
struct node* next;
}Node, *LinkList;
typedef struct stack{
LinkList top;
}STACK;
int stackInit(STACK* s);
int pushStack(STACK* s, TypeData x, TypeData y);
void popStack(STACK* s, TypeData* x, TypeData* y);
int isStackEmpty(STACK* s);
int mizePath(STACK* s, TypeData end_x, TypeData end_y);
int passMizu(TypeData x, TypeData y);
int printStackData(STACK s);
/* 鏈棧初始化 */
int stackInit(STACK* s)
{
LinkList p = (LinkList)malloc(sizeof(Node));
if(!p) return -1;
p->next = NULL;
p->datax = -1;
p->datay = -1;
s->top = p;
return 1;
}
/* 入棧 */
int pushStack(STACK* s, TypeData x, TypeData y)
{
LinkList p = (LinkList)malloc(sizeof(Node));
if(!p) return -1;
p->datax = x;
p->datay = y;
p->next = s->top;
s->top = p;
return 1;
}
/* 出棧 */
void popStack(STACK* s, TypeData* x, TypeData* y)
{
LinkList p = s->top;
s->top = p->next;
(*x) = p->datax;
(*y) = p->datay;
free(p);
}
/* 1 空 0 不空 */
int isStackEmpty(STACK* s)
{
if(s->top->datax == -1)
return 1;
return 0;
}
/* 尋路:上、右、下、左 */
int mizePath(STACK* s, TypeData end_x, TypeData end_y)
{
pushStack(s, 1, 1);
TypeData nowx = 1, nowy = 1;
flag[nowx][nowy] = 1;
while(!isStackEmpty(s))
{
if((nowx == end_x) && (nowy == end_y))
return 1;
if(passMizu(nowx, nowy-1)) // 上
{
nowy = nowy - 1;
pushStack(s, nowx, nowy);
flag[nowy][nowx] = 1;
}
else if(passMizu(nowx+1, nowy)) // 右
{
nowx = nowx + 1;
pushStack(s, nowx, nowy);
flag[nowy][nowx] = 1;
}
else if(passMizu(nowx, nowy+1)) // 下
{
nowy = nowy + 1;
pushStack(s, nowx, nowy);
flag[nowy][nowx] = 1;
}
else if(passMizu(nowx-1, nowy)) // 左
{
nowx = nowx - 1;
pushStack(s, nowx, nowy);
flag[nowy][nowx] = 1;
}
else // 四方向不通,回溯
{
popStack(s, &nowx, &nowy);
}
}
return 0;
}
/* 判斷該位置是否可通 */
int passMizu(TypeData x, TypeData y)
{
if((x > 9)||(y > 9)) return 0;
if(mizu[y][x]) return 0;
if(flag[y][x]) return 0;
return 1;
}
/* 打印棧中坐標(借助臨時棧倒置) */
int printStackData(STACK s)
{
STACK temp;
stackInit(&temp);
if(s.top->datax == -1) return 0;
while(s.top->datax != -1)
{
pushStack(&temp, s.top->datax, s.top->datay);
s.top = s.top->next;
}
while(temp.top->datax != -1)
{
printf("(%d,%d)\n", temp.top->datax, temp.top->datay);
temp.top = temp.top->next;
}
return 1;
}
int main()
{
STACK mi_stack;
stackInit(&mi_stack);
if(mizePath(&mi_stack, 8, 8))
{
printStackData(mi_stack); // 按行走順序打印
}
else printf("沒有通路!\n");
return 0;
}
幾處值得注意的細節:passMizu 中數組下標是 mizu[y][x],行列表達需與題目約定一致;flag 全局標記避免了走回頭路;打印時借助臨時棧做一次倒置,輸出順序才與實際行走一致。有更好實現歡迎交流。