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

时间:2019-03-31 15:03:52

标签: c++ loops path

该问题需要用户输入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”矿替换,以此类推。

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

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

1 个答案:

答案 0 :(得分:1)

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

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