| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 896 人关注过本帖
标题:用栈来实现迷宫路径求解问题,有很多问题,各位高手帮我看看
只看楼主 加入收藏
维海
Rank: 2
等 级:论坛游民
帖 子:23
专家分:53
注 册:2010-11-25
结帖率:100%
收藏
已结贴  问题点数:30 回复次数:4 
用栈来实现迷宫路径求解问题,有很多问题,各位高手帮我看看
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

#define SIZE 100;
#define ADD 10;
typedef struct node1 {
    int x;
    int y;
}positype;
typedef struct node2 {
    int step;
    positype seat;
    int di;
}statype,*staptr;
typedef struct node3 {
    staptr base;
    staptr top;
    int size;
}sqsttype;

void initstack(sqsttype &p)
{
    staptr s;
    s=(staptr)malloc(SIZE*(sizeof(statype)));
    if(!s) exit(-1);
    p.base=p.top=s;
    p.size=100;
}
void pushsta(sqsttype &p,statype e)
{
    if(p.top-p.base>=p.size)
    {
        p.base=(staptr)realloc(p.base,(p.SIZE+ADD)*sizeof(statype));
        if (!p.base) exit(-1);
        p.top=p.base+p.size;
        p.size+=ADD;
    }
    p.top++;
    p.top=e;
}
void pop(sqsttype &p,statype &e)
{
    if (p.top==p.base) exit(-1);
    e=p.top;
    p.top--;
}
int emptyst(sqsttype p)
{
    if(p.top==p.base)
    return 1;
    else return 0;
}
statype create(int step,positype curpos,int di)
{
    statype e;
    e.step=step;e.seat=curpos;e.di=di;
    return e;
}
int pass(int a[m],positype curpos)
{
    return(a[curpos.y][curpos.x]==0||a[curpos.y][curpos.x]==3||a[curpos.y][curpos.x]==4);
}
void footprint(int &a[m],positype curpos)
{
    a[curpos.y][curpos.x]=='*';
}
void makprint(int &a[m],positype e.seat)
{
    a[curpos.y][curpos.x]='$';
}
void creatmaze(int &a[m],int m,int n,positype &enter,positype &out)
{
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",a[i][j]);
            if(a[i][j]==3)
            {
                enter.x=j;
                emter.y=i;
            }
            if(a[i][j]==4)
            {
                out.x=j;
                out.y=i;
            }
        }
    }

}
positype nextcurp(positype curpos,int di)
{
    positype pos=curpos;
    switch(di)
    {
        case 1://南
            pos.y++;
            break;
        case 2:
            pos.x--;
            break;
        case 3:
            pos.y--;
            break;
        case 4:
            pos.x++;
            break;
        return pos;
    }

}
void mazepath(sqsttype &p,int &a[m],positype &enter,positype &out)
{
    positype curpos;
    curpos=enter;
    initstack(p);
    int step=1;
    do
    {
        if(pass(a[m],curpos))
        {
            footprint(a[m],curpos);
            e=create(step,curpos,1);
            pushsta(p,e);
            if(curpos.x=out.x&&curpos.y==out.y) return 1;
            curpos=nextcurp(curpos,1);
            step++;
        }
        else
        {
            if(!emptyst(p))
            pop(p,e);
            while(e.di==4&&!emptyst(p))
            {
                markprint(a[m],e.seat);
                step--;
            }
            if(e.di<4)
            {
                e.di++;pushsta(p,e);
                curpos=nextcurp(e.seat,e.di);
            }
        }
    }while(!emptyst(p));
    return 0;
}
void mazeprint(sqsttype &p)
{
    while(!emptyst(p))
    {
        pop(p,e);
        printf("%d %d\n",e.seat->x,e.seat->y);
    }
}
int main()
{
    int m,n;
    staptr p;
    scanf("%d%d",&m,&n);
    int a[m][n];
    positype enter,out;
    creatmaze(a[m],m,n,enter,out);
    if(mazepath())
    pathprint(p);
    else
    printf("没找到通路");
    return 0;
}
描述:    迷宫问题
   
    迷宫是一个二维矩阵,其中1为墙,0为路,3为入口,4为出口.要求从入口开始,从出口结束,按照 下,左,上,右 的顺序来搜索路径.
输入:    迷宫宽度w 迷宫高度h
    迷宫第一行
    迷宫第二行
    ...
    迷宫第h 行
输出:    入口横坐标1  入口纵坐标1
    横坐标2       纵坐标2
    横坐标3       纵坐标3
    横坐标4       纵坐标4
    ...
    横坐标n-1    纵坐标n-1
    出口横坐标n 出口纵坐标n
输入样例:    8 10
    1 1 1 1 1 1 1 1
    1 0 1 1 0 1 0 1
    1 0 1 0 0 1 0 1
    1 1 0 3 1 0 1 1
    1 0 0 1 0 0 4 1
    1 0 0 0 0 1 1 1
    1 0 1 0 0 1 0 1
    1 0 1 0 0 0 1 1
    1 1 1 1 0 0 0 1
    1 1 1 1 1 1 1 1
输出样例:   
    3 3
    2 3
    2 4
    2 5
    3 5
    3 6
    3 7
    4 7
    4 6
    4 5
    4 4
    5 4
    6 4


[ 本帖最后由 维海 于 2011-3-27 18:16 编辑 ]
搜索更多相关主题的帖子: void 100 include 
2011-03-27 17:47
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:20 
看我的吧   
栈回溯解迷宫.rar (246.62 KB)

                                         
===========深入<----------------->浅出============
2011-03-27 18:29
维海
Rank: 2
等 级:论坛游民
帖 子:23
专家分:53
注 册:2010-11-25
收藏
得分:0 
回复 2楼 laoyang103
谢谢
2011-03-27 19:25
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:5 
很不错啊

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2011-03-28 10:26
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:5 
呵呵   大家谁会图片压缩  帮我去看一个我发的帖子   100分的哦

                                         
===========深入<----------------->浅出============
2011-03-28 10:42
快速回复:用栈来实现迷宫路径求解问题,有很多问题,各位高手帮我看看
数据加载中...
 
   



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

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