C语言中用栈实现迷宫问题

Python016

C语言中用栈实现迷宫问题,第1张

#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=1val<top+1val++)

{

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=0i<14i++) //迷宫图形化输出

{

for(int j=0j<28j++)

{

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

}

满意请采纳!

#include <stdio.h>#include <mem.h>void main() { int a[100][100]//0:blocked 1:passage 2:finish -1:visitedint b[10000][2]int m=0,n=0int sttop=0int i,j,k,lmemset(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-1b[0][1]=j-1printf("Input the Map:\n")for (i=0i<mi++) for (j=0j<nj++) { scanf("%d",&a[i][j])} printf("Input the Finish:")scanf("%d%d",&i,&j)a[i-1][j-1]=2i=b[sttop][0]j=b[sttop][1]a[i][j]=-1while (sttop!=-1) { if (i>0) { //can up? if (a[i-1][j]==1) { a[--i][j]=-1b[++sttop][0]=ib[sttop][1]=jcontinue}else if (a[i-1][j]==2) { b[++sttop][0]=--ib[sttop][1]=jbreak} } if (i<m-1) { //can up? if (a[i+1][j]==1) { a[++i][j]=-1b[++sttop][0]=ib[sttop][1]=jcontinue}else if (a[i+1][j]==2) { b[++sttop][0]=++ib[sttop][1]=jbreak} } if (j>0) { //can left? if (a[i][j-1]==1) { a[i][--j]=-1b[++sttop][0]=ib[sttop][1]=jcontinue}else if (a[i][j-1]==2) { b[++sttop][0]=ib[sttop][1]=--jbreak} } if (j<m-1) { //can up? if (a[i][j+1]==1) { a[i][++j]=-1b[++sttop][0]=ib[sttop][1]=jcontinue}else if (a[i][j+1]==2) { b[++sttop][0]=ib[sttop][1]=j++break} } sttop--} if (sttop+1) { for (i=0i<sttopi++) 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,太大栈放不下了)