| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4227 人关注过本帖
标题:求一个c语言编的迷宫代码
只看楼主 加入收藏
lianghao23
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-1-5
收藏
 问题点数:0 回复次数:8 
求一个c语言编的迷宫代码
求一个c语言编的迷宫代码
搜索更多相关主题的帖子: c语言 迷宫 代码 
2010-01-05 16:18
佳嘉
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:3
帖 子:534
专家分:1383
注 册:2009-11-8
收藏
得分:0 
要么在网上找,要么自己编写
2010-01-05 18:07
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:0 
不少人写不出来,你求也没用

我就是真命天子,顺我者生,逆我者死!
2010-01-05 18:52
flylee
Rank: 5Rank: 5
等 级:职业侠客
帖 子:309
专家分:374
注 册:2004-8-10
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"

#define MAP_SIZE 50        //迷宫的大小

struct node {
    char dir;            //方向标记
    struct list_head list;
};

/***************************
* 数据文件格式:
* 文件的第一行应该给出迷宫的宽和高n,m
* 接下来的m行表示了迷宫的形态
* "#"表示障碍物,"."表示可通过区域,"S"表示起点,"E"表示出口
* 如果存在起点到出口的路径,输出用"*"表示路线
***************************/
int main(int argc, char* argv[])
{
    struct node trace, *tmp;
    struct list_head *ltmp, *q;
    FILE *fp;
    char filename[255];
    char pos[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};    //方向向量
    char map[MAP_SIZE][MAP_SIZE];
    char found, old;
    int count, width, height;
    int i, j, x, y;

    if(argc==1) {
        printf("please specify the data file: ");
        scanf("%s", filename);
    }
    else
        strcpy(filename, argv[1]);

    fp = fopen(filename, "r");
    if(!fp) {
        fprintf(stderr, "open file %s failed.\n", filename);
        return 1;
    }

    memset(map, 0, sizeof(char)*MAP_SIZE*MAP_SIZE);
    INIT_LIST_HEAD(&trace.list);
    trace.dir = -1;

    fscanf(fp, "%d%d", &width, &height);

    for(i=0; i<height; ++i) {
        getc(fp);
        for(j=0; j<width; ++j) {
            map[i][j] = getc(fp);
            if(map[i][j] == 'S') {
                x = i;
                y = j;
            }
        }
    }
    fclose(fp);

    count = 0;
    found = 0;
    i = 0;
    old = 0;
    while(!found){
        tmp = list_entry(trace.list.prev, struct node, list);
        for(; i<4; ++i){
            if(i != 3 - tmp->dir &&
                    0<=x+pos[i][0] && x+pos[i][0]<height &&
                    0<=y+pos[i][1] && y+pos[i][1]<width &&
                    map[x+pos[i][0]][y+pos[i][1]] != '#' &&
                    map[x+pos[i][0]][y+pos[i][1]] != '@') {
                x += pos[i][0];
                y += pos[i][1];
                if(!old) {
                    tmp = (struct node*)malloc(sizeof(struct node));
                    list_add_tail(&(tmp->list), &(trace.list));
                }
                tmp->dir = i;
                ++count;
                if(map[x][y] == 'E')
                    found = 1;
                map[x][y] = '@';
                break;
            }
        }
        if(i == 4) {
            if(list_empty(&trace.list)) {
                found = 1;
            }
            else {
                x -= pos[tmp->dir][0];
                y -= pos[tmp->dir][1];
                list_del(trace.list.prev);
                free(tmp);
                --count;
                i = (list_entry(trace.list.prev, struct node, list) -> dir) + 1;
                old = 1;
            }
        }
        else {
            i = 0;
            old = 0;
        }
    }
    if(i==4) {
        printf("no way to get the exit.\n");
    }
    else {
        for(i=0; i<height; ++i)
            for(j=0; j<width; ++j)
                if(map[i][j]=='@')
                    map[i][j] = '.';
        list_for_each_prev(ltmp, &trace.list) {
            tmp = list_entry(ltmp, struct node, list);
            map[x][y] = '*';
            x -= pos[tmp->dir][0];
            y -= pos[tmp->dir][1];
        }
        map[x][y] = '*';
        printf("found it\n");
        for(i=0; i<height; ++i) {
            for(j=0; j<width; ++j)
                printf("%c", map[i][j]);
            printf("\n");
        }
    }
    list_for_each_safe(ltmp, q, &trace.list){
        tmp= list_entry(ltmp, struct node, list);
        list_del(ltmp);
        free(tmp);
    }
    return 0;
}


