第十二周项目5-迷宫问题之深度优先遍历算法

/*  
 * Copyright (c) 2016, 烟台大学计算机与控制工程学院  
 * All rights reserved。  
 * 文件名称 :1.cpp  
 * 作    者 :杨俊杰 
 * 完成日期 :2016年 11月18日  
 * 版 本 号 :v1.0  
 * 问题描述 : 
 * 输出描述 :
 */

设计一个程序,采用深度优先遍历算法的思路,解决迷宫问题。 
  (1)建立迷宫对应的图数据结构,并建立其邻接表表示。 
  (2)采用深度优先遍历的思路设计算法,输出从入口(1,1)点到出口(M,N)的所有迷宫路径。

[模型建立] 
  将迷宫中的每一格作为一个顶点,相邻格子可以到达,则对应的顶点之间存在边相连。 
  例如,下面的迷宫 
这里写图片描述 
在使用数组表示时,用0表示格子是空地,用1表示格子处是墙,对应的矩阵是:

    int mg[M+2][N+2]=   //迷宫数组
    {
        {1,1,1,1,1,1},
        {1,0,0,0,1,1},
        {1,0,1,0,0,1},
        {1,0,0,0,1,1},
        {1,1,0,0,0,1},
        {1,1,1,1,1,1}
    };
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

建立的图结构为: 
这里写图片描述 
于是,从(1,1)到(4,4)的迷宫问题,转化为寻找顶点(1,1)到顶点(4,4)的路径的问题。

[cpp]  view plain  copy
  1. #include <stdio.h>    
  2. #include <malloc.h>    
  3. #define MaxSize 100    
  4. #define M 4    
  5. #define N 4    
  6. //以下定义邻接表类型    
  7. typedef struct ANode            //边的结点结构类型    
  8. {    
  9.     int i,j;                    //该边的终点位置(i,j)    
  10.     struct ANode *nextarc;      //指向下一条边的指针    
  11. } ArcNode;    
  12.     
  13. typedef struct Vnode            //邻接表头结点的类型    
  14. {    
  15.     ArcNode *firstarc;          //指向第一条边    
  16. } VNode;    
  17.     
  18. typedef struct    
  19. {    
  20.     VNode adjlist[M+2][N+2];    //邻接表头节点数组    
  21. } ALGraph;                      //图的邻接表类型    
  22.     
  23. typedef struct    
  24. {    
  25.     int i;                      //当前方块的行号    
  26.     int j;                      //当前方块的列号    
  27. } Box;    
  28.     
  29. typedef struct    
  30. {    
  31.     Box data[MaxSize];    
  32.     int length;                 //路径长度    
  33. } PathType;                     //定义路径类型    
  34.     
  35. int visited[M+2][N+2]= {0};    
  36. int count=0;    
  37. void CreateList(ALGraph *&G,int mg[][N+2])    
  38. //建立迷宫数组对应的邻接表G    
  39. {    
  40.     int i,j,i1,j1,di;    
  41.     ArcNode *p;    
  42.     G=(ALGraph *)malloc(sizeof(ALGraph));    
  43.     for (i=0; i<M+2; i++)                   //给邻接表中所有头节点的指针域置初值    
  44.         for (j=0; j<N+2; j++)    
  45.             G->adjlist[i][j].firstarc=NULL;    
  46.     for (i=1; i<=M; i++)                    //检查mg中每个元素    
  47.         for (j=1; j<=N; j++)    
  48.             if (mg[i][j]==0)    
  49.             {    
  50.                 di=0;    
  51.                 while (di<4)    
  52.                 {    
  53.                     switch(di)    
  54.                     {    
  55.                     case 0:    
  56.                         i1=i-1;    
  57.                         j1=j;    
  58.                         break;    
  59.                     case 1:    
  60.                         i1=i;    
  61.                         j1=j+1;    
  62.                         break;    
  63.                     case 2:    
  64.                         i1=i+1;    
  65.                         j1=j;    
  66.                         break;    
  67.                     case 3:    
  68.                         i1=i, j1=j-1;    
  69.                         break;    
  70.                     }    
  71.                     if (mg[i1][j1]==0)                          //(i1,j1)为可走方块    
  72.                     {    
  73.                         p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p    
  74.                         p->i=i1;    
  75.                         p->j=j1;    
  76.                         p->nextarc=G->adjlist[i][j].firstarc;   //将*p节点链到链表后    
  77.                         G->adjlist[i][j].firstarc=p;    
  78.                     }    
  79.                     di++;    
  80.                 }    
  81.             }    
  82. }    
  83. //输出邻接表G    
  84. void DispAdj(ALGraph *G)    
  85. {    
  86.     int i,j;    
  87.     ArcNode *p;    
  88.     for (i=0; i<M+2; i++)    
  89.         for (j=0; j<N+2; j++)    
  90.         {    
  91.             printf("  [%d,%d]: ",i,j);    
  92.             p=G->adjlist[i][j].firstarc;    
  93.             while (p!=NULL)    
  94.             {    
  95.                 printf("(%d,%d)  ",p->i,p->j);    
  96.                 p=p->nextarc;    
  97.             }    
  98.             printf("\n");    
  99.         }    
  100. }    
  101. void FindPath(ALGraph *G,int xi,int yi,int xe,int ye,PathType path)    
  102. {    
  103.     ArcNode *p;    
  104.     visited[xi][yi]=1;                   //置已访问标记    
  105.     path.data[path.length].i=xi;    
  106.     path.data[path.length].j=yi;    
  107.     path.length++;    
  108.     if (xi==xe && yi==ye)    
  109.     {    
  110.         printf("  迷宫路径%d: ",++count);   
  111.         int k;  
  112.         for ( k=0; k<path.length; k++)    
  113.             printf("(%d,%d) ",path.data[k].i,path.data[k].j);    
  114.         printf("\n");    
  115.     }    
  116.     p=G->adjlist[xi][yi].firstarc;  //p指向顶点v的第一条边顶点    
  117.     while (p!=NULL)    
  118.     {    
  119.         if (visited[p->i][p->j]==0) //若(p->i,p->j)方块未访问,递归访问它    
  120.             FindPath(G,p->i,p->j,xe,ye,path);    
  121.         p=p->nextarc;               //p指向顶点v的下一条边顶点    
  122.     }    
  123.     visited[xi][yi]=0;    
  124. }    
  125.     
  126. int main()    
  127. {    
  128.     ALGraph *G;    
  129.     int mg[M+2][N+2]=                           //迷宫数组    
  130.     {    
  131.         {1,1,1,1,1,1},    
  132.         {1,0,0,0,1,1},    
  133.         {1,0,1,0,0,1},    
  134.         {1,0,0,0,1,1},    
  135.         {1,1,0,0,0,1},    
  136.         {1,1,1,1,1,1}    
  137.     };    
  138.     CreateList(G,mg);    
  139.     printf("迷宫对应的邻接表:\n");    
  140.     DispAdj(G); //  
  141.     PathType path;    
  142.     path.length=0;    
  143.     printf("所有的迷宫路径:\n");    
  144.     FindPath(G,1,1,M,N,path);    
  145.     return 0;    
  146. }    

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值