通过迷宫找到所有可能的路径

时间:2016-05-11 01:55:57

标签: maze

我正在尝试创建一个程序,它将遍历一个随机生成的迷宫,其中1是开放的,0是墙。从左上角开始到右下角结束。路径可以向上,向下,向左和向右。

目前,我的程序为我提供了一个解决方案,但我无法让它打印多条路径。

我已经阅读了这个问题的几个不同版本,但我无法找到一个完全符合我的参数。

这是我的代码,我省略了随机生成迷宫的部分。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

int  n, minMatrix, solIndex = 1, minLen = 10000000; //I use the latter 3 variables in order to find the shortest path, not relevant for now


bool solveMaze(int mat[n][n],int x, int y, int sol[][n], int count){

int i, j;
  if((!(x >= 0 && x <n  && y >=0 && y < n)) || mat[x][y] == 0 || sol[x][y] == 1){
    return false;
  }

  if(x == n-1 && y == n-1){
    sol[x][y] = 1;

    printf("Solution %d is:\n", solIndex);
    for(i = 0; i < n; i++)
    {
      for( j=0;j<n;j++)
      {
        printf("%d", sol[i][j]);
      }
      printf("\n");
    }

    if(count<minLen)
    {
      minLen = count;
      minMatrix = solIndex;
    }
    solIndex +=1;
    sol[x][y] = 0;
    return true;
  }

  sol[x][y] = 1;

  if(solveMaze(mat, x+1, y, sol, count+1)){
    return true;
  }

  if(solveMaze(mat, x-1, y, sol, count+1)){
    return true;
  }

  if(solveMaze(mat, x, y+1, sol, count+1)){
    return true;
  }

  if(solveMaze(mat, x, y-1, sol, count+1)){
    return true;
  }
  sol[x][y] = 0;
  return false;

}

我省略了我随机生成迷宫的主要部分。

int main(){

if(!solveMaze(**mat, 0, 0, sol, 0)){
    printf("No possible paths, run program again\n");
  }
  else{
    printf("the shortest path is %d\n", minMatrix);
  }
}

例如,如果我有迷宫

1100111111
1101111111
1111110110
1110011111
1101101011
1111101011
1110111101
1100111111
1110111011
1101101111

它给了我找到的第一条路径

1000000000
1001100000
1111110000
1100011000
1100001000
1100001000
1100001000
1100001011
1100001011
1100001111

虽然它需要一种迂回的方式到达那里,由于按顺序向下,向上,向右和向左的顺序,它仍然是一条路。

所以最终,我不确定如何迭代多条路径。

2 个答案:

答案 0 :(得分:3)

使用来自这个类似问题的示例迷宫(标记为重复但可以独立编译)的直接完全工作解决方案:Find all paths in a maze using DFS

它使用简单的DFS和简单的递归,这看起来与此处的问题相同。它跟踪单个字符串实例中的当前轨道,并修改迷宫以阻挡当前轨道。

#include <iostream>
#include <string>

const int WIDTH = 6;
const int HEIGHT = 5;

void check(int x, int y, int dest_x, int dest_y, 
           int (&maze)[HEIGHT][WIDTH], std::string& path) {
  if (x < 0 || y < 0 || x >= WIDTH|| y >= HEIGHT || !maze[y][x]) {
    return;        
  }
  int len = path.size();
  path += (char) ('0' + x);
  path += ',';
  path += (char) ('0' + y);

  if (x == dest_x && y == dest_y) {
    std::cout << path << "\n";
  } else {
    path += " > ";
    maze[y][x] = 0;  
    check (x + 0, y - 1, dest_x, dest_y, maze, path);
    check (x + 0, y + 1, dest_x, dest_y, maze, path);
    check (x - 1, y + 0, dest_x, dest_y, maze, path);
    check (x + 1, y + 0, dest_x, dest_y, maze, path);
    maze[y][x] = 1;
  }

  path.resize(len);
}


int main() {
  int maze[HEIGHT][WIDTH] = {
      {1,0,1,1,1,1},
      {1,0,1,0,1,1},
      {1,1,1,0,1,1},
      {0,0,0,0,1,0},
      {1,1,1,0,1,1}};

  std::string path;
  check(0, 0, 4, 3, maze, path);
  return 0;
}

Runnable版本:https://code.sololearn.com/cYn18c5p7609

答案 1 :(得分:1)

我终于找到了你问题的解决方案。但说实话,这不是我开发的解决方案,其他人(即Schröder)之前有过这个想法!

问题由Schröder描述,但请查看排列二叉树的german translation

将您的路径和所有可到达的节点转换为二叉树并置换它! (但要注意,可能有很多解决方案)

enter image description here

正如您所看到的,这些都是穿越4x4平方的解决方案(缺少镜像部分,但这就是唉)。