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

Python017

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>

/*

1 means it is blocked.

0 means it is available/ you can go.

E means the Exit point.

* means the start point.*/

typedef struct{

int x,y,last

}ST

char in[15][15]int vis[15][15]={0}

const int dx[]={0,0,1,-1},

  dy[]={1,-1,0,0}

ST st[300]int fr,re

#define ok(_) ((_)>=0&&(_)<11)

#define canto(_,__) (ok(st[_].x+dx[__])&&ok(st[_].y+dy[__]))

#define V(_) (vis[st[_].x][st[_].y])

#define Vd(_,__) (vis[st[_].x+dx[__]][st[_].y+dy[__]])

#define Md(_,__) (in [st[_].x+dx[__]][st[_].y+dy[__]])

int go(int x,int y){

fr=0re=1

st[0].x=x

st[0].y=y

V(0)=1

int i

while(fr<re){

for(i=0i<4i++)

if(canto(fr,i)&&!Vd(fr,i)&&Md(fr,i)!='1'){

st[re].x=st[fr].x+dx[i]

st[re].y=st[fr].y+dy[i]

st[re].last=fr

Vd(fr,i)=1

if(Md(fr,i)=='E')return re

re++}

fr++

}

return 0

}

void solve(void){

int i,j,p=-1

for(i=0i<11i++)

for(j=0j<11j++)

if(in[i][j]=='*')

if(p==-1)p=go(i,j)

else {

printf("Too Many Start Point!")//这是废话,可删 

return }

if(p==-1)printf("No Start Point!")//同上 

else if(p==0)printf("Cant Go To End!")//同上 

else {

for(i=0i<11i++)

printf("%s\n",in[i])//输出原迷宫 

printf("\n") 

while(st[p].last!=0){

p=st[p].last

if(p==0)break

in[st[p].x][st[p].y]='x'}//用x替换路径 

for(i=0i<11i++)

printf("%s\n",in[i])//输出 

}

return 

}

int main(int ac,char **ar){

FILE *inp=NULL

int i

if(ac==1)return 0

inp=fopen(ar[1],"r")

if(inp==NULL)return 0

for(i=0i<11i++)

fscanf(inp,"%s",in[i])

solve()

    return 0

}

/*BY TXJ*/

#include <stdio.h>

#include <stdlib.h>

typedef struct

{

int x

int y

int type

int v

}POINT

POINT s[10][10],stack[50],start,end,c

int pos=0

void prt()

{

int i,j

system("cls")

for(i=0i<10i++)

{

for(j=0j<10j++)

{

if(end.x==i && end.y==j)

{

printf("ex")

}

else if(s[i][j].type==1)

{

printf("■")

}

else if(i==c.x && j==c.y)

{

printf("●")

}

else

{

printf("  ")

}

}

printf("\n")

}

system("pause")

}

void stack_in(POINT a)

{

stack[pos++]=a

}

POINT stack_out()

{

int i

POINT t

t=stack[0]

for(i=0i<pos-1i++)

{

stack[i]=stack[i+1]

}

pos--

return t

}

void fun()

{

POINT a

int x,y,v

while(1)

{

if(pos==0)

{

break

}

a=stack_out()

x=a.x

y=a.y

if(x==start.x && y==start.y)

{

break

}

v=s[x][y].v

if(x-1>=0 && s[x-1][y].type==0 && (s[x-1][y].v==-1 || s[x-1][y].v>v+1))

{

s[x-1][y].v=v+1

stack_in(s[x-1][y])

}

if(x+1<=9 && s[x+1][y].type==0 && (s[x+1][y].v==-1 || s[x-1][y].v>v+1))

{

s[x+1][y].v=v+1

stack_in(s[x+1][y])

}

if(y-1>=0 && s[x][y-1].type==0 && (s[x][y-1].v==-1 || s[x-1][y].v>v+1))

{

s[x][y-1].v=v+1

stack_in(s[x][y-1])

}

if(y+1<=9 && s[x][y+1].type==0 && (s[x][y+1].v==-1 || s[x-1][y].v>v+1))

{

s[x][y+1].v=v+1

stack_in(s[x][y+1])

}

// prt()

}

}

void go(int x, int y)

{

int v

while(1)

{

if(x==end.x && y==end.y)

{

return

}

v=s[x][y].v

if(v==0)

{

return

}

if(x-1>=0 && s[x-1][y].v==v-1)

{

c=s[x-1][y]

x=x-1

prt()

continue

}

else if(x+1<=9 && s[x+1][y].v==v-1)

{

c=s[x+1][y]

x++

prt()

continue

}

else if(y-1>=0 && s[x][y-1].v==v-1)

{

c=s[x][y-1]

y--

prt()

continue

}

else if(y+1<=9 && s[x][y+1].v==v-1)

{

c=s[x][y+1]

y++

prt()

continue

}

}

}

int main()

{

int i,j

for(i=0i<10i++)

{

for(j=0j<10j++)

{

s[i][j].x=i

s[i][j].y=j

s[i][j].type=0

s[i][j].v=-1

}

}

int x,y

end=s[9][5]

start=s[0][0]

x=end.x

y=end.y

s[x][y].v=0

stack_in(end)

s[0][4].type=1

s[0][8].type=1

s[1][2].type=1

s[1][6].type=1

s[1][8].type=1

s[2][0].type=1

s[2][2].type=1

s[2][4].type=1

s[2][5].type=1

s[2][6].type=1

s[2][6].type=1

s[3][2].type=1

s[3][4].type=1

s[3][8].type=1

s[4][0].type=1

s[4][2].type=1

s[4][4].type=1

s[4][6].type=1

s[4][7].type=1

s[4][8].type=1

s[5][2].type=1

s[6][0].type=1

s[6][2].type=1

s[6][4].type=1

s[6][5].type=1

s[6][6].type=1

s[6][7].type=1

s[6][8].type=1

s[7][2].type=1

s[7][4].type=1

s[7][8].type=1

s[8][1].type=1

s[8][2].type=1

s[8][4].type=1

s[8][6].type=1

s[8][8].type=1

s[9][4].type=1

s[9][6].type=1

fun()

c=start

prt()

go(start.x,start.y)

return 0

}