无法追踪迷宫中的灰色细胞
not able to trace grey cell in maze
有一个矩阵,其中包含白色单元格(表示为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)
的路径,然后找到从P2
到P3(goal)
的路径。
为了重建路径,您可以创建另一个父矩阵pi[N][N]
,如果最短路径来自上方,则pi[i][j] = (i-1, j)
如果来自左侧pi[i][j] = (i, j-1)
如果不可能到达该单元格pi[i][j] = null(whatever you want)
。
一般来说,我的方法是:
- 生成一个图形,其中每个像元都是一个顶点,并与表示迷宫中相邻白色/灰色像元的边顶点连接。
- 找到起始顶点(表示 Array[0][0])和灰色顶点(建议使用 A*)之间的最短路径
P1
。 - 查找灰色顶点和结束顶点(表示数组 [N-1][N-1])之间的最短路径
P2
。 - P1 和 P2 可能只相交一次(因为它们代表最短路径),如果它们确实相交,从这一点开始,将代表相同的路径。 因此:
- 如果
P1
和P2
不相交,则P1
后跟P2
是最佳解决方案。 - 如果
P1
和P2
相交,则删除相交部分中的顶点,再次执行操作 2 和 3 以分别P3
和P4
找到新的最短路径。 最佳解是P1
后跟P4
和P3
之间的最小值,后跟P2
。
- 如果
相关文章:
- 在细胞中产生噪声 - CPP 和 GLSL
- OpenCV C++将灰色图像组合成彩色图像
- 计数用红色着色m灰色块的方法,其中红色块应以计数至少为n的组进行
- Qmage:如何将灰色图像转换为RGB热图
- 如何在QT中为QInputDialog输入密码时设置背景非活动和灰色?
- 如何阻止LWA_COLORKEY制作灰色轮廓?
- 为什么延迟太高,即使只是 RGB 到灰色转换 (Vivado HLS)?
- 如何在灰色区域增长明亮像素?OpenCV C++
- OpenCV4 Imshow函数打开一个空的灰色框
- 活着的邻居细胞未正确计数
- MPI 中的鬼细胞交换模式
- 选择一些用灰色代码编码的数字
- C 读取/写作PPM图像文件灰色图像
- 如何在C Win32 GUI中为文本框设置默认背景(灰色)文本
- OPENCV解码灰色代码模式相机校准错误.如何格式化固有和外在结果
- 包括带有性细胞群的标题
- 从C 中读取Excel的细胞
- 2D细胞自动机,用于基于瓷砖的水
- 如何找到两个细胞之间的路径中最狭窄的点
- 无法追踪迷宫中的灰色细胞