无法追踪迷宫中的灰色细胞

not able to trace grey cell in maze

本文关键字:灰色 细胞 迷宫 追踪      更新时间:2023-10-16

有一个矩阵,其中包含白色单元格(表示为1),黑色单元格(表示为0)和只有一个灰色单元格(表示为2),需要在Array[N][N]中从(0,0)到(N-1,N-1)。

约束:

1)路径应仅覆盖白色单元格,并且必须通过灰色单元格(此灰色单元格可以在数组中的任何位置)

2)曾经访问过的节点不能再访问。

下面是典型的迷宫问题解决方案,但此解决方案无法处理遍历灰色单元格的特定情况......因此,您能否帮助我修改以下代码以处理特定情况。

我的问题是我不确定如何检查灰细胞?

#include "stdafx.h"
#include "algorithm"
#include <iostream>
 #include <fstream>
using namespace std;
#include<stdio.h>
// Maze size
#define N 4 
bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]);
/* A utility function to print solution matrix sol[N][N] */
void printSolution(int sol[N][N])
{
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
           printf(" %d ", sol[i][j]);
        printf("n");
    }
}
/* A utility function to check if x,y is valid index for N*N maze */
bool isSafe(int maze[N][N], int x, int y)
{
    //solveMazeUtil() to solve the problem. It returns false if no path is possible,
    //otherwise return true and prints the path in the form of 1s. Please note that
    //there may be more than one solutions, this function prints one of the feasible
    if(x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1)
        // if (x,y outside maze) return false
        return true;
    return false;
}
/* This function solves the Maze problem using Backtracking. It mainly uses
solutions.*/
bool solveMaze(int maze[N][N])
{
    int sol[N][N] = { {0, 0, 0, 0},
                      {0, 0, 0, 0},
                      {0, 0, 0, 0},
                      {0, 0, 0, 0}
                    };
    if(solveMazeUtil(maze, 0, 0, sol) == false)
    {
        printf("Solution doesn't exist");
        return false;
    }
    printSolution(sol);
    return true;
}
/* A recursive utility function to solve Maze problem */
bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N])
{
    // if (x,y is goal) return true
    if(x == N-1 && y == N-1)
    {
        sol[x][y] = 1;
        return true;
    }
    // Check if maze[x][y] is valid
    if(isSafe(maze, x, y) == true)
    {
        // mark x,y as part of solution path
        sol[x][y] = 1;
        /* Move forward in x direction */
        if (solveMazeUtil(maze, x+1, y, sol) == true)
            return true;
        /* If x moving in x direction doesn't give solution then
        Move down in y direction */
        if (solveMazeUtil(maze, x, y+1, sol) == true)
            return true;
        /* If none of the above movements work then BACKTRACK: 
        unmark x,y as part of solution path */
        sol[x][y] = 0;
        return false;
    } 
    return false;
}
// driver program to test above function
int main()
{
    int maze[N][N] = { {1, 0, 0, 0},
                       {1, 1, 0, 1},
                       {0, 1, 0, 0},
                       {1, 1, 1, 1}
                     };
    solveMaze(maze);
    getchar();
    return 0;
}

我正在考虑的一个解决方案是:

生成所有可能的路径(遍历 1 或 2)。

然后,找出哪个路径中有 2 个。 ,然后将该路径打印为输出。

但我不认为这将是好方法...所以,请让我知道如何以体面的方式实现我的目标。谢谢

由于在代码中只使用两种可能的移动:向下和向右,因此这是一个 DAG。DAG 适用于动态规划方法:每个单元有两种到达那里的可能性,一种来自上方,另一种来自左侧。因此,像元的最小距离为:

cost[i][j] = min(cost[i][j-1],cost[i-1][j]) + 1

这是考虑到进行运动的成本是 1。如果单元格是黑色的,您可以给它无限的成本,您只需要找到从P1(start)P2(gray cell)的路径,然后找到从P2P3(goal)的路径。

为了重建路径,您可以创建另一个父矩阵pi[N][N],如果最短路径来自上方,则pi[i][j] = (i-1, j)如果来自左侧pi[i][j] = (i, j-1)如果不可能到达该单元格pi[i][j] = null(whatever you want)

一般来说,我的方法是:

  1. 生成一个图形,其中每个像元都是一个顶点,并与表示迷宫中相邻白色/灰色像元的边顶点连接。
  2. 找到起始顶点(表示 Array[0][0])和灰色顶点(建议使用 A*)之间的最短路径P1
  3. 查找灰色顶点和结束顶点(表示数组 [N-1][N-1])之间的最短路径P2
  4. P1 和 P2 可能只相交一次(因为它们代表最短路径),如果它们确实相交,从这一点开始,将代表相同的路径。 因此:
    • 如果P1P2不相交,则P1后跟P2是最佳解决方案。
    • 如果P1P2相交,则删除相交部分中的顶点,再次执行操作 2 和 3 以分别P3P4找到新的最短路径。 最佳解是 P1 后跟 P4P3 之间的最小值,后跟 P2