注册 登录
编程论坛 数据结构与算法

我这个用栈解迷宫程序哪里错了

飞黄腾达 发布于 2013-04-21 22:58, 658 次点击
#include<stdio.h>
#include<stdlib.h>
#define m 6
#define n 8
#define MAXSIZE 100
typedef struct{
    int x;
    int y;
    int d;
}Lacate;
typedef struct{
    Lacate La[MAXSIZE];
    int top;
}*pstack,stack;
typedef struct{
    int x;
    int y;
}iterm;

int judge(pstack st)
{
    if(st->top!=-1)
        return 1;
    else return 0;
}
void pop_stack(pstack S,Lacate *te)
{
       *te=S->La[S->top];
       S->top--;
}
pstack creat_stack()
{
    pstack S;
    S=(pstack)malloc(sizeof(pstack));
      
    S->top=-1;
       return S;
}
void push_stack(pstack S,Lacate te)
{
    S->top++;
    S->La[S->top]=te;
}

void mazepath(int maze[][10],iterm move[],int x0,int y0)
{
    int x,y,d,i,j;
    pstack s;
    Lacate term;
    x=x0;
    y=y0;
    d=-1;
    term.x=x0;term.y=y0;term.d=d;
    s=creat_stack();
   
    push_stack(s,term);
   
   
   
    if(judge(s))
    {
        pop_stack(s,&term);
        x=term.x;y=term.y;d=term.d+1;
        if(d<4)
        {
            i=x+move[d].x;j=y+move[d].y;
            
            if(maze[i][j]!=1)
            {
               
                term.x=x;term.y=y;term.d=d;
                push_stack(s,term);
                x=i;y=j;
                maze[x][y]=-1;
                if(x==m&&y==n)
                {
                    while(judge(s))
                    {
                        pop_stack(s,&term);
                        printf("(%d,%d)->",term.x,term.y);
                    }
                }
                else d=0;
            }
            else
            d++;
        }
    }

}




int main()
{
    int x=0,y=0;
    int a[m+2][n+2]={1,1,1,1,1,1,1,1,1,1,
                     1,0,1,1,1,0,1,1,1,1,
                     1,0,0,0,0,1,1,1,1,1,
                     1,0,1,0,0,0,0,0,1,1,
                     1,0,1,1,1,0,0,1,1,1,
                     1,1,0,0,1,1,0,0,0,1,
                     1,0,1,1,0,0,1,1,0,1,
                     1,1,1,1,1,1,1,1,1,1};
    iterm m[4]={
         {1,0},{-1,0},{0,1},{0,-1}
    };
   
    mazepath(a,m,x,y);
}
5 回复
#2
azzbcc2013-04-22 07:48
mazepath函数里面都是 if(),执行一遍就结束了,不知道你要怎么实现

另外那个 iterm m[4] 你编译通过?
#3
飞黄腾达2013-04-22 12:49
哦,前两个if应改成while,我编译就是main函数不通过
--------------------Configuration: 迷宫递归 - Win32 Debug--------------------
Compiling...
迷宫递归.c
F:\数据结构上机\迷宫递归.c(106) : error C2143: syntax error : missing ';' before 'constant'
F:\数据结构上机\迷宫递归.c(106) : error C2109: subscript requires array or pointer type
F:\数据结构上机\迷宫递归.c(106) : error C2059: syntax error : '{'
F:\数据结构上机\迷宫递归.c(110) : warning C4047: 'function' : 'struct iterm *' differs in levels of indirection from 'const int '
F:\数据结构上机\迷宫递归.c(110) : warning C4024: 'mazapath' : different types for formal and actual parameter 2
执行 cl.exe 时出错.

迷宫递归.obj - 1 error(s), 0 warning(s)

但我另写程序测试iterm[4]是可以通过的,我觉得main函数应该没有错误
#4
azzbcc2013-04-22 13:34
编译不过是因为 你 define m 6

后面又 iterm m[4] ,换个变量名就可以了
#5
飞黄腾达2013-04-25 12:59
回复 4楼 azzbcc
多谢
#6
C_放飞梦想2013-04-25 20:57
都没有注释噢。好些表示看不懂啊
1