转载的迷宫程序

来源:互联网 发布:部落冲突黑气球数据 编辑:程序博客网 时间:2024/04/30 12:18

 代码出处: #include<stdio.h>

002  
003#include<iostream>
004#include<stdlib.h>
005#include<time.h>
006#define STACK_INIT_SIZE 1000
007#define STACKINCREMENT 10
008#define OK 1
009#define OVERFLOW 0
010#define ERROR 0
011   
012//typedef int SElemType;
013typedef struct SElemType{
014    int x;int y;
015};
016typedef struct{
017    SElemType *base;
018    SElemType *top;
019    int stacksize;
020}SqStack;
021int InitStack(SqStack &S){
022    S.base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
023    if(!S.base) exit(OVERFLOW);
024    S.top=S.base;
025    S.stacksize=STACK_INIT_SIZE;
026    return OK;
027}
028void Push(SqStack &S,SElemType &e){
029    if(S.top-S.base>=S.stacksize){
030        S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
031        if(!S.base) exit (OVERFLOW);
032        S.top=S.base+S.stacksize;
033        S.stacksize+=STACKINCREMENT;
034    }
035    *S.top++=e;
036}
037SElemType Pop(SqStack &S,SElemType &e){
038    //if(S.top==S.base) return ERROR;
039    e=*--S.top;
040    return e;
041}
042SElemType GetTop(SqStack S,SElemType &e){
043    e=*(S.top-1);
044    return e;
045}
046   
047int StackEmpty(SqStack &S)
048 {//若栈S为空栈,则返回OK,否则返回ERROR
049    if(S.base==S.top)
050        return OK;
051    else
052        return ERROR;
053}
054   
055   
056int main()
057{
058    int m;
059    SqStack S;
060    InitStack(S);
061    printf("请输入迷宫的大小(m*m)");
062    scanf("%d",&m);
063    //数组的形式表示八个方向
064    int move[8][2]={{0,1},{1,1},{1,0},
065                    {1,-1},{0,-1},{-1,-1},{-1, 0},{-1, 1}};
066      
067    //用结构体表示位置
068    struct position
069    {
070        int x,y;
071    };
072    //用于记录和输出迷宫探路中相关符号,包括1 .
073    char maze[20][20];
074    //用栈来存储探路过程中的数据
075    int i,x,y,ok;
076    SElemType p;
077    //二维数组的第0行、第m+1行、第0列、第m+1列元素全
078    //置成“1”,表示迷宫的边界;第1行第1列元素和第m行第m列
079    //元素置成“0”,表示迷宫的入口和出口;其余元素值用随机
080    //函数产生。
081    srand(time(0));
082    for(x=1;x<=m;x++)
083        for(y=1;y<=m;y++)
084            maze[x][y]=48+rand()%2;
085    maze[1][1]='0';maze[m][m]='0';
086    for(x=0;x<=m+1;x++)
087    {
088        maze[x][0]='1';maze[x][m+1]='1';
089    }
090    for(y=0;y<=m+1;y++)
091    {
092        maze[0][y]='1';maze[m+1][y]='1';
093    }
094    p.x=1;p.y=1;
095    Push(S,p);
096    maze[1][1]='.';
097    printf("迷宫初始为(m*m)/n");
098    for(x=1;x<=m;x++)
099    {
100        printf("/n");
101        for(y=1;y<=m;y++) 
102            printf("%c ",maze[x][y]);
103              
104    }
105    printf("/n");
106      
107    //开始探路
108    do{
109        GetTop(S,p);
110        ok=0;i=0;
111        while((ok==0)&&(i<8))
112        {
113            x=p.x+move[i][0];
114            y=p.y+move[i][1];
115            if(maze[x][y]=='0')
116            {
117                p.x=x;p.y=y;
118                Push(S,p);
119                maze[x][y]='.';
120                ok=1;
121            }
122            i++;
123        }
124        if(i==8)
125        {
126            maze[p.x][p.y]='*';
127            Pop(S,p);
128        }
129    }while((!StackEmpty(S))&&((p.x!=m)||(p.y!=m)));
130    //输出探路结果
131    if(StackEmpty(S)) printf("没有路径/n");
132    else printf("有路径/n");
133    //输出探路迷宫留下的踪迹
134    for(x=1;x<=m;x++)
135    {
136        printf("/n");
137        for(y=1;y<=m;y++) 
138            printf("%c ",maze[x][y]);
139              
140    }
141    printf("/n");
142    system("pause");
143   
144}

http://www.oschina.net/code/snippet_110108_2184