迷宫问题C(栈)

迷宫问题C

思路

模仿人走迷宫做记号,所以我的思路是给出地图,地图用1表示墙,0表示路,2表示走过的路,3表示此路不通同1的作用(经过探测,发现死胡同,因为输出地图的原因,用3标记)

采用结构:栈(本文实现的比较简陋)

1.给出起点,入栈

2.获取栈顶元素,判断是否出口,是退出,不是的话往四个方向寻找通路

3.尝试
(1)若栈顶元素有一个方向能够走(如果maze[x][y+1]表示向右 上的值为0),就将该点maze[x][y+1]入栈,并修改maze[x][y]为2,
(2)如果发现该点所有方向的值都不为0,出栈并将maze[x][y]修改为3

4.跳到步骤2

代码

栈的实现以及地图的定义(栈实现比较简陋)

#define row 7
#define col 7
#define max 100

int maze[row][col]={1,1,1,1,1,1,1,
                 1,0,1,0,0,0,1,
                 1,0,1,0,1,0,1,
                 1,0,0,0,1,0,1,
                 1,0,1,0,1,0,1,
                 1,1,1,0,1,0,1,
                 1,1,1,1,1,1,1};
                 
typedef struct point
{
    int x;
    int y;
}pos;

typedef struct s
{
    pos data[max];
    int top;
}Sqtack,*sqtack;

sqtack init()
{
    sqtack S=(sqtack)malloc(sizeof(Sqtack));
    S->top=0;
    if(!S)
    {
        printf("error");
    }
    
    return S;
}

int Push(sqtack S,pos q)
 {
     S->data[S->top]=q;
     S->top++;
     return 0;
 }
 
int Pop(sqtack S,pos *q)
 {
     S->top--;
     *q=S->data[S->top];
     return 0;
 }
 
int Empty(sqtack S)
 {
     if(S->top==0)
     {
         return 1;
     }
     else
     {
         return 0;
     }
     
 }
 
 int GetTop(sqtack S,pos *q)
 {
     *q=S->data[S->top-1];
     return 0;
 }
 
void print(sqtack S)//打印栈里的元素
 {
     for(int x=0;x<S->top;x++)
     {
         printf("%d %d \n",S->data[x].x,S->data[x].y);
     }
     printf("\n");
 }

地图的打印

void printmaze()//显示地图
{
    for(int x=0;x<row;x++)
    {
        for(int y=0;y<col;y++)
        {
            if(maze[x][y]==1)
            {
                printf("5");
            }
            else if (maze[x][y]==0)
            {
               printf(" ");
            }
            else if(maze[x][y]==2)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }        
        }
        printf("\n");
    }
}

关键代码

void findpath()
 {
 	 printf("the orignal map is:\n\n");
     printmaze();
     pos *z;
     sqtack S;
     S=init();
     pos q={1,1};
     Push(S,q);
     maze[1][1]=2;
     while (!Empty(S))
     {
         
         GetTop(S,&q);
         if(q.x==row-2&&q.y==col-2)//是否出口
         {
             break;
         }
         //down
         if(q.x+1<row-1&&maze[q.x+1][q.y]==0)
         {
             q.x++;
             maze[q.x][q.y]=2;
             Push(S,q);
             continue;
            
         }
         //right
         if(q.y+1<col-1&&maze[q.x][q.y+1]==0)
         {
                 q.y++;
            
                 maze[q.x][q.y]=2;
                

                 Push(S,q);
              continue;
           
            
         }
         //up
         if(q.x-1>=1&&maze[q.x-1][q.y]==0)
            {
                 q.x--;
             
                 maze[q.x][q.y]=2;
               
                 Push(S,q);
             continue;
            
            }
            //left
            if(q.y-1>=1&&maze[q.x][q.y-1]==0)
            {
                q.y--;
               maze[q.x][q.y]=2;
                
                 Push(S,q);
             continue;
         
            }
            //没路的情况下
            Pop(S,z);
            maze[z->x][z->y]=3;
            
     }
     //跳出while后
    if(Empty(S))
    {
        printf("no exit");
    }
    else
    {
        printf("the path is:\n");
        print(S);
        printmaze();
    }
    
 }

主程序

int main()
{
    
    findpath();
    return 0;
}

结果:在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值