使用 2D 阵列进行路径规划

Path planning using 2D array

本文关键字:路径 规划 2D 阵列 使用      更新时间:2023-10-16

该问题需要用户输入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
    -1 0 0 0 -1
    -1 **1** -1 0 -1
    -1 0 0 -2 -1
    -1 -1 -1 -1 -1
    
  2. 将它与"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
    
  3. 重复这些步骤。

    -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;
}