首页 > 解决方案 > 使用二维阵列进行路径规划

问题描述

该问题需要用户输入二维网格的大小。然后输入每个条目。

像这样:

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. 用“2”替换它的邻居“0”并找到其他可能的可行空间。

    -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”等等。

但是,我不能在我的程序中包含任何标题。

感谢您阅读长长的问题:)!

标签: c++loopspath

解决方案


您必须将您的开放节点存储在某个队列中,或者每次都从整个地图中填充:

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

推荐阅读