使用 2D 阵列进行路径规划
Path planning using 2D array
该问题需要用户输入2D网格的大小。然后输入每个条目。
喜欢这个:
5 5
-1 -1 -1 -1 -1
-1 0 0 0 -1
-1 1 -1 0 -1
-1 0 0 -2 -1
-1 -1 -1 -1 -1
则输出应为 3。这是从起点"1"到终点"-2"的最小路径。其中 "-1" 是障碍,0 是可行空间。
导师给出的方法是:
首先,找到起点 "1"。用"2"填充其可行空间4-邻居(左,右,上和下)
然后,重复这些步骤,用"3"填充相邻的可行空间,依此类推。
当可能的可行空间为 "-2" 时。停止并打印出最小步数。
我会试着把它写出来。
找到起点。
-1 -1 -1 -1 -1 -1 0 0 0 -1 -1 **1** -1 0 -1 -1 0 0 -2 -1 -1 -1 -1 -1 -1
将它与"0"相邻的"2"替换,并找到另一个可能的可行空间。
-1 -1 -1 -1 -1 -1 **2** **0** 0 -1 -1 1 -1 0 -1 -1 **2** **0** -2 -1 -1 -1 -1 -1 -1
重复这些步骤。
-1 -1 -1 -1 -1 -1 2 3 **0** -1 -1 1 -1 0 -1 -1 2 3 **-2** -1 -1 -1 -1 -1 -1
因为邻居是 "-2"。所以最短路径是 3。
// Find the Starting point " 1 ".
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
///////// DO NOT MODIFY ANYTHING ABOVE THIS LINE /////////
// IMPORTANT: Do NOT change any of the function headers already provided to you
// It means that you will need to use the function headers as is
// You may implement additional functions here
bool NextFill(int(&map)[MAX_SIZE][MAX_SIZE], int n)
{
const int offx = { -1, 0, 0, 1 };
const int offy = { 0, -1, 1, 0 }
bool found = false;
for (int x = 0; x != MAX_SIZE; ++x) {
for (int y = 0; y != MAX_SIZE; ++y) {
if (map[x][y] == n) {
for (int i = 0; i != 4) {
auto& neighbor = map[x + offx[i]][y + offy[i]];
if (neighbor == -1) { }
else if (neighbor == -2) { found = true; }
else if (neighbor == 0) { neighbor = n + 1; }
}
}
}
}
return found;
}
// Function: find the smallest number of steps to go from the starting point
// to the destination in a given map.
//
// Input: int map[][]: 2D-array map
// int map_h: the height of the map
// int map_w: the width of the map
// Output: return true if a path is found, and store the smallest number of
// steps taken in &num_steps (pass-by-reference)
// return false if there is no path
// ==============================================================
bool FindPath(int map[][MAX_SIZE], int map_h, int map_w, int& num_steps)
{
// ==========================
int time = 0;
if (NextFill(map, time))
return true;
else
return false;
}
///////// DO NOT MODIFY ANYTHING BELOW THIS LINE /////////
// Function: main function
// ==============================================================
int main()
{
int map_h;
int map_w;
cin >> map_h >> map_w;
int map[MAX_SIZE][MAX_SIZE];
// initialize map
for (int i = 0; i < MAX_SIZE; i++)
for (int j = 0; j < MAX_SIZE; j++)
map[i][j] = -1;
// read map from standard input
for (int i = 0; i < map_h; i++)
for (int j = 0; j < map_w; j++)
cin >> map[i][j];
int steps;
// print to screen number of steps if a path is found, otherwise print "No"
if (FindPath(map, map_h, map_w, steps))
cout << steps << endl;
else
cout << "No" << endl;
}
我的代码可以找到起点并找到其可能的可行空间,并将其替换为" 2"。但是我不知道找到我的"2"的可能可行空间并将其替换为"3"等等。
但是,我不能在我的程序中包含任何标头。
感谢您:)阅读一个很长的问题!
您必须将打开的节点存储在某个队列中,或者每次都从整个映射中填充:
bool NextFill(int (&map)[MAX_SIZE][MAX_SIZE], int n)
{
const int offx = {-1, 0, 0, 1};
const int offy = {0, -1, 1, 0}
bool found = false;
for (int x = 0; x != MAX_SIZE; ++x) {
for (int y = 0; y != MAX_SIZE; ++y) {
if (map[x][y] == n) {
for (int i = 0; i != 4) {
auto& neighbor = map[x + offx[i]][y + offy[i]];
if (neighbor == -1) { /*Nothing*/ } // wall
else if (neighbor == -2) { found = true; } // Found
else if (neighbor == 0) { neighbor = n + 1; } // unvisited
// else {/*Nothing*/} // Already visited.
}
}
}
}
return found;
}
相关文章:
- 如何将更多文件夹添加到c++include路径
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- C++A*算法并不总是在路径中具有目标节点
- 从函数角度看ID到文件路径的内部与外部映射
- boost xml parsingl将xml的路径作为变量发送
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- 如何使用cppcheck处理半相对包含路径
- 在C++中设置基于操作系统的文件路径
- 基于编译器选项的编译二进制路径
- 按边长度递归搜索图中所有可行路径
- 使用变量值作为 PlaySound 中的路径
- 如何转换真实路径 CString c++
- 从 GUID 获取 USB 卷路径
- 查找 GCD:并非所有控制路径都返回值
- C++17 文件系统::remove_all 带有通配符路径
- 在带有尾部斜杠的路径上返回 std::filesystem::create_directories() 的值
- 使用动态规划在矩阵中的所有可能路径中具有最小总和的打印路径
- 使用 2D 阵列进行路径规划
- 路径规划-多个目的地
- 使用动态规划计算网格中的路径数