| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5194 人关注过本帖
标题:只用栈实现迷宫(C语言描述)
只看楼主 加入收藏
编程小呆
Rank: 2
来 自:西电
等 级:论坛游民
帖 子:31
专家分:23
注 册:2010-3-20
结帖率:100%
收藏
已结贴  问题点数:80 回复次数:9 
只用栈实现迷宫(C语言描述)
今天到网上找了老半天都没有找到一个满意的迷宫求解(栈实现),
在咱论坛上的也不是很好,故在此特发一征集帖,有好代码的都请
回上,有高手能写的也请不吝赐教,因为迷宫问题对我们数据结构初学者
太重要了,不管用栈还是用树的结构来实现,谢谢了
大家踊跃参与啊!!!!!!!(
把我的全部积分都用上了,下血本了~~)
搜索更多相关主题的帖子: 描述 迷宫 C语言 
2010-04-11 14:02
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
刚接触数据结构  课后习题 吧!
坐等答案 记得分享
2010-04-11 14:23
编程小呆
Rank: 2
来 自:西电
等 级:论坛游民
帖 子:31
专家分:23
注 册:2010-3-20
收藏
得分:0 
回复 2楼 hahayezhe
我主要是为大家方便参考,其次才是自己
2010-04-11 14:28
zgh9780
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2010-4-11
收藏
得分:10 
求TC的环境
2010-04-11 19:07
qq8801103
Rank: 5Rank: 5
来 自:苏州中科大软件学院
等 级:职业侠客
威 望:1
帖 子:422
专家分:340
注 册:2009-10-8
收藏
得分:50 
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10000
#define STACKINCREMENT 100
#define M 10
#define N 10
int flag=0;
typedef struct//坐标位置
{
    int x;
    int y;
}PosType;
typedef struct//栈中元素结构体
{
    int ord;//序号
    PosType seat;//坐标位置
    int di;//通道方向(1东2南3西4北)
}SElemType;
typedef struct//栈
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;
////////////////////////////////////////栈操作
void initialStack(SqStack *s)//栈的建立
{
    s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!s->base) exit(0);
    s->top=s->base;
    s->stacksize=STACK_INIT_SIZE;
}
void Push(SqStack *s,SElemType e)//插入栈顶为e的元素
{
    if(s->top-s->base>=s->stacksize)
    {
        s->base=(SElemType *)realloc(s->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(SElemType));
        if(!s->base) exit(0);
        s->top=s->base+s->stacksize;
        s->stacksize+=STACKINCREMENT;
    }
    (*(s->top)).ord=e.ord;
    (*(s->top)).seat.x=e.seat.x;
    (*(s->top)).seat.y=e.seat.y;
    (*(s->top)).di=e.di;
    s->top++;
}
void Pop(SqStack *s,SElemType *e)//删除栈顶元素 用e返回
{
    if(s->top==s->base) exit(0);
    (s->top)--;
    e->ord=(*(s->top)).ord;
    e->seat.x=(*(s->top)).seat.x;
    e->seat.y=(*(s->top)).seat.y;
    e->di=(*(s->top)).di;
}
int StackEmpty(SqStack *s)//检查栈是否为空
{
    if(s->top==s->base) return(1);
    else return(0);
}
void ClearStack(SqStack *s)//将s栈清空
{
    s->top=s->base;
    s->stacksize=0;
}
//////////////////////////////////////////初始化迷宫
void initialarray(int (*p)[N])
{
    int i,j;
    int maze[M][N]={1,1,1,1,1,1,1,1,1,1,
                    1,0,0,1,0,0,0,1,0,1,
                    1,0,0,0,0,1,0,1,0,1,
                    1,0,0,1,1,0,0,0,0,1,
                    1,1,1,1,1,0,0,1,0,1,
                    1,0,0,0,1,0,0,0,0,1,
                    1,0,1,0,0,1,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};/***/
    printf("打印迷宫:\n");
    for(i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("%d",maze[i][j]);
            *(*(p+i)+j)=maze[i][j];/***/利用二维数组
        }
        printf("\n");
    }
}
///////////
void NextPos(PosType *p,int d)//确定下一个位置的坐标
{
    switch(d)
    {
    case 1:p->y++;break;
    case 2:p->x++;break;
    case 3:p->y--;break;
    case 4:p->x--;break;
    }    /////////////////////////(1东2南3西4北)改变坐标
}
void MazePath(int (*maze)[N])//核心函数//---int (*maze)[N]
{   
    int curstep;
    int mark[M][N]={0};
    SqStack s;
    PosType curpos;
    SElemType e;//栈中元素结构体 1.序号;2.坐标位置;3.通道方向
    initialStack(&s);
    curpos.x=1;
    curpos.y=1;
    curstep=1;
    do
    {
        if((!maze[curpos.x][curpos.y])&&(!mark[curpos.x][curpos.y]))//*******
        {
            mark[curpos.x][curpos.y]=1;
            e.ord=curstep;//**e=(curstep,curpos,1)
            e.seat.x=curpos.x;
            e.seat.y=curpos.y;
            e.di=1;//***
            Push(&s,e);
            if((curpos.x==M-2)&&(curpos.y==N-2))
            {
               
                flag=1;
                printf("走出迷宫的一条坐标路径为:\n[终点]");
                while(!StackEmpty(&s))
                {
                    Pop(&s,&e);
                    printf("<--(%d,%d)",e.seat.x,e.seat.y);
                }
                printf("<--[起点]");
                printf("\n");
                ClearStack(&s);
            }
                NextPos(&curpos,1);//curpos=NextPos(curpos,1)
                curstep++;
        }
            else
            {
                if(!StackEmpty(&s))
                    Pop(&s,&e);
                while(e.di==4&&!StackEmpty(&s))
                {
                    mark[e.seat.x][e.seat.y]=1;
                    Pop(&s,&e);
                }
                if(e.di<4)
                {
                    e.di++;
                    Push(&s,e);
                    curpos.x=e.seat.x;//**curpos=NextPos(e.seat,e,di)
                    curpos.y=e.seat.y;
                    NextPos(&curpos,e.di);///**
                }
            }
    }while(!StackEmpty(&s));
}
void main()
{
    int array[M][N];
    initialarray(array);
    MazePath(array);
    if(flag==0)
        printf("该迷宫不能走出去!\n");
}

