用棧編程迷宮
1. 用棧的方法設計迷宮求解(c語言)。。限時
原來做過,以下為源代碼和部分注釋,可以運行
#include<stdio.h>
#include<stdlib.h>
#define M 15
#define N 15
struct mark //定義迷宮內點的坐標類型
{
int x;
int y;
};
struct Element //"戀"棧元素,嘿嘿。。
{
int x,y; //x行,y列
int d; //d下一步的方向
};
typedef struct LStack //鏈棧
{
Element elem;
struct LStack *next;
}*PLStack;
/*************棧函數****************/
int InitStack(PLStack &S)//構造空棧
{
S=NULL;
return 1;
}
int StackEmpty(PLStack S)//判斷棧是否為空
{
if(S==NULL)
return 1;
else
return 0;
}
int Push(PLStack &S, Element e)//壓入新數據元素
{
PLStack p;
p=(PLStack)malloc(sizeof(LStack));
p->elem=e;
p->next=S;
S=p;
return 1;
}
int Pop(PLStack &S,Element &e) //棧頂元素出棧
{
PLStack p;
if(!StackEmpty(S))
{
e=S->elem;
p=S;
S=S->next;
free(p);
return 1;
}
else
return 0;
}
/***************求迷宮路徑函數***********************/
void MazePath(struct mark start,struct mark end,int maze[M][N],int diradd[4][2])
{
int i,j,d;int a,b;
Element elem,e;
PLStack S1, S2;
InitStack(S1);
InitStack(S2);
maze[start.x][start.y]=2; //入口點作上標記
elem.x=start.x;
elem.y=start.y;
elem.d=-1; //開始為-1
Push(S1,elem);
while(!StackEmpty(S1)) //棧不為空 有路徑可走
{
Pop(S1,elem);
i=elem.x;
j=elem.y;
d=elem.d+1; //下一個方向
while(d<4) //試探東南西北各個方向
{
a=i+diradd[d][0];
b=j+diradd[d][1];
if(a==end.x && b==end.y && maze[a][b]==0) //如果到了出口
{
elem.x=i;
elem.y=j;
elem.d=d;
Push(S1,elem);
elem.x=a;
elem.y=b;
elem.d=886; //方向輸出為-1 判斷是否到了出口
Push(S1,elem);
printf("\n0=東 1=南 2=西 3=北 886為則走出迷宮\n\n通路為:(行坐標,列坐標,方向)\n");
while(S1) //逆置序列 並輸出迷宮路徑序列
{
Pop(S1,e);
Push(S2,e);
}
while(S2)
{
Pop(S2,e);
printf("-->(%d,%d,%d)",e.x,e.y,e.d);
}
return; //跳出兩層循環,本來用break,但發現出錯,exit又會結束程序,選用return還是不錯滴o(∩_∩)o...
}
if(maze[a][b]==0) //找到可以前進的非出口的點
{
maze[a][b]=2; //標記走過此點
elem.x=i;
elem.y=j;
elem.d=d;
Push(S1,elem); //當前位置入棧
i=a; //下一點轉化為當前點
j=b;
d=-1;
}
d++;
}
}
printf("沒有找到可以走出此迷宮的路徑\n");
}
/*************建立迷宮*******************/
void initmaze(int maze[M][N])
{
int i,j;
int m,n; //迷宮行,列
printf("請輸入迷宮的行數 m=");
scanf("%d",&m);
printf("請輸入迷宮的列數 n=");
scanf("%d",&n);
printf("\n請輸入迷宮的各行各列:\n用空格隔開,0代表路,1代表牆\n",m,n);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&maze[i][j]);
printf("你建立的迷宮為o(∩_∩)o...\n");
for(i=0;i<=m+1;i++) //加一圈圍牆
{
maze[i][0]=1;
maze[i][n+1]=1;
}
for(j=0;j<=n+1;j++)
{
maze[0][j]=1;
maze[m+1][j]=1;
}
for(i=0;i<=m+1;i++) //輸出迷宮
{
for(j=0;j<=n+1;j++)
printf("%d ",maze[i][j]);
printf("\n");
}
}
void main()
{
int sto[M][N];
struct mark start,end; //start,end入口和出口的坐標
int add[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//行增量和列增量 方向依次為東西南北
initmaze(sto);//建立迷宮
printf("輸入入口的橫坐標,縱坐標[逗號隔開]\n");
scanf("%d,%d",&start.x,&start.y);
printf("輸入出口的橫坐標,縱坐標[逗號隔開]\n");
scanf("%d,%d",&end.x,&end.y);
MazePath(start,end,sto,add); //find path
system("PAUSE");
}
2. C++用棧實現迷宮的搜索
java">
packagecom.stack;
importjava.util.Stack;
classStep{
intx,y,d;
publicStep(intx,inty,intd){
this.x=x;//橫坐標
this.y=y;//縱坐標
this.d=d;//方向
}
}
publicclassMazeTest{
publicstaticvoidmain(String[]args){
//迷宮定義
int[][]maze={{1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1},
{1,1,0,1,0,1,1,1,1,1},
{1,0,1,0,0,0,0,0,1,1},
{1,0,1,1,1,0,1,1,1,1},
{1,1,0,0,1,1,0,0,0,1},
{1,0,1,1,0,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1}};
int[][]move={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
Stack<Step>s=newStack<Step>();
inta=path(maze,move,s);
while(!s.isEmpty()){
Stepstep=s.pop();
System.out.println(step.x+":"+step.y);
}
}
publicstaticintpath(int[][]maze,int[][]move,Stack<Step>s){
Steptemp=newStep(1,1,-1);//起點
s.push(temp);
while(!s.isEmpty()){
//temp=s.pop();這樣寫是有問題的,不能將出棧的元素作為當前位置,應該將棧頂元素作為當前位置
s.pop();//如果路走不通就出棧
if(!s.isEmpty()){
temp=s.peek();//將棧頂元素設置為當前位置
}
intx=temp.x;
inty=temp.y;
intd=temp.d+1;
while(d<8){
inti=x+move[d][0];
intj=y+move[d][1];
if(maze[i][j]==0){//該點可達
temp=newStep(i,j,d);//到達新點
s.push(temp);
x=i;
y=j;
maze[x][y]=-1;//到達新點,標志已經到達
if(x==6&&y==1){
return1;//到達出口,迷宮有路,返回1
}else{
d=0;//重新初始化方向
}
}else{
d++;//改變方向
}
}
}
return0;
}
}
3. C語言中用棧實現迷宮問題
#include
#define MAXSIZE 100
using namespace std;
struct stack{
int iway;
int jway;
int dire;
};
stack maze[MAXSIZE];
int top;
int map[14][28]={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1},
{1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,1,0,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,1,0,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
void findway(int xS,int yS,int xE,int yE)
{
top=0;
maze[top].iway=xS;
maze[top].jway=yS;
map[xS][yS]=-1;
int i,j,di,find;
while(top>-1)
{
i=maze[top].iway;
j=maze[top].jway;
di=maze[top].dire;
if(i==xE&&j==yE)
{
cout<<"***********************************\n";
cout<<"path"<<":"<<endl;
cout<<"("<<maze[0].iway<<","<<maze[0].jway<<")";
for(int val=1;val<top+1;val++)
{
cout<("<<maze[val].iway<<","<<maze[val].jway<<")";
if((val+1)%5==0)
cout<<endl;
}
cout<<endl;
cout<<"***********************************\n";
return;
}
find=0;
while(find==0&&di<4)
{
di++;
switch(di)
{
case(0):i=maze[top].iway;
j=maze[top].jway+1;
break;
case(1):i=maze[top].iway;
j=maze[top].jway-1;
break;
case(2):i=maze[top].iway+1;
j=maze[top].jway;
break;
case(3):i=maze[top].iway-1;
j=maze[top].jway;
break;
}
if(map[i][j]==0)
{
find=1;
}
}
if(find==1)
{
maze[top].dire=di;
top++;
maze[top].iway=i;
maze[top].jway=j;
maze[top].dire=-1;
map[i][j]=-1;
}
else
{
map[maze[top].iway][maze[top].jway]=0;
top--;
}
}
}
int main()
{
for(int i=0;i<14;i++) //迷宮圖形化輸出
{
for(int j=0;j<28;j++)
{
if(map[i][j]==1)
cout<<"■";
else cout<<"□";
}
cout<<endl;
}
int xStart,yStart,xEnd,yEnd;
cout<<"請輸入迷宮起點坐標,用空格隔開(左上角坐標為(0,0)):";
cin>>xStart>>yStart;
cout<<"請輸入迷宮終點坐標,用空格隔開(右上角坐標為(13,27)):";
cin>>xEnd>>yEnd;
findway(xStart,yStart,xEnd,yEnd);
return 0;
}
滿意請採納!
4. 數據結構 迷宮問題 用棧解決
核心演算法是搜索,這里如果要求用棧實現那就是深度優先搜索。 如果他不指定是用棧, 那麼用隊列來做就是廣度優先搜索。
正常的深度優先搜索是可以用遞歸來實現的, 即然要求你非遞歸 你可以用棧來模擬遞歸的效果,畢竟函數的遞歸調用本質也是用棧來實現的
5. 請幫我做一道數據結構程序題,題目為用棧解決迷宮問題
註:下面分別是三個文件:棧的定義與聲明(頭文件)、棧的實現、迷宮問題。
/* 順序棧表示:類型和界面函數聲明 */
enum { MAXNUM = 20 /* 棧中最大元素個數,應根據需要定義 */
};
typedef int DataType; /* 棧中元素類型,應根據需要定義 */
struct SeqStack { /* 順序棧類型定義 */
int t; /* 棧頂位置指示 */
DataType s[MAXNUM];
};
typedef struct SeqStack SeqSack, *PSeqStack; /* 順序棧類型和指針類型 */
/*創建一個空棧;為棧結構申請空間,並將棧頂變數賦值為-1*/
PSeqStack createEmptyStack_seq( void );
/*判斷pastack所指的棧是否為空棧,當pastack所指的棧為空棧時,則返回1,否則返回0*/
int isEmptyStack_seq( PSeqStack pastack );
/* 在棧中壓入一元素x */
void push_seq( PSeqStack pastack, DataType x );
/* 刪除棧頂元素 */
void pop_seq( PSeqStack pastack );
/* 當pastack所指的棧不為空棧時,求棧頂元素的值 */
DataType top_seq( PSeqStack pastack );
/* 順序棧表示:函數定義 */
#include <stdio.h>
#include <stdlib.h>
#include "sstack.h"
/*創建一個空棧;為棧結構申請空間,並將棧頂變數賦值為-1*/
PSeqStack createEmptyStack_seq( void ) {
PSeqStack pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if (pastack==NULL)
printf("Out of space!! \n");
else
pastack->t = -1;
return pastack;
}
/*判斷pastack所指的棧是否為空棧,當pastack所指的棧為空棧時,則返回1,否則返回0*/
int isEmptyStack_seq( PSeqStack pastack ) {
return pastack->t == -1;
}
/* 在棧中壓入一元素x */
void push_seq( PSeqStack pastack, DataType x ) {
if( pastack->t >= MAXNUM - 1 )
printf( "Stack Overflow! \n" );
else {
pastack->t++;
pastack->s[pastack->t] = x;
}
}
/* 刪除棧頂元素 */
void pop_seq( PSeqStack pastack ) {
if (pastack->t == -1 )
printf( "Underflow!\n" );
else
pastack->t--;
}
/* 當pastack所指的棧不為空棧時,求棧頂元素的值 */
DataType top_seq( PSeqStack pastack ) {
return pastack->s[pastack->t];
}
/* 迷宮問題的非遞歸演算法(棧實現)*/
#define MAXNUM 100/* 棧中最大元素個數 */
#define N 11 /*地圖的第一維長度*/
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x;/* 行下標 */
int y;/* 列下標 */
int d;/* 運動方向 */
} DataType;
struct SeqStack { /* 順序棧類型定義 */
int t; /* 指示棧頂位置 */
DataType s[MAXNUM];
};
typedef struct SeqStack *PSeqStack; /* 順序棧類型的指針類型 */
PSeqStack pastack; /* pastack是指向順序棧的一個指針變數 */
PSeqStack createEmptyStack_seq( void ) {
PSeqStack pastack;
pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if (pastack == NULL)
printf("Out of space!! \n");
else
pastack->t = -1;
return pastack;
}
int isEmptyStack_seq( PSeqStack pastack ) {
return pastack->t == -1;
}
/* 在棧中壓入一元素x */
void push_seq( PSeqStack pastack, DataType x ) {
if( pastack->t >= MAXNUM - 1 )
printf( "Overflow! \n" );
else {
pastack->t++;
pastack->s[pastack->t] = x;
}
}
/* 刪除棧頂元素 */
void pop_seq( PSeqStack pastack ) {
if (pastack->t == -1 )
printf( "Underflow!\n" );
else
pastack->t--;
}
/* 當pastack所指的棧不為空棧時,求棧頂元素的值 */
DataType top_seq( PSeqStack pastack ) {
return (pastack->s[pastack->t]);
}
void pushtostack(PSeqStack st, int x, int y, int d) {
DataType element;
element.x = x;
element.y = y;
element.d = d;
push_seq(st, element);
}
void printpath(PSeqStack st) {
DataType element;
printf("The revers path is:\n"); /* 列印路徑上的每一點 */
while(!isEmptyStack_seq(st)) {
element = top_seq(st);
pop_seq(st);
printf("the node is: %d %d \n", element.x, element.y);
}
}
/* 迷宮maze[M][N]中求從入口maze[x1][y1]到出口maze[x2][y2]的一條路徑 */
/* 其中 1<=x1,x2<=M-2 , 1<=y1,y2<=N-2 */
void mazePath(int maze[][N], int direction[][2], int x1, int y1, int x2, int y2) {
int i, j, k, g, h;
PSeqStack st;
DataType element;
st = createEmptyStack_seq( );
maze[x1][y1] = 2; /* 從入口開始進入,作標記 */
pushtostack(st, x1, y1, -1); /* 入口點進棧 */
while ( !isEmptyStack_seq(st)) { /* 走不通時,一步步回退 */
element = top_seq(st);
pop_seq(st);
i = element.x; j = element.y;
for (k = element.d + 1; k <= 3; k++) { /* 依次試探每個方向 */
g = i + direction[k][0];h = j + direction[k][1];
if (g == x2 && h == y2 && maze[g][h] == 0) { /* 走到出口點 */
printpath(st); /* 列印路徑 */
return;
}
if (maze[g][h] == 0) { /* 走到沒走過的點 */
maze[g][h] = 2; /* 作標記 */
pushtostack(st, i, j, k); /* 進棧 */
i = g; j = h; k = -1; /* 下一點轉換成當前點 */
}
}
}
printf("The path has not been found.\n");/* 棧退完未找到路徑 */
}
int main(){
int direction[][2]={0,1,1,0,0,-1,-1,0};
int maze[][N] = {
1,1,1,1,1,1,1,1,1,1,1,
1,0,1,0,0,1,1,1,0,0,1,
1,0,0,0,0,0,1,0,0,1,1,
1,0,1,1,1,0,0,0,1,1,1,
1,0,0,0,1,0,1,1,0,1,1,
1,1,0,0,1,0,1,1,0,0,1,
1,1,1,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1
};
mazePath(maze,direction,1,1,6,9);
getchar();
return 0;
}
6. 【迷宮問題】用堆棧解迷宮
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#defineMAXM40
#defineMAXN60
intmaze[MAXM][MAXN];//01-障礙物
intm=40,n=60;//40行60列
intused[MAXM][MAXN];//記錄是否到過
intline[MAXM*MAXN];//記錄迷宮路線
intline_len;
intline_r[MAXM*MAXN];
intd[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//4個方向
voiddfs(intu,intv,intstep)
{
intx,y,i;
if(line_len!=0)
return;
if(u==m-1&&v==n-1)
{
line_len=step;
memcpy(line_r,line,line_len*sizeof(int));
return;
}
for(i=0;i<4;i++)
{
x=u+d[i][0];
y=v+d[i][1];
if(x>=0&&x<m&&y>=0&&y<n&&!used[x][y]&&maze[x][y]==0)
{
used[x][y]=1;
line[step]=x<<16|y;
dfs(x,y,step+1);
//used[x][y]=0;//不用退回來了,因為只需要一條路徑
}
}
}
intmain()
{
inti,j;
//隨機生成迷宮
srand(time(NULL));
for(i=0;i<m;i++)
for(j=0;j<n;j++)
maze[i][j]=rand()%8==0?1:0;//是1的概率約1/8,通路的概率大些
maze[0][0]=maze[m-1][n-1]=0;//起始點和終端必須為0
line_len=0;
line[0]=0<<16|0;
memset(used,0,sizeof(used));
dfs(0,0,0);//(0,0)->(m-1,n-1)
if(line_len==0)
printf("沒有通路 ");
else
{
for(i=0;i<line_len;i++)
printf("(%d,%d) ",(line_r[i]>>16)+1,(line_r[i]&0xffff)+1);
}
return0;
}
你給的那庫實在是沒什麼用的慾望,要最短路徑一般用廣度搜索,上面的代碼應該屬於深度搜索
7. c語言用棧解決迷宮問題
明擺著圖的深度遍歷 用堆棧模擬遞歸
8. C語言迷宮問題,用棧來做
#include <stdio.h> #include <mem.h> void main() { int a[100][100]; //0:blocked 1:passage 2:finish -1:visited; int b[10000][2]; int m=0,n=0; int sttop=0; int i,j,k,l; memset(a,0,sizeof(a)); printf("Please Input m,n:"); scanf("%d%d",&m,&n); printf("Input the start:"); scanf("%d%d",&i,&j); b[0][0]=i-1; b[0][1]=j-1; printf("Input the Map:\n"); for (i=0;i<m;i++) for (j=0;j<n;j++) { scanf("%d",&a[i][j]); } printf("Input the Finish:"); scanf("%d%d",&i,&j); a[i-1][j-1]=2; i=b[sttop][0];j=b[sttop][1]; a[i][j]=-1; while (sttop!=-1) { if (i>0) { //can up? if (a[i-1][j]==1) { a[--i][j]=-1; b[++sttop][0]=i; b[sttop][1]=j; continue; }else if (a[i-1][j]==2) { b[++sttop][0]=--i; b[sttop][1]=j; break; } } if (i<m-1) { //can up? if (a[i+1][j]==1) { a[++i][j]=-1; b[++sttop][0]=i; b[sttop][1]=j; continue; }else if (a[i+1][j]==2) { b[++sttop][0]=++i; b[sttop][1]=j; break; } } if (j>0) { //can left? if (a[i][j-1]==1) { a[i][--j]=-1; b[++sttop][0]=i; b[sttop][1]=j; continue; }else if (a[i][j-1]==2) { b[++sttop][0]=i; b[sttop][1]=--j; break; } } if (j<m-1) { //can up? if (a[i][j+1]==1) { a[i][++j]=-1; b[++sttop][0]=i; b[sttop][1]=j; continue; }else if (a[i][j+1]==2) { b[++sttop][0]=i; b[sttop][1]=j++; break; } } sttop--; } if (sttop+1) { for (i=0;i<sttop;i++) printf("(%d,%d)->",b[i][0]+1,b[i][1]+1); printf("(%d,%d)\n",b[i][0]+1,b[i][1]+1); } else printf("Can't Reach The Finish Spot!\n"); return; } 用非嵌套的方法做的棧,起點 終點自己定 輸入數據規則如下: 先輸入m n(m為行數,n為列數) 再輸入起點(最左上角為(1,1)(前者行號,後者列號)則輸入"1 1"(不含引號)其他依次類推) 再輸入地圖(0為被阻擋,1為通路)(起點被默認為通路,無論輸入0還是1) 再輸入終點(規則和起點一樣) 回車後出結果,結果的表達方式以(x,y)->(x,y)->....->(x,y)(坐標任以1,1為左上角) 或者Can't Reach The Finish Spot! 呈現 其他的修改的話可以隨便咯(規模m,n<=100,太大棧放不下了)
9. 迷宮問題,用棧,用C語言
這個是我寫的一個迷宮最短路徑的一個例子:廣搜
#include<stdio.h>
#include<queue>
using namespace std;
struct node
{
int x,y;
};
int map[5][5],vis[5][5];
node root[30];
int rn,f;
int dir[4][2]={-1,0,0,1,1,0,0,-1};
void bfs(int i,int j)
{
node cur,next;
queue<node>q;
cur.x=i;
int k;
cur.y=j;
memset(vis,-1,sizeof(vis));
vis[i][j]=0;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
for(k=0;k<4;k++)
{
next.x=cur.x+dir[k][0];
next.y=cur.y+dir[k][1];
if(next.x>=0&&next.x<5&&next.y>=0&&next.y<5)
{
if(map[next.x][next.y]==1) continue;
if(vis[next.x][next.y]==-1)
{
vis[next.x][next.y]=vis[cur.x][cur.y]+1;
q.push(next);
}
}
}
}
}
void dfs(int i,int j)
{
root[rn].x=i;
root[rn].y=j;
rn++;
if(i==0&&j==0)
{
f=1;
return;
}
int k,ii,jj;
for(k=0;k<4;k++)
{
ii=i+dir[k][0];
jj=j+dir[k][1];
if(ii>=0&&ii<5&&jj>=0&&jj<5)
{
if(vis[ii][jj]==(vis[i][j]-1))
dfs(ii,jj);
}
if(f)
return;
}
}
int main()
{
int i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf("%d",&map[i][j]);
bfs(0,0);
rn=0;
f=0;
dfs(4,4);
for(i=rn-1;i>=0;i--)
{
printf("(%d, %d)\n",root[i].x,root[i].y);
}
return 0;
}
10. 棧的運用-迷宮問題
我說一下思路,代碼自己實現吧。
你的要求是用棧:
首先得到迷宮的表示(一般是一個二維數組),在起點處,按順時針方向,即如果東能走,走東(東進棧),如果東不能走,則走南(南進棧)。依此不停地走,如果四個方向都走不了,退棧。...到這里講的是棧在這個演算法中的用處。
至於要棧中要保存的信息:位置,走的方向。
演算法中還需要一個數組來保存路徑。
講的很泛,但意思都在,希望你認真理解。