24小时热门版块排行榜    

查看: 287  |  回复: 0

计算机计科

新虫 (初入文坛)

[求助] 新手求大神指导,,为什么这个运行一下总要循环两次呢?

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define M 8
#define N 8
#define MAXS 100
#define OK 1
#define ERROR 0
typedef int Status;

typedef struct{
        int x, y;
}PosType;
typedef int MazeType[M+2][N+2];

typedef struct
{
        int i;
        int j;
        int di;
}SElemType;

typedef struct
{
        SElemType st[MAXS];
        int top;
}Stack;

Status MazePath(MazeType maze, Stack &S, PosType start, PosType end)
{
        int i, j, di, ftag;
        S.top++;
        S.st[S.top].i=start.x, S.st[S.top].j=start.y, S.st[S.top].di=-1;//入口进栈
        maze[start.x][start.y]= -1;
        while(S.top>-1)
        {
                i = S.st[S.top].i, j= S.st[S.top].j, di= S.st[S.top].di;//读取栈顶元素
                if(end.x==i&&end.y==j)
                        return OK;
                ftag = 0;
                while(di<4&&ftag==0)
                {
                        di++;
                        switch(di)
                        {
                                case 0: i=S.st[S.top].i+1, j=S.st[S.top].j; break; //向下
                                case 1: i=S.st[S.top].i, j=S.st[S.top].j+1; break; //向右
                                case 2: i=S.st[S.top].i-1, j=S.st[S.top].j; break; //向上
                                case 3: i=S.st[S.top].i, j=S.st[S.top].j-1; break; //向左
                        }
                        if(maze[j]==0)
                                ftag = 1;
                }
                if(ftag == 1)
                {
                        S.st[S.top].di = di;
                        S.top++;
                        S.st[S.top].i=i, S.st[S.top].j=j, S.st[S.top].di=-1; //新位置进栈
                        maze[j]=1; //做标记
                }
                else
                {
                        maze[S.st[S.top].i][S.st[S.top].j]=0;
                        S.top--;
                }
        }
        return ERROR;
}

int main()
{       
        char p;
        while(true){
                printf("请输入任意键继续,输入q时退出: ";
                scanf("%c",&p);       
                printf("\n";
                if(p=='q'||p=='Q')
                        break;
                MazeType maze;
                int i, j;
                Stack S;
                S.top = -1;
                PosType start={1,1}, end={M,N};
                srand((unsigned)time(NULL));//随机生成迷宫数组
                for(i=1; i<=M; i++)
                        for(j=1; j<=N; j++)
                                maze[j]=rand()%2;
                for(i=0; i<=M+1; i++)                //迷宫四周为1
                        maze[0]=maze[N+1]=1;
                for(j=0; j<=N+1; j++)
                        maze[0][j]=maze[M+1][j]=1;
                maze[1][1]=maze[M][N]=0;
                printf("迷宫输出:  \n";
                for(i=0; i<=M+1; i++)
                {
                        for(j=0; j<=N+1; j++)
                                printf("%3d", maze[j]);
                        printf("\n";
                }
                if(MazePath(maze, S, start, end))
                {
                        printf("迷宫路径如下:";
                        for (i=0; i<=S.top; i++)
                        {
                                printf("  (%d,%d)", S.st.i, S.st.j);
                                if((i+1)%6 == 0)
                                        printf("\n";
                        }
                        printf("\n";
                }
                else
                        printf("  无路径!\n";
        }
        return 0;
}
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

相关版块跳转 我要订阅楼主 计算机计科 的主题更新
信息提示
请填处理意见