matlab修改实验数据结构,数据结构实验练习(二):迷宫求解的两种方法

实验二 栈和队列的应用

一、实验目的

掌握栈与队列的基本结构和操作方法,培养学生灵活使用结构解决实际问题的能力。

二、实验内容

1、利用栈深度优先进行迷宫求解。

2、利用队列宽度优先进行迷宫求解。

三、实验要求

1、用数组表示迷宫。

2、建立栈,利用栈实现深度优先搜索。

3,建立队列,利用队列实现宽度优先搜索。

四,实验设计

#include

#include

#define M 8

#define N 8

#define Max 100

int mg[M+2][N+2]={

{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}

};

typedef struct

{  int i;

int

j;

int

di;

}Box;

typedef struct

{

Box date[Max];

int top;

}StType;

//

定义栈

struct

{ int i,j;

int pre;

}Qu[Max];

int front=-1,rear=-1;

//定义队列

bool mgpath1(int xi,int yi,int xe,int ye)//栈求解函数

{

int i,j,k,di,find;

StType st;

st.top=-1;

st.top++;

st.date[st.top].i=xi;

st.date[st.top].j=yi;

st.date[st.top].di=-1;

mg[xi][yi]=-1;

while(st.top>-1)

{

i=st.date[st.top].i;j=st.date[st.top].j;

di=st.date[st.top].di;

if(i==xe&&j==ye)

{

printf("栈深度优先搜索结果:\n");

for(k=0;k<=st.top;k++)

{ printf("\t(%d,%d)",st.date[k].i,st.date[k].j);

mg[st.date[k].i][st.date[k].j]=0;

if((k+1)%5==0)

printf("\n");

}

printf("\n");

return true;

}

find=0;

while(di<4&&find==0)

{

di++;

switch(di)

{

case 0:i=st.date[st.top].i-1;j=st.date[st.top].j;break;

case 1:i=st.date[st.top].i;j=st.date[st.top].j+1;break;

case 2:i=st.date[st.top].i+1;j=st.date[st.top].j;break;

case 3:i=st.date[st.top].i;j=st.date[st.top].j-1;break;

}

if(mg[i][j]==0)

find=1;

}

if(find==1)

{

st.date[st.top].di=di;

st.top++;

st.date[st.top].i=i;

st.date[st.top].j=j;

st.date[st.top].di=-1;

mg[i][j]=-1;

}

else

{

mg[st.date[st.top].i][st.date[st.top].j]=0;

st.top--;

}

}

return false;

}

void printo(int front)//队列输出

{ int k=front;

int j,ns=0;

do

{ j=k;

k=Qu[k].pre;

Qu[j].pre=-1;

}while(k!=0);

printf("队列深度优先搜索结果:\n");

k=0;

while(k

{ if(Qu[k].pre==-1)

{ ns++;

printf("\t(%d,%d)",Qu[k].i,Qu[k].j);

if(ns%5==0) printf("\n");

}

k++;

}

printf("\n");

}

mgpath2(int xi,int yi,int xe,int ye)

{

int i,j,find=0,di;

rear++;

Qu[rear].i=xi;Qu[rear].j=yi;

Qu[rear].pre=-1;

mg[1][1]=-1;

while(front<=rear&&!find)

{ front++;

i=Qu[front].i;

j=Qu[front].j;

if(i==xe&&j==ye)

{ find=1;

printo(front);

return 1;

}

for(di=0;di<4;di++)

{ switch(di)

{ case 0:i=Qu[front].i-1;j=Qu[front].j;break;

case 1:i=Qu[front].i;j=Qu[front].j+1;break;

case 2:i=Qu[front].i+1;j=Qu[front].j;break;

case 3:i=Qu[front].i;j=Qu[front].j-1;break;

}

if(mg[i][j]==0)

{ rear++;

Qu[rear].i=i;Qu[rear].j=j;Qu[rear].pre=front;

mg[i][j]=-1;

}

}

}return 0;

}

void main()

{

if(!mgpath1(1,1,M,N))

printf("该迷宫问题没有解!\n");

else

mgpath2(1,1,M,N);

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值