• 大小: 370KB
    文件类型: .rar
    金币: 2
    下载: 0 次
    发布日期: 2024-01-25
  • 语言: C/C++
  • 标签: 迷宫  代码  

资源简介

通过键盘读入矩阵迷宫地图 运算出任一条可走出路径 若无路径怎返回error

资源截图

代码片段和文件信息

#include 
#include 

#define STACK_INIT_SIZE 10000
#define STACKINCREMENT 100
#define M 10
#define N 10

int flag=0;

typedef struct/* 坐标位置 */
{
    int x;
    int y;
}PosType;

typedef struct/* 栈中元素结构体 */
{
    int num;/* 序号 */
    PosType seat;/* 坐标位置 */
    int di;/* 通道方向(1东2南3西4北) */
}SElemType;

typedef struct/* 栈 */
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;
/* 栈操作 */

void initialStack(SqStack *s)/* 栈的建立 */
{
    s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!s->base) exit(0);
    s->top=s->base;
    s->stacksize=STACK_INIT_SIZE;
}

void Push(SqStack *sSElemType e)/* 插入栈顶为e的元素 */
{
    if(s->top-s->base>=s->stacksize)
    {
        s->base=(SElemType *)realloc(s->base(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(SElemType));
        if(!s->base) exit(0);
        s->top=s->base+s->stacksize;
        s->stacksize+=STACKINCREMENT;
    }
    (*(s->top)).num=e.num;
    (*(s->top)).seat.x=e.seat.x;
    (*(s->top)).seat.y=e.seat.y;
    (*(s->top)).di=e.di;
    s->top++;
}

void Pop(SqStack *sSElemType *e)/* 删除栈顶元素 用e返回 */
{
    if(s->top==s->base) exit(0);
    (s->top)--;
    e->num=(*(s->top)).num;
    e->seat.x=(*(s->top)).seat.x;
    e->seat.y=(*(s->top)).seat.y;
    e->di=(*(s->top)).di;
}

int StackEmpty(SqStack *s)/* 检查栈是否为空  */
{
    if(s->top==s->base)
    return(1);
    return(0);
}

void ClearStack(SqStack *s)/* 将s栈清空 */
{
    s->top=s->base;
    s->stacksize=0;
}
/* ////////////////////////////////////////初始化迷宫 */

void ReadArray(int maze[M][N])
{
    int ij;
    for(i=0;i    {
        for(j=0;j        scanf(“%d“&maze[i][j]);
    }
}

void initialarray(int (*p)[N])
{
    int ij;
    int kl;
    int maze[M][N];
    for(k=0;k    {
        for(l=0;l        scanf(“%d“&maze[k][l]);/*测试地图:{1111111111
                                            1001000101
                                            1000010101
                                            1001100001
                                            1111100101
                                            1000100001
                                            1010011001
                                            1011101101
                                            1100000001
                                            1111111111}*/
    }
    printf(“********Print the maze:*********\n“);
    for(i=0;i    {
        for(j=0;j        {
            printf(“%d“maze[i][j]);
            *(*(p+i)+j)=maze[i][j];/*利用二维数组*/
        }
        printf(“\n“);
    }
}
/* ///////// */

void NextPos(PosType *pint d)/* 确定下一个位置的坐标 */
{
    switch(d)
    {
    case 1:p->y++;break;
    case 2:p->x++;break;
    case 3:p->y--;break;
    case 4:p->x--;break;
    }

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6673  2011-04-15 15:29  迷宫\AutoMaze.c

     文件      17789  2011-04-14 22:07  迷宫\AutoMaze.docx

     文件      16384  2011-04-15 15:26  迷宫\Debug\AutoMaze.obj

     文件      33792  2011-04-15 15:26  迷宫\Debug\vc60.idb

     文件      53248  2011-04-15 15:26  迷宫\Debug\vc60.pdb

     文件     192560  2011-04-15 15:26  迷宫\Debug\迷宫.exe

     文件     221560  2011-04-15 15:26  迷宫\Debug\迷宫.ilk

     文件     184772  2011-04-15 15:17  迷宫\Debug\迷宫.pch

     文件     467968  2011-04-15 15:26  迷宫\Debug\迷宫.pdb

     文件      11939  2011-04-14 19:40  迷宫\测试用迷宫地图.docx

     文件       4314  2011-04-15 15:32  迷宫\迷宫.dsp

     文件        516  2011-04-15 15:32  迷宫\迷宫.dsw

     文件      41984  2011-04-15 15:32  迷宫\迷宫.ncb

     文件      53760  2011-04-15 15:32  迷宫\迷宫.opt

     文件        921  2011-04-15 15:31  迷宫\迷宫.plg

     文件      16524  2011-04-15 15:31  迷宫\迷宫___Win32_Debug\AutoMaze.obj

     文件      33792  2011-04-15 15:31  迷宫\迷宫___Win32_Debug\vc60.idb

     文件      45056  2011-04-15 15:31  迷宫\迷宫___Win32_Debug\vc60.pdb

     文件     192573  2011-04-15 15:31  迷宫\迷宫___Win32_Debug\迷宫.exe

     文件     199476  2011-04-15 15:31  迷宫\迷宫___Win32_Debug\迷宫.ilk

     文件     184772  2011-04-15 15:31  迷宫\迷宫___Win32_Debug\迷宫.pch

     文件     377856  2011-04-15 15:31  迷宫\迷宫___Win32_Debug\迷宫.pdb

     目录          0  2011-04-15 15:17  迷宫\Debug

     目录          0  2011-04-15 15:31  迷宫\迷宫___Win32_Debug

     目录          0  2011-04-15 15:32  迷宫

----------- ---------  ---------- -----  ----

              2358229                    25


评论

共有 条评论