| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 943 人关注过本帖
标题:迷宫问题
只看楼主 加入收藏
mlovelyy
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-8-30
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:2 
迷宫问题
我照这书上写了迷宫问题,但是总是不对,哪位大侠帮忙看下,只说下思路就行了,谢谢
#include <stdio.h>
#define InitSize 100
#define Increment 20
#define N 10
typedef struct PosType
{
    int row;
    int col;
}PosType;
typedef struct
{
    int ord;
    PosType seat;
    int di;
}SElemType;
typedef struct Stack
{
    SElemType * top;
    SElemType * base;
    int size;
}Stack;
typedef struct
{
    int a[N][N];
}MazeType;

void StackInit(Stack *S)
{
    S->top=S->base=(SElemType *)malloc(InitSize *sizeof(SElemType));
    if(!S->top)
        exit (1);
    S->size=InitSize;
}
SElemType Gettop(Stack S)
{
    return *(S.top-1);
}
void Push(Stack *S,SElemType x)
{
    if(S->top-S->base>=S->size)
    {
        S->base=(SElemType *)realloc((S->size+Increment)*sizeof(SElemType));
        if(!S->base)
            exit (0);
    }
    *(S->top++)=x;
}
void Pop(Stack *S,SElemType  *x)
{
    if(S->top-S->base!=0)
        *x=*(--S->top);
}
int StackEmpty(Stack S)
{
    if(S.top==S.base)
        return 1;
    else
        return 0;
}
int Pass(PosType step,MazeType maze)
{
    if(maze.a[step.row][step.col]=1)
        return 1;
    else
        return 0;
}
void FootPrint(PosType step,MazeType *maze)
{
    maze->a[step.row][step.col]=5;
}
void MarkPrint(PosType step,MazeType *maze)
{
    maze->a[step.row][step.col]=9;
}
PosType NextStep(PosType p,int i)
{
    PosType pos;
    switch(i)
    {
    case 1:pos.row=p.row;pos.col=p.col+1;return pos;
    case 2:pos.row=p.row+1;pos.col=p.col;return pos;
    case 3:pos.row=p.row;pos.col=p.col-1;return pos;
    case 4:pos.row=p.row-1;pos.col=p.col;return pos;
    }
}
void MazeInit(MazeType *maze)
{
    int j,i;
    for(j=0;j<N;j++)
    {
        maze->a[0][j]=maze->a[N-1][j]=0;
        maze->a[j][0]=maze->a[j][N-1]=0;
    }
    for(i=1;i<N-1;i++)
        for(j=1;j<N-1;j++)
            maze->a[i][j]=1;
}
int MazePath(MazeType *maze,PosType start,PosType end)
{
    int curstep=1,i,j;
    PosType curpos=start;
    Stack st;
    SElemType e;
    StackInit(&st);
    printf("迷宫为:\n");
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
            printf("%d",maze->a[i][j]);
        printf("\n");
    }
    do{
        if(Pass(curpos,*maze))
        {
            FootPrint(curpos,maze);
            e.ord=curstep;
            e.seat=curpos;
            e.di=1;
            Push(&st,e);
            if(curpos.col==end.col&&curpos.row==end.row)
                return 1;
            else
            {
                curpos=NextStep(curpos,1);
                curstep++;
            }
        }
        else
        {
            if(!StackEmpty(st))
                Pop(&st,&e);
            while(e.di==4&&!StackEmpty)
            {
                MarkPrint(e.seat,maze);
                Pop(&st,&e);
            }
            if(e.di<4)
            {
                e.di++;
                Push(&st,e);
                curpos=NextStep(e.seat,e.di);
            }
        }
    }while(!StackEmpty(st));
    return 0;
}
int main(void)
{
    MazeType m;
    int x,y,i,j;
    PosType start={1,1},end={8,8};
    MazeInit(&m);
    printf("输入不可通的位置的坐标x,y(0结束):");
    while(x!=0)
    {
        scanf("%d",&x);
        scanf("%d",&y);
        m.a[x][y]=0;
        printf("输入不可通的位置的坐标x,y(0结束):");
    }
    
    MazePath(&m,start,end);
    printf("\n");
    for(i=1;i<N;i++)
    {
        for(j=1;j<N;j++)
            printf("%d",m.a[i][j]);
        printf("\n");
    }
return 0;
}
搜索更多相关主题的帖子: 迷宫 
2009-08-14 16:09
龙心
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:14
专家分:159
注 册:2009-8-20
收藏
得分:20 
用二唯数组作为存放迷宫位置的数据结构,用栈作为存放路径顺序的数据结构,问题很好解决
2009-08-21 10:43
wongstar
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-10-12
收藏
得分:0 
写的不错,用栈的回溯和数组的数据结构 来完成
2009-10-12 13:46
快速回复:迷宫问题
数据加载中...
 
   



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

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