鱼C论坛

 找回密码
 立即注册
查看: 4760|回复: 6

迷宫问题全部路径和最短路径求解

[复制链接]
发表于 2019-5-26 17:51:26 | 显示全部楼层 |阅读模式
60鱼币
下面的代码能找出一条路径,不知道怎么修改才能求出全部路径。

以下是输入的迷宫:
1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 0 1 1 1 1
1 1 0 1 0 1 1 1 1 1
1 0 1 0 0 0 0 0 1 1
1 0 1 1 1 0 1 1 1 1
1 1 0 0 1 1 0 0 0 1
1 0 1 1 0 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1

  1. #include <iostream>
  2. #include<stack>
  3. using namespace std;
  4. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  5. const int XLEN=8,YLEN=10;//默认迷宫行数和列数
  6. int m,p;// 出口坐标
  7. int maze[XLEN][YLEN];//迷宫数组
  8. int mark[XLEN][YLEN];//访问标记数组
  9. struct Offsets{
  10.         int a,b;//a和b是x y方向的偏移
  11. };
  12. Offsets directions[8]={
  13.         {-1,0},//上
  14.         {-1,1},//右上
  15.         {0,1},//右
  16.         {1,1},//右下
  17.         {1,0},//下
  18.         {1,-1},//左下
  19.         {0,-1},//左
  20.         {-1,-1},//左上
  21. };
  22. struct items{
  23.         int x,y,dir;//栈结点定义
  24. };

  25. void outputpath(stack<items> a, items Tmp) {
  26.     stack<items> b;
  27.     b.push(Tmp);
  28.     while(!a.empty()){
  29.                                         b.push(a.top());
  30.                                         a.pop();
  31.                                 }
  32.                                 while(!b.empty()){
  33.                                         cout<<"<"<<b.top().x<<","<<b.top().y<<">"<<" ";
  34.                                         b.pop();
  35.                                     
  36.                                 }
  37. }
  38. void Path(int x,int y,int m,int n){//x,y为入口坐标,m,n为出口坐标
  39.         int i,j,g,h,d = 0; // i,j为当前位置, g,h为下一步的位置,d为下一步的方向
  40.         stack<items> S;//设置工作栈
  41.         stack<items> St;//输出栈
  42.         items tmp;
  43.         items Tmp;
  44.         mark[x][y]=1;//x,y是入口
  45.         tmp.x=x,tmp.y=y;
  46.         tmp.dir=0;
  47.         S.push(tmp);//入口坐标入栈
  48.         while(!S.empty()){//当栈不空,一直走下去
  49.                 tmp=S.top();//取栈顶元素
  50.                 S.pop();
  51.                 i=tmp.x;
  52.                 j=tmp.y;//取当前位置的坐标
  53.                 d=tmp.dir;
  54.                 while(d<8){
  55.                         //找下一位置(g,h)
  56.                         g=i+directions[d].a;
  57.                         h=j+directions[d].b;
  58.                         if(g==m && h==n){//到达出口
  59.                                 //逆向存入输出栈
  60.                                 Tmp.x=m,Tmp.y=n;
  61.                                 outputpath(S, Tmp);
  62.                                 mark[g][h]=1;//标记已被访问
  63.                                 return;
  64.                         }
  65.                
  66.                         //未到达出口
  67.                         if(maze[g][h]==0 && mark[g][h]==0){//如果可通且未被访问
  68.                                 mark[g][h]=1;//标记已被访问
  69.                                 tmp.x=i;
  70.                                 tmp.y=j;
  71.                                 tmp.dir=d;//记录已走过的位置和前进的方向
  72.                                 S.push(tmp);
  73.                                 i=g;
  74.                                 j=h;
  75.                                 d=0;}//移到(g,h)在各个方向试探
  76.                         else//试探下一个方向
  77.                                 d++;
  78.                 }
  79.         }
  80. }
  81. int main(int argc, char** argv) {
  82.         int i,j,x,y,m,n;
  83.         cout<<"迷宫如下图所示:"<<endl;
  84.         for(i=0;i<XLEN;i++){
  85.                  for(j=0;j<YLEN;j++){
  86.                          cin>>maze[i][j];
  87.                          mark[i][j]=0;
  88.                  }
  89.    }
  90.                
  91.         cout<<"迷宫所有路径如下:"<<endl;
  92.         Path(1,1,XLEN-2,YLEN-2);
  93. //        cout<<"最短路径如下:"<<endl;
  94. //        cout<<"长度为:"<<
  95. //        cout<<"通路为:"<<
  96.         return 0;
  97. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-26 17:58:47 | 显示全部楼层
主要是最近在用Python, 大一的数据结构都忘得差不多了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-28 00:07:28 | 显示全部楼层
dfs可以很简单实现
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-28 00:24:38 | 显示全部楼层
  1. #include <iostream>
  2. using namespace std;
  3. //0 为可以移动 1为障碍
  4. int map[][10] =
  5. {
  6. 1,1,1,1,1,1,1,1,1,1,
  7. 1,0,1,1,1,0,1,1,1,1,
  8. 1,0,0,0,0,1,1,1,1,1,
  9. 1,0,1,0,0,0,0,0,1,1,
  10. 1,1,1,1,1,0,1,1,1,1,
  11. 1,0,0,0,0,0,0,0,0,1,
  12. 1,0,1,1,0,0,1,1,0,1,
  13. 1,0,0,0,0,0,0,0,0,1 };
  14. struct point
  15. {
  16.         int x, y;
  17. };
  18. point steps[100] = { 0 };
  19. int startx = 1, starty = 1;
  20. int endx = 5, endy = 3;
  21. int direct[][2] = { {-1,0},{1,0},{0,-1},{0,1} };
  22. int row = 8, col = 10;//8行10列地图

  23. void dfs(int deep, int x, int y)
  24. {
  25.         int i;
  26.         if (x<0 || x>row - 1 || y<0 || y>col - 1 || map[x][y] == 1)
  27.                 return;
  28.         point tmp = { x,y };
  29.         steps[deep] = tmp;
  30.         if (x == endx && y == endy) {//终点
  31.                 for (i = 0; i < deep+1; i++) {
  32.                         printf("(%d,%d)-", steps[i].x, steps[i].y);
  33.                 }
  34.                 printf("\n");
  35.                 return;
  36.         }
  37.         for (i = 0; i < 4; i++)
  38.         {
  39.                 map[x][y] = 1;
  40.                 dfs(deep + 1, x + direct[i][0],y+direct[i][1]);
  41.                 map[x][y] = 0;
  42.         }
  43. }

  44. int main()
  45. {
  46.         point t = { startx,starty };
  47.         dfs(0, startx, starty);
  48.         return 0;
  49. }
复制代码



(1,1)-(2,1)-(2,2)-(2,3)-(3,3)-(3,4)-(3,5)-(4,5)-(5,5)-(6,5)-(7,5)-(7,4)-(6,4)-(5,4)-(5,3)-
(1,1)-(2,1)-(2,2)-(2,3)-(3,3)-(3,4)-(3,5)-(4,5)-(5,5)-(6,5)-(7,5)-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,6)-(7,5)-(6,5)-(6,4)-(7,4)-(7,3)-(7,2)-(7,1)-(6,1)-(5,1)-(5,2)-(5,3)-
(1,1)-(2,1)-(2,2)-(2,3)-(2,4)-(3,4)-(3,5)-(4,5)-(5,5)-(5,6)-(5,7)-(5,8)-(6,8)-(7,8)-(7,7)-(7,6)-(7,5)-(7,4)-(6,4)-(5,4)-(5,3)-
(1,1)-(2,1)-(2,2)-(2,3)-(2,4)-(3,4)-(3,5)-(4,5)-(5,5)-(5,6)-(5,7)-(5,8)-(6,8)-(7,8)-(7,7)-(7,6)-(7,5)-(7,4)-(7,3)-(7,2)-(7,1)-(6,1)-(5,1)-(5,2)-(5,3)-
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-28 09:41:38 | 显示全部楼层
迷雾少年 发表于 2019-5-28 00:24
(1,1)-(2,1)-(2,2)-(2,3)-(3,3)-(3,4)-(3,5)-(4,5)-(5,5)-(6,5)-(7,5)-(7,4)-(6,4)-(5,4)-(5,3)-
(1 ...

···你这个是四个方向的,我的是八个方向的,而且我想要的是在我基础上上面修改一下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-28 16:10:58 | 显示全部楼层
程序员的救赎 发表于 2019-5-28 09:41
···你这个是四个方向的,我的是八个方向的,而且我想要的是在我基础上上面修改一下

八个方向x-1,y-1,x+1,y+1,x-1,y+1,x-1,y+1就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-28 16:12:06 | 显示全部楼层
程序员的救赎 发表于 2019-5-28 09:41
···你这个是四个方向的,我的是八个方向的,而且我想要的是在我基础上上面修改一下

dfs递归用的就是栈啊,原理一样
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-2 05:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表