Discuz!  
好好学习  天天向上
2010-04-11 21:02
zbyw
Rank: 2
等 级:论坛游民
帖 子:31
专家分:57
注 册:2009-7-23
收藏
得分:20 
楼上厉害...
2010-04-11 21:27
kiss_jia
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-6-14
收藏
得分:0 
12个错误 。。。。
2010-06-14 16:50
lingshanini
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-7-12
收藏
得分:0 
楼上的代码还是不错的!一下的地方修改一下,就可以用了!

//////////////////////////////////////////初始化迷宫
void initialarray(int (*p)[N])
{
    int i,j;
    int maze[M][N]={1,1,1,1,1,1,1,1,1,1,
                    1,0,0,1,0,0,0,1,0,1,
                    1,0,0,0,0,1,0,1,0,1,
                    1,0,0,1,1,0,0,0,0,1,
                    1,1,1,1,1,0,0,1,0,1,
                    1,0,0,0,1,0,0,0,0,1,
                    1,0,1,0,0,1,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};/***/
    printf("打印迷宫:\n");
    for(i=0;i<M;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("%d",maze[i][j]);
            *(*(p+i)+j)=maze[i][j];/***/利用二维数组————改成   //利用二维数组
        }                                 就行了
2010-07-13 00:11
李煜爱因斯坦
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-5-13
收藏
得分:0 
我的一个初创程序,用VC++,希望对你们有用
#include <stdio.h>
#include<stdlib.h>
typedef struct//代表一个元素
{
    int row;//行
    int col;//列
    int di;//方向,di=1代表右,di=2代表下,di=3代表左,di=2代表上
}SElemType;
typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;
int M[10][10]={//等于1代表通不过,0代表能通过,2代表已经放在栈中
    {1,1,1,1,1,1,1,1,1,1},//四周为1代表围墙
    {1,0,0,1,1,1,1,1,1,1},
    {1,0,1,1,1,1,1,1,1,1},
    {1,0,0,0,0,0,0,0,0,1},
    {1,0,1,1,1,1,1,1,0,1},
    {1,0,1,1,1,1,1,1,0,1},
    {1,0,1,1,1,1,1,1,0,1},
    {1,0,1,1,1,1,1,0,0,1},
    {1,0,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1}
};
int InitStack(SqStack *s)
{
    s->base=(SElemType *)malloc(100*sizeof(SElemType));
    if(!s->base)exit(0);
    s->top=s->base;
    s->stacksize=100;
    return 1;
}//栈的初始化
int Pop(SqStack *s,SElemType *e)
{
    if(s->base==s->top)
        return 0;
    *e=*--s->top;
    return 1;
}//出栈
int Push(SqStack *s,SElemType e)
{
    if(s->base-s->top==s->stacksize)
    {
        s->base=(SElemType *)realloc(s->base,(100+10)*sizeof(SElemType));
        if(!s->base)exit(0);
        s->top=s->base+s->stacksize;
        s->stacksize=100+10;
    }
    *s->top++=e;
    return 1;
}//入栈
void reverseprint(SqStack s,SElemType *q)
{

    while(s.base!=s.top)
    {
        printf("[%d][%d]->",(*s.base).row,(*s.base).col);
        s.base++;
    }
    printf("[%d][%d]\n",q->row,q->col);
}//反向输出栈
int Chuli(SqStack *s,SElemType *e,SElemType *q)
{
    static int i=1;//第个路径
    for(;;)
    {
    while(e->di<=4)//如果四周都不通,则退出
    {
        switch(e->di)
        {
        case 1://如果四周存在可通的,则标记为2,才入栈,再进入可通的,方向重新开始
        if(M[e->row][e->col+1]==0){M[e->row][e->col]=2;Push(s,*e);e->col++;e->di=1;}
        else e->di++;break;
        case 2:
        if(M[e->row+1][e->col]==0){M[e->row][e->col]=2;Push(s,*e);e->row++;e->di=1;}
        else e->di++;break;
        case 3:
        if(M[e->row][e->col-1]==0){M[e->row][e->col]=2;Push(s,*e);e->col--;e->di=1;}
        else e->di++;break;
        case 4:
        if(M[e->row-1][e->col]==0){M[e->row][e->col]=2;Push(s,*e);e->row--;e->di=1;}
        else e->di++;break;
        }
    }
    if(s->base==s->top)return 0;//如果回到的起点,返回0
    if(!(e->row==q->row&&e->col==q->col))//还没找到了终点
    {
        Pop(s,e);e->di++;M[e->row][e->col]=0;
    }
    else break;//找到了终点,退出for
    }
    printf("第%d条路径:",i++);
    reverseprint(*s,q);//输出路径
    return 1;
}
void main()
{
    SqStack s;
    SElemType e,q;
    InitStack(&s);
    printf("请输入入口的横纵坐标:\n");
    scanf("%d%d",&e.row,&e.col);
    printf("请输入出口的横纵坐标:\n");
    scanf("%d%d",&q.row,&q.col);
    e.di=1;
    if(!Chuli(&s,&e,&q))printf("走不通\n");
    else
        while(s.base!=s.top)//直到走完所有的方向
        {
          Pop(&s,&e);
          e.di++;
          M[e.row][e.col]=0;
          Chuli(&s,&e,&q);

        }
}
2012-05-13 20:06
liupengjuan
Rank: 1
来 自:软件学院
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-6-29
收藏
得分:0 
楼上程序有BUG 还没输入 出口坐标就出现了: press any key to continue
2015-12-02 20:27
快速回复:只用栈实现迷宫(C语言描述)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023192 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved