| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 999 人关注过本帖
标题:用栈求解迷宫,由于基础不牢,编得很乱,出不了正确结果,望高人指教。。。 ...
取消只看楼主 加入收藏
tzhg555
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-12-5
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
用栈求解迷宫,由于基础不牢,编得很乱,出不了正确结果,望高人指教。。。谢谢!!!
# include <stdio.h>
# include <malloc.h>
typedef struct
{
    int x,y;
    int flag;
}postype,posch[20][20];

typedef struct
{
    int ord;
    postype seat;
    int di;
}selemtype;

typedef struct
{
    posch a;
    int m;
    int n;
    int t;
    postype start,end;
}mazetype;

typedef struct sqstack
{
    selemtype *top;
    selemtype *base;
    int stacksize;
}sqstack;
sqstack initstack(sqstack S)
{
    S.base=(selemtype *)malloc(100*sizeof(selemtype));
    S.top=S.base;
    S.stacksize=100;
    return S;
}
sqstack push(sqstack S,selemtype *e)
{
    *S.top=*e;
    S.top++;
    return S;
}
sqstack pop(sqstack S,selemtype *e)
{
    S.top--;
    *e=*S.top;
    return S;
}
stackempty(sqstack S)
{
    if(S.base==S.top)
        return 1;
    else
        return 0;
}
postype nextpos(postype Q,int n)
{
    switch(n)
    {
    case 1: Q.y++;break;
    case 2: Q.x++;break;
    case 3: Q.y--;break;
    case 4: Q.x--;break;
    }
    return Q;
}
mazetype creat(mazetype M)
{
    int i,j,p,q;
    scanf("%d%d%d",&M.m,&M.n,&M.t);
    for(i=0;i<=M.m;i++)   
        for(j=0;j<=M.n;j++)
        {
                M.a[i][j].flag=0;
        }
    for(i=0;i<M.t;i++)
    {
        scanf("%d%d",&p,&q);
        scanf("%d",&M.a[p][q].flag);
    }
    return M;
}
void main()
{
   sqstack S;
   mazetype maze;
   int curstep=1;
   selemtype *e;
   postype curpos;
   e=(selemtype *)malloc(sizeof(selemtype));
   maze=creat(maze);
   scanf("%d%d%d%d%d%d",&maze.start.x,&maze.start.y,&maze.start.flag,&maze.end.x,&maze.end.y,&maze.end.flag);
   S=initstack(S);
   curpos=maze.start;
   do
   {
       if(maze.a[curpos.x][curpos.y].flag)
       {
           curpos.flag=0;
           e->ord=curstep;
           e->seat=curpos;
           e->di=1;
           S=push(S,e);
           if(curpos.x==maze.end.x&&curpos.y==maze.end.y)
               break;
           curpos=nextpos(curpos,1);
           curstep++;
       }
       else
       {
           if(!stackempty(S))
           {
               S=pop(S,e);
               while(e->di==4&&!stackempty(S))
               {
                   e->seat.flag=0;
                   S=pop(S,e);
               }
               if(e->di<4)
               {
                   e->di++;
                   S=push(S,e);
                   curpos=nextpos(e->seat,e->di);
               }
           }
       }
   }while(!stackempty(S));
   while(!stackempty(S))
   {
       S=pop(S,e);
       printf("(%d,%d)",e->seat.x,e->seat.y);
   }
}

搜索更多相关主题的帖子: 迷宫 高人 基础 求解 结果 
2010-12-14 12:16
tzhg555
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-12-5
收藏
得分:0 
这解法和我的想法不一样,我得好好消化去,谢谢啦!!!
2010-12-14 22:42
tzhg555
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-12-5
收藏
得分:0 
谁理解我想法的帮我改改,谢谢。。。。
2010-12-14 22:43
tzhg555
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-12-5
收藏
得分:0 
# include <stdio.h>
# include <malloc.h>
typedef struct
{
    int x,y;
    int flag; //flag=0,表示不可通过
}postype;

typedef struct
{
    int ord;//通道块在路径上的‘序号’
    postype seat;//通道块在迷宫中的‘坐标位置’
    int di;//此通道块走向下一通道块的‘方向’
}selemtype;//栈的元素类型

typedef struct
{
    postype a[20][20];
    int m;  //迷宫行数
    int n;  //迷宫列数
    int t;  //迷宫中可通过的块的个数
    postype start,end;
}mazetype;

typedef struct sqstack
{
    selemtype *top;
    selemtype *base;
    int stacksize;
}sqstack;
sqstack initstack(sqstack S)
{
    S.base=(selemtype *)malloc(100*sizeof(selemtype));
    S.top=S.base;
    S.stacksize=100;
    return S;
}
sqstack push(sqstack S,selemtype *e)
{
    *S.top=*e;
    S.top++;
    return S;
}
sqstack pop(sqstack S,selemtype *e)
{
    S.top--;
    *e=*S.top;
    return S;
}
stackempty(sqstack S)
{
    if(S.base==S.top)
        return 1;
    else
        return 0;
}
postype nextpos(postype Q,int n)//求下一步的位置
{
    switch(n)
    {
    case 1: Q.y++;break;
    case 2: Q.x++;break;
    case 3: Q.y--;break;
    case 4: Q.x--;break;
    }
    return Q;
}
mazetype creat(mazetype M)
{
    int i,j,p,q;
    scanf("%d%d%d",&M.m,&M.n,&M.t);
    for(i=0;i<=M.m;i++)   
        for(j=0;j<=M.n;j++)
        {
                M.a[i][j].flag=0;
        }
    for(i=0;i<M.t;i++)
    {
        scanf("%d%d",&p,&q);
        scanf("%d",&M.a[p][q].flag);//输入迷宫可通过位置的坐标
    }
    for(i=0;i<=M.m;i++)//输出迷宫
    {
        for(j=0;j<=M.n;j++)
        {
            if(M.a[i][j].flag==0)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
    return M;
}
void main()
{
   sqstack S;
   mazetype maze;
   int curstep=1; //走第一步
   selemtype *e;
   postype curpos;
   e=(selemtype *)malloc(sizeof(selemtype));
   maze=creat(maze);
   scanf("%d%d%d%d%d%d",&maze.start.x,&maze.start.y,&maze.start.flag,&maze.end.x,&maze.end.y,&maze.end.flag);
   S=initstack(S);
   curpos=maze.start;
   do
   {
       if(maze.a[curpos.x][curpos.y].flag)  //当前位置可通过
       {
           curpos.flag=0;  //留下足迹
           e->ord=curstep;
           e->seat=curpos;
           e->di=1;
           S=push(S,e);
           if(curpos.x==maze.end.x&&curpos.y==maze.end.y)
               break;  //找到终点
           curpos=nextpos(curpos,1);
           curstep++;  //探索下一步
       }
       else  //当前位置不可通过
       {
           if(!stackempty(S))
           {
               S=pop(S,e);
               while(e->di==4&&!stackempty(S))
               {
                   e->seat.flag=0;
                   S=pop(S,e);
               }
               if(e->di<4)
               {
                   e->di++;
                   S=push(S,e);
                   curpos=nextpos(e->seat,e->di);
               }
           }
       }
   }while(!stackempty(S));
   while(!stackempty(S))  //输出栈存的路径
   {
       S=pop(S,e);
       printf("(%d,%d)",e->seat.x,e->seat.y);
   }
}

2010-12-16 13:24
tzhg555
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-12-5
收藏
得分:0 
比如我可以输入
4 4 5
1 1 1
2 1 1
3 1 1
3 2 1
3 3 1
他就出现迷宫
再输入
1 1 1
3 3 1
就可以有路径
但是,复杂的迷宫
如:
5 7 12
1 1 1
1 4 1
2 1 1
2 4 1
2 5 1
2 6 1
3 3 1
3 2 1
3 3 1
3 4 1
4 1 1
5 1 1
就不行了。。。
2010-12-16 13:30
tzhg555
Rank: 1
等 级:新手上路
帖 子:10
专家分:1
注 册:2010-12-5
收藏
得分:0 
# include <stdio.h>
# include <malloc.h>
typedef struct
{
    int x,y;
    int flag; //flag=0,表示不可通过
}postype;

typedef struct
{
    int ord;//通道块在路径上的‘序号’
    postype seat;//通道块在迷宫中的‘坐标位置’
    int di;//此通道块走向下一通道块的‘方向’
}selemtype;//栈的元素类型

typedef struct
{
    postype a[20][20];
    int m;  //迷宫行数
    int n;  //迷宫列数
    int t;  //迷宫中可通过的块的个数
    postype start,end;
}mazetype;

typedef struct sqstack
{
    selemtype *top;
    selemtype *base;
    int stacksize;
}sqstack;
sqstack initstack(sqstack S)
{
    S.base=(selemtype *)malloc(100*sizeof(selemtype));
    S.top=S.base;
    S.stacksize=100;
    return S;
}
sqstack push(sqstack S,selemtype *e)
{
    *S.top=*e;
    S.top++;
    return S;
}
sqstack pop(sqstack S,selemtype *e)
{
    S.top--;
    *e=*S.top;
    return S;
}
stackempty(sqstack S)
{
    if(S.base==S.top)
        return 1;
    else
        return 0;
}
postype nextpos(postype Q,int n)//求下一步的位置
{
    switch(n)
    {
    case 1: Q.y++;break;
    case 2: Q.x++;break;
    case 3: Q.y--;break;
    case 4: Q.x--;break;
    }
    return Q;
}
mazetype creat(mazetype M)
{
    int i,j,p,q;
    scanf("%d%d%d",&M.m,&M.n,&M.t);
    for(i=0;i<=M.m;i++)   
        for(j=0;j<=M.n;j++)
        {
                M.a[i][j].flag=0;
        }
    for(i=0;i<M.t;i++)
    {
        scanf("%d%d",&p,&q);
        scanf("%d",&M.a[p][q].flag);//输入迷宫可通过位置的坐标
    }
    for(i=0;i<=M.m;i++)//输出迷宫
    {
        for(j=0;j<=M.n;j++)
        {
            if(M.a[i][j].flag==0)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
    return M;
}
void main()
{
   sqstack S;
   mazetype maze;
   int curstep=1; //走第一步
   selemtype *e;
   postype curpos;
   e=(selemtype *)malloc(sizeof(selemtype));
   maze=creat(maze);
   scanf("%d%d%d%d%d%d",&maze.start.x,&maze.start.y,&maze.start.flag,&maze.end.x,&maze.end.y,&maze.end.flag);
   S=initstack(S);
   curpos=maze.start;
   do
   {
       if(maze.a[curpos.x][curpos.y].flag)  //当前位置可通过
       {
          maze.a[curpos.x][curpos.y].flag=0;  //留下足迹
           e->ord=curstep;
           e->seat=curpos;
           e->di=1;
           S=push(S,e);
           if(curpos.x==maze.end.x&&curpos.y==maze.end.y)
               break;  //找到终点
           curpos=nextpos(curpos,1);
           curstep++;  //探索下一步
       }
       else  //当前位置不可通过
       {
           if(!stackempty(S))
           {
               S=pop(S,e);
               while(e->di==4&&!stackempty(S))
               {
                   e->seat.flag=0;
                   S=pop(S,e);
               }
               if(e->di<4)
               {
                   e->di++;
                   S=push(S,e);
                   curpos=nextpos(e->seat,e->di);
               }
           }
       }
   }while(!stackempty(S));
   while(!stackempty(S))  //输出栈存的路径
   {
       S=pop(S,e);
       printf("(%d,%d)",e->seat.x,e->seat.y);
   }
}
调试的时候发现错哪了,现在改回来了,谢谢大家哈!!!
2010-12-17 13:09
快速回复:用栈求解迷宫,由于基础不牢,编得很乱,出不了正确结果,望高人指教。 ...
数据加载中...
 
   



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

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