前段时间一个选修课的作业,主要是练习linux内核链表,list_***的函数都是进行链表操作的
基本思路就是裸搜,如果迷宫太大的话就很慢
仅仅提供一个思路,想要运行的话需要自己把栈实现一下
2010-01-05 19:23
一尛星一
Rank: 2
来 自:湖北孝感
等 级:论坛游民
帖 子:41
专家分:49
注 册:2009-10-17
收藏
得分:0 
程序代码:
这是我以前刚学不久写的迷宫!可能很粗糙!!!

/*
时间:time 迷宫:labyrinth 初始:initial 地点:location 执行:execute 人:person
上:up 下:down 左:left 右:right 终点:destination 空:empty 横:horizontal
竖:vertical 长:length 宽:width 坐标:coordinate
*/

#include<stdio.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
/*--------------------------迷宫地图-------------------------*/
int horizontal=1,vertical=0;
;
int length=20,width=70;
int person=2,map[20][70]={
                            {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
                            { 2 ,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#'},
                            {'#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#',' ','#','#','#','#','#','#','#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#'},
                            {'#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#','#',' ',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#',' ','#'},
                            {'#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#','#',' ','#',' ','#','#',' ',' ',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#','#','#',' ','#'},
                            {'#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#',' ','#',' ','#','#','#','#','#','#',' ','#','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ','#'},                
                            {'#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ','#','#',' ','#','#','#','#','#','#','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#','#','#','#','#',' ','#'},
                            {'#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#','#','#','#','#','#','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ','#',' ','#'},
                            {'#',' ','#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#',' ','#','#','#','#','#','#','#',' ','#','#','#','#','#','#','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#'},
                            {'#',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#','#','#',' ','#'},
                            {'#',' ','#',' ','#',' ',' ',' ','#',' ','#','#','#',' ','#',' ','#','#','#','#','#','#','#','#',' ','#',' ',' ',' ','#','#','#',' ',' ',' ','#',' ',' ',' ',' ',' ','#','#','#','#','#',' ','#','#','#','#','#','#','#','#','#','#','#','#',' ',' ','#','#','#','#','#','#','#',' ','#'},
                            {'#',' ','#',' ','#','#','#','#','#','#','#','#',' ',' ','#',' ','#',' ',' ',' ',' ','#','#','#',' ','#','#','#','#','#','#','#',' ','#',' ','#',' ','#','#','#',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#',' ','#'},
                            {'#',' ','#',' ','#',' ',' ',' ',' ',' ',' ','#',' ','#','#',' ','#',' ','#','#',' ','#','#','#',' ',' ',' ',' ',' ','#','#','#',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#',' ','#','#','#','#',' ',' ',' ','#',' ','#'},
                            {'#',' ','#',' ','#',' ','#','#','#','#',' ','#',' ',' ',' ',' ','#',' ',' ','#',' ','#','#','#','#','#','#','#','#',' ',' ',' ',' ','#','#','#','#',' ',' ','#',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#','#','#','#','#','#',' ','#'},
                            {'#',' ','#',' ','#',' ',' ',' ',' ','#',' ','#','#','#','#','#','#','#',' ','#',' ','#','#','#','#','#','#','#','#',' ','#','#',' ','#','#','#','#',' ','#','#',' ','#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ',' ',' ',' ',' ','#',' ','#'},
                            {'#',' ','#',' ','#','#','#','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#',' ',' ',' ',' ',' ',' ','#','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#','#','#',' ','#'},
                            {'#',' ','#',' ',' ',' ',' ',' ',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ',' ',' ',' ','#',' ','#'},
                            {'#',' ','#','#','#','#','#','#','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ','#','#','#','#','#','#','#','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#','#','#',' ',' ',' ','#','#','#','#','#',' ','#','#',' ','#',' ','#'},
                            {'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
                            {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
                            
};


/*------------------------------------------------------------*/

void execute();/*执行函数声明*/
void maps();   /*地图函数声明*/
void interface_main();/*主界面*/
void interface_options();/*胜利语*/
void interface_last();    /*退出语*/
void up();    /*向上执行函数声明*/
void down();    /*向下执行函数声明*/
void left();      /*向左执行函数声明*/
void right();    /*向右执行函数声明*/
void soul();     /*灵魂函数声明*/

void main()           /*主函数*/
{

/*  interface_main();
    maps();
*/
    execute();                 /*执行方向操作函数调用*/
    maps();
    interface_last();           /*退出语*/
    getch();
}





void interface_main()              /*主界面函数*/
{
    printf("--------------wlecome use----------------\n");
    printf("             time 09.07.20               \n");
    printf("             author snoopy                \n");
    printf("ESC(shut)\n");

}
void interface_options()       /*胜利语*/
{
    printf("congratulate pass a test ! ! !    ^-^");
    getch();
    exit(0);
}
void interface_last()         /*退出语*/
{
    printf("good bay ! ! !   ^-^");
}
void execute()          /*方向执行函数*/
{
    int key;
    do
    {
        system("cls");
        interface_main();
        maps();
        key=bioskey(0);
        system("cls");
        interface_main();
        if(key==UP||key=='8')            /*当按上按键时执行返回值为1*/
        {
            up();            /*调用上方向执行函数*/
        }
        if(key==DOWN)
        {
            down();
        }
        if(key==LEFT)
        {
            left();
        }
        if(key==RIGHT)
        {
            right();
        }
        soul();                 /*调用灵魂函数*/
    }while(key!=ESC);

}

void maps()                 /*迷宫输出函数*/
{
    int i,j;
    for(i=0;i<length;i++)
    {
        printf("     ");
        for(j=0;j<width;j++)
        {
            printf("%c",map[i][j]);
        }
        printf("\n");
    }
}
void up()       /*上方向执行函数*/
{
    int t;
    if(map[horizontal-1][vertical]=='*')                 /*当即将要走到得地方为‘*‘时则返回1*/
    {
        map[horizontal-1][vertical]=map[horizontal][vertical];
        map[horizontal][vertical]=' ';
        maps();
        interface_options();
    }
    if(map[horizontal-1][vertical]==' ')
    {
        horizontal=horizontal-1;
        t=map[horizontal][vertical];
        map[horizontal][vertical]=map[horizontal+1][vertical];
        map[horizontal+1][vertical]=t;
    }
    maps();

}
void down()                   /*下方向执行函数*/
{
    int t;
    if(map[horizontal+1][vertical]=='*')
    {
        map[horizontal+1][vertical]=map[horizontal][vertical];
        map[horizontal][vertical]=' ';
        maps();
        interface_options();
    }
    if(map[horizontal+1][vertical]==' ')
    {
        horizontal=horizontal+1;
        t=map[horizontal][vertical];
        map[horizontal][vertical]=map[horizontal-1][vertical];
        map[horizontal-1][vertical]=t;
    }
    maps();
}
void left()          /*左方向执行函数*/
{
    int t;
    if(map[horizontal][vertical-1]=='*')
    {
        map[horizontal][vertical-1]=map[horizontal][vertical];
        map[horizontal][vertical]=' ';
        maps();
        interface_options();
    }
    if(map[horizontal][vertical-1]==' ')
    {
        vertical=vertical-1;
        t=map[horizontal][vertical];
        map[horizontal][vertical]=map[horizontal][vertical+1];
        map[horizontal][vertical+1]=t;
    }
    maps();
}
void right()        /*右方向执行函数*/
{
    int t;
    if(map[horizontal][vertical+1]=='*')
    {
        map[horizontal][vertical+1]=map[horizontal][vertical];
        map[horizontal][vertical]=' ';
        maps();
        interface_options();
    }
    if(map[horizontal][vertical+1]==' ')
    {
        vertical=vertical+1;
        t=map[horizontal][vertical];
        map[horizontal][vertical]=map[horizontal][vertical-1];
        map[horizontal][vertical-1]=t;
    }
    maps();
}
void soul()
{
    if(map[18][63]==2)
    {
        map[18][67]='#';
    }
    if(map[18][65]==2)
    {
        map[18][64]='#';
        map[1][68]=' ';
    }
    if(map[18][58]==2)
    {
        map[18][67]=' ';
        map[18][64]=' ';
    }

    if(map[16][68]==2)
    {
        map[15][68]='#';
        map[4][68]='#';
    }
    if(map[18][68]==2)
    {
        map[18][69]='#';
        map[5][69]='*';
        map[5][67]=' ';
        map[11][2]=' ';
    }

    if(map[5][65]==2)
    {
        map[5][67]='#';
        map[9][67]=' ';
        map[9][66]=' ';
    }

    if(map[9][66]==2)
    {
        map[9][67]='#';
        map[14][67]=' ';
    }

    if(map[15][28]==2)
    {
        map[14][29]='#';
        map[12][29]=' ';
    }
    if(map[12][36]==2)
    {
        map[10][36]='#';
        map[11][38]=' ';
    }
    if(map[14][67]==2)
    {
        map[12][67]=' ';
        map[13][68]='#';
    }
    
    if(map[7][59]==2)
    {
        map[7][60]='#';
    }
    if(map[9][58]==2)
    {
        map[9][59]='#';
    }
    if(map[13][58]==2)
    {
        map[12][59]='#';
    }
    if(map[12][67]==2)
    {
        map[5][69]='#';
        map[1][0]='*';
    }
}
2010-01-06 08:47
转身
Rank: 2
等 级:论坛游民
帖 子:58
专家分:25
注 册:2010-1-4
收藏
得分:0 

没有谁对谁错,只有谁不懂得珍惜
2010-01-07 12:29
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
以下是引用一尛星一在2010-1-6 08:47:37的发言:

这是我以前刚学不久写的迷宫!可能很粗糙!!!

/*
时间:time 迷宫:labyrinth 初始:initial 地点:location 执行:execute 人:person
上:up 下:down 左:left 右:right 终点:destination 空:empty 横:horizontal
竖:ve ...

不是一般的粗糙。。。
2010-01-07 15:22
我爱问路
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-10-18
收藏
得分:0 
   继续努力
2010-10-18 15:34
洪梅
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-10-23
收藏
得分:0 
刚刚在网上找的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define UNDERFLOW -2
typedef int Status;
//-----栈开始-----
 
typedef struct//迷宫中r行c列的位置
{
    int r;  
    int c;
}PostType;//坐标位置类型

typedef struct
{  
    int ord;// 当前位置在路径上的序号  
    PostType seat;// 当前坐标  
    int di;// 从此通块走向下一通块的“方向”
}SElemType;// 栈的元素类型  
//定义链式栈的存储结构

struct LNode
{   
    SElemType data;//数据域   
    struct LNode *next;//指针域  
};

struct LStack
{  
    struct LNode *top;//栈顶指针
};

Status InitStack(LStack &s)//操作结果:构造一个空栈S
 {
    struct LNode *p;
    p=(LNode *)malloc(sizeof(LNode));  
    if(!p)  
    {
        printf("分配失败,退出程序");         
        exit(ERROR);   
    }  
    s.top=p;  
    p->next=NULL;  
    return OK;
}

Status StackEmpty(LStack s)    //若栈s为空栈,则返回TRUE,否则FALSE
{  
    if(s.top->next==NULL)
        return TRUE;  
    return FALSE;
}
 
Status Push(LStack &s,SElemType e)//插入元素e成为新的栈顶元素
{  
    struct LNode *p;
    p=(LNode *)malloc(sizeof(LNode));  
    if(!p)
        exit(OVERFLOW);  
    s.top->data=e;
    p->next=s.top;   
    s.top=p;
    return OK;
}

Status Pop(LStack &s,SElemType &e)//删除s的栈顶元素,并且用e返回其值
{  
    struct LNode *p;
    if(!(s.top->next))
        exit(UNDERFLOW);  
    p=s.top;   
    s.top=p->next;  
    e=s.top->data;  
    free(p);   
    return OK;
}

Status DestroyStack(LStack &s)//操作结果:栈s被销毁
{  
    struct LNode *p;  
    p=s.top;   
    while(p)   
    {   
        s.top=p->next;   
        free(p);   
        p=s.top;  
    }
    return OK;
}
//-----栈结束------  

//-----迷宫开始-------
#define MAXLEN 10// 迷宫包括外墙最大行列数
typedef struct
{  
    int r;
    int c;  
    char adr[MAXLEN][MAXLEN];// 可取' ''*' '@' '#'
}MazeType;// 迷宫类型

Status InitMaze(MazeType&maze)// 初始化迷宫,成功返回TRUE,否则返回FALSE  
{
    int m,n,i,j;
    printf("输入迷宫行数和列数(包括了外墙): ");  
    scanf("%d%d",&maze.r,&maze.c); // 输入迷宫行数和列数  
    for(i=0;i<=maze.c+1;i++)  
    {// 迷宫行外墙   
        maze.adr[0][i]='#';
        maze.adr[maze.r+1][i]='#';
}//for
    for(i=0;i<=maze.r+1;i++)  
    {// 迷宫列外墙   
        maze.adr[i][0]='#';   
        maze.adr[i][maze.c+1]='#';  
    }
    for(i=1;i<=maze.r;i++)  
        for(j=1;j<=maze.c;j++)
            maze.adr[i][j]=' ';// 初始化迷宫
        printf("输入障碍的坐标((-1 -1)结束): ");  
        scanf("%d%d",&m,&n);// 接收障碍的坐标  
        while(m!=-1)  
        {   
            if(m>maze.r || n>maze.c)// 越界   
                exit(ERROR);
            maze.adr[m][n]='#';// 迷宫障碍用#标记?   
            printf("输入障碍的坐标((-1,-1)结束): ");   
            scanf("%d%d",&m,&n);
        }//while  
        return OK;
}//InitMaze  

Status Pass(MazeType maze,PostType curpos)
{// 当前位置可同则返回TURE,否则返回FALSE  
    if(maze.adr[curpos.r]  [curpos.c]==' ')// 可通  
        return TRUE;  
    else
        return FALSE;
}//Pass  

Status FootPrint(MazeType &maze,PostType curpos)
{// 若走过并且可通,则返回TRUE,否则返回FALSE  
    maze.adr[curpos.r]
        [curpos.c]='*';//"*"表示可通
    return OK;
}//FootPrint

PostType NextPos(PostType &curpos,int i) {// 指示并返回下一位置的坐标  
    PostType cpos;  
    cpos=curpos;
    switch(i)
    {//1.2.3.4 分别表示东南西北方向  
    case 1 : cpos.c+=1; break;  
    case 2 : cpos.r+=1; break;  
    case 3 : cpos.c-=1; break;  
    case 4 : cpos.r-=1; break;  
    default: exit(ERROR);  
    }
    return cpos;
}//Nextpos

Status MarkPrint(MazeType &maze,PostType curpos)
{// 曾走过,但不是通路标记,并返回OK  
    maze.adr[curpos.r][curpos.c]='@';//"@" 表示曾走过但不通
    return OK;
}//MarkPrint  

Status MazePath(MazeType &maze,PostType start,PostType end)
{// 若迷宫maze存在通路,则求出一条同路放在栈中,并返回TRUE,否则返回FALSE  
    struct LStack S;
    PostType curpos;
    int curstep;// 当前序号,1,2,3,4分别表示东南西北方向  
    SElemType e;
    InitStack(S);
    curpos=start; //设置"当前位置"为"入口位置"  
    curstep=1;// 探索第一位
    printf("以三元组形式表示迷宫路径:\n");  
    do
    {   
        if(Pass(maze,curpos))   
        {// 当前位置可以通过     
            FootPrint(maze,curpos);// 留下足迹     
            e.ord=curstep;     
            e.seat=curpos;     
            e.di=1;
            printf("%d %d %d-->",e.seat.r,e.seat.c,e.di);     
            Push(S,e);// 加入路径
            if(curpos.r==end.r&&curpos.c==end.c)
                if(!DestroyStack(S))// 销毁失败      
                    exit(OVERFLOW);      
                else      
                    return TRUE; // 到达出口
                else     
                {      
                    curpos=NextPos(curpos,1);  // 下一位置是当前位置的东邻      
                    curstep++;// 探索下一步
                }//else
        }//if   
        else   
        {// 当前位置不通时      
            if(!StackEmpty(S))     
            {      
                Pop(S,e);
                while(e.di==4&& !StackEmpty(S))     
                {      
                    MarkPrint(maze,e.seat);      
                    Pop(S,e); // 留下不能通过的标记,并退一步
                }//while      
                if(e.di < 4)      
                {       e.di++;// 换一个方向探索      
                Push(S,e);
                curpos=NextPos(e.seat,e.di);// 设定当前位置是该方向上的相邻
                printf("%d %d %d-->",e.seat.r,e.seat.c,e.di);     
                }//if
            }//if
        }//else
    }
    while(!StackEmpty(S));
    if(!DestroyStack(S))// 销毁栈  
        exit(OVERFLOW);  
    else
        return FALSE;
}//MazePath
 
void PrintMaze(MazeType &maze)
{// 将标记路径信息的迷宫输出到终端  
    int i,j;
    printf("\n输出迷宫(*表示通路):\n\n");  
    printf("");
    for(i=0;i<=maze.r+1;i++)// 打印列数名
        printf("%4d",i);
    printf("\n\n");
    for(i=0;i<=maze.r+1;i++)  
    {   
        printf("%2d",i);// 打印行名   
        for(j=0;j<=maze.c+1;j++)
            printf("%4c",maze.adr[i][j]);// 输出迷宫当前位置的标记   
        printf("\n\n");
    }
}//PrintMaze

int main()
{// 主函数  
    MazeType maze;  
    PostType start,end;  
    char cmd;  
    do{   
        printf("-------创建迷宫--------\n");   
        if(!InitMaze(maze))   
        {      
            printf("Initialization errors!!!\n");   
            exit(OVERFLOW);// 初始化失败
        }
        do{// 输入迷宫入口坐标   
            printf("\n输入迷宫入口坐标: ");   
            scanf("%d%d",&start.r,&start.c);   
            if(start.r>maze.r ||start.c>maze.c)   
            {     
                printf("\nBeyond the maze!!!\n");    continue;
            }
        }
        while(start.r>maze.r ||start.c>maze.c);     
        do{// 输入迷宫出口坐标   
            printf("\n输入迷宫出口坐标: ");   
            scanf("%d%d",&end.r,&end.c);   
            if(end.r>maze.r ||end.c>maze.c)   
            {     
                printf("\nBeyond the maze!!!\n");     continue;
            }
        }
        while(end.r>maze.r ||end.c>maze.c);   
        if(!MazePath(maze,start,end))//迷宫求解  
            printf("\nNo path from entranceto exit!\n");
        else
            PrintMaze(maze);// 打印路径
        printf("\n需要继续创建新的迷宫吗 (y/n): ");
        scanf("%s",&cmd);
    }
    while(cmd=='y' || cmd=='Y');
}
2015-10-25 20:16
快速回复:求一个c语言编的迷宫代码
数据加载中...
 
   



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

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