两点之间网格中的最短路径。抓住了

时间:2013-01-04 13:41:41

标签: c++ algorithm graph-algorithm shortest-path dijkstra

我有这个问题,我必须通过向右或向下移动找到NxM网格中从A点(总是左上角)到B点(总是在右下角)的最短路径。听起来很简单,嗯?好吧,这里有一个问题:我只能移动我正坐在的瓷砖上显示的数字。让我来说明一下:

2 5 1 2
9 2 5 3
3 3 1 1
4 8 2 7

在这个4x4网格中,最短路径需要3个步骤,从左上角2个节点向下步行到3个,从那里3个节点从右到1,然后从1个节点向下到目标。

[2] 5  1  2
 9  2  5  3
[3] 3  1 [1]
 4  8  2 [7]

如果不是最短的路径,我也可以采取这条路线:

[2] 5 [1][2]
 9  2  5  3
 3  3  1 [1]
 4  8  2 [7]

不幸的是,这将是一个高达4个步骤,因此,不符合我的利益。 这应该清楚一点。 现在关于输入。


用户输入网格如下:

5 4      // height and width
2 5 2 2  //
2 2 7 3  // the
3 1 2 2  // grid
4 8 2 7  //
1 1 1 1  //

作业

我已经考虑过这一点,但是无法找到一个更好的解决方案,而不是将输入的网格简化为未加权(或负权重)的图形,并在其上运行类似dijkstra或A *(或沿着这些线的东西)。嗯......这是我迷路的部分。我实现了一些事情(或者立刻投入到捶打中)。它与dijkstra或A *或任何东西无关;只是直接的广度优先搜索。


守则

#include <iostream>
#include <vector>

struct Point;

typedef std::vector<int> vector_1D;
typedef std::vector< std::vector<int> > vector_2D;
typedef std::vector<Point> vector_point;

struct Point {
    int y, x;
    vector_point Parents;
    Point(int yPos = 0, int xPos = 0) : y(yPos), x(xPos) { }

    void operator << (const Point& point) { this->Parents.push_back(point); }
};

struct grid_t {
    int height, width;
    vector_2D tiles;

    grid_t() // construct the grid
    { 
        std::cin >> height >> width; // input grid height & width

        tiles.resize(height, vector_1D(width, 0)); // initialize grid tiles

        for(int i = 0; i < height; i++)     //
            for(int j = 0; j < width; j++)  // input each tile one at a time
                std::cin >> tiles[i][j];    // by looping through the grid
    }
};

void go_find_it(grid_t &grid)
{
    vector_point openList, closedList;
    Point previous_node; // the point is initialized as (y = 0, x = 0) if not told otherwise
    openList.push_back(previous_node); // (0, 0) is the first point we want to consult, of course

    do
    {
        closedList.push_back(openList.back()); // the tile we are at is good and checked. mark it so.
        openList.pop_back(); // we don't need this guy no more

        int y = closedList.back().y; // now we'll actually
        int x = closedList.back().x; // move to the new point

        int jump = grid.tiles[y][x]; // 'jump' is the number shown on the tile we're standing on.

        if(y + jump < grid.height) // if we're not going out of bounds
        { 
            openList.push_back(Point(y+jump, x)); // 
            openList.back() << Point(y, x); // push in the point we're at right now, since it's the parent node
        }
        if(x + jump < grid.width) // if we're not going out of bounds
        { 
            openList.push_back(Point(y, x+jump)); // push in the new promising point
            openList.back() << Point(y, x); // push in the point we're at right now, since it's the parent node
        }
    }
    while(openList.size() > 0); // when there are no new tiles to check, break out and return
}

int main()
{
    grid_t grid; // initialize grid

    go_find_it(grid); // basically a brute-force get-it-all-algorithm

    return 0;
}

我应该也指出,运行时间不能超过1秒,最大网格高度和宽度为1000.所有的图块也是1到1000之间的数字。

感谢。


已编辑的代码

#include <iostream>
#include <vector>

struct Point;

typedef std::vector<int> vector_1D;
typedef std::vector< std::vector<int> > vector_2D;
typedef std::vector<Point> vector_point;

struct Point {
    int y, x, depth;
    vector_point Parents;
    Point(int yPos = 0, int xPos = 0, int dDepth = 0) : y(yPos), x(xPos), depth(dDepth) { }

    void operator << (const Point& point) { this->Parents.push_back(point); }
};

struct grid_t {
    int height, width;
    vector_2D tiles;

    grid_t() // construct the grid
    { 
        std::cin >> height >> width; // input grid height & width

        tiles.resize(height, vector_1D(width, 0)); // initialize grid tiles

        for(int i = 0; i < height; i++)     //
            for(int j = 0; j < width; j++)  // input each tile one at a time
                std::cin >> tiles[i][j];    // by looping through the grid
    }
};

int go_find_it(grid_t &grid)
{
    vector_point openList, closedList;
    Point previous_node(0, 0, 0); // the point is initialized as (y = 0, x = 0, depth = 0) if not told otherwise
    openList.push_back(previous_node); // (0, 0) is the first point we want to consult, of course

    int min_path = 1000000;

    do
    {
        closedList.push_back(openList[0]); // the tile we are at is good and checked. mark it so.
        openList.erase(openList.begin()); // we don't need this guy no more

        int y = closedList.back().y; // now we'll actually move to the new point
        int x = closedList.back().x; //
        int depth = closedList.back().depth; // the new depth

        if(y == grid.height-1 && x == grid.width-1) return depth; // the first path is the shortest one. return it

        int jump = grid.tiles[y][x]; // 'jump' is the number shown on the tile we're standing on.

        if(y + jump < grid.height) // if we're not going out of bounds
        { 
            openList.push_back(Point(y+jump, x, depth+1)); // 
            openList.back() << Point(y, x); // push in the point we're at right now, since it's the parent node
        }
        if(x + jump < grid.width) // if we're not going out of bounds
        { 
            openList.push_back(Point(y, x+jump, depth+1)); // push in the new promising point
            openList.back() << Point(y, x); // push in the point we're at right now, since it's the parent node
        }
    }
    while(openList.size() > 0); // when there are no new tiles to check, break out and return false

    return 0;
}

int main()
{
    grid_t grid; // initialize grid

    int min_path = go_find_it(grid); // basically a brute-force get-it-all-algorithm

    std::cout << min_path << std::endl;
    //system("pause");
    return 0;
}

程序现在打印正确的答案。现在我必须优化(运行时间太大)。有关这一个的任何提示吗?优化是我吮吸的一件事。


答案

最后解决方案似乎只包含很少的代码。我喜欢它越少越好。感谢DejanJovanović的美丽解决方案

#include <iostream>
#include <vector>
#include <algorithm>

struct grid_t {
    int height, width;
    std::vector< std::vector<int> > tiles;
    std::vector< std::vector<int> > distance;

    grid_t() // construct the grid
    { 
        std::cin >> height >> width; // input grid height & width

        tiles.resize(height, std::vector<int>(width, 0)); // initialize grid tiles
        distance.resize(height, std::vector<int>(width, 1000000)); // initialize grid tiles

        for(int i = 0; i < height; i++)     //
            for(int j = 0; j < width; j++)  // input each tile one at a time
                std::cin >> tiles[i][j];    // by looping through the grid
    }
};

int main()
{
    grid_t grid; // initialize grid

    grid.distance[0][0] = 0;
    for(int i = 0; i < grid.height; i++) {
        for(int j = 0; j < grid.width; j++) {
            if(grid.distance[i][j] < 1000000) {
                int d = grid.tiles[i][j];
                if (i + d < grid.height) {
                    grid.distance[i+d][j] = std::min(grid.distance[i][j] + 1, grid.distance[i+d][j]);
                }
                if (j + d < grid.width) {
                    grid.distance[i][j+d] = std::min(grid.distance[i][j] + 1, grid.distance[i][j+d]);
                }
            }
        }
    }
    if(grid.distance[grid.height-1][grid.width-1] == 1000000) grid.distance[grid.height-1][grid.width-1] = 0;
    std::cout << grid.distance[grid.height-1][grid.width-1] << std::endl;
    //system("pause");
    return 0;
}

4 个答案:

答案 0 :(得分:3)

你是在思考它。 :)运行广度优先搜索。解空间是二叉树,其中每个节点分支为“右”或“向下”。从当前点开始,生成下点和右点,将其坐标填入队列,重复直到完成。

不检查,像这样:

queue = [{ x: 0, y: 0, path: [] }] # seed queue with starting point
p = nil
do
  raise NoSolutionException if p.empty? # solution space exhausted
  p = queue.pop # get next state from the back of the queue
  break if p.x == MAX_X - 1 && p.y == MAX_Y - 1 # we found final state
  l = grid[p.x][p.y] # leap length

  # add right state to the front of the queue
  queue.unshift({x: p.x + l, y: p.y, path: p.path + [p] }) if p.x + l <= MAX_X

  # add down state to the front of the queue
  queue.unshift({x: p.x, y: p.y + l, path: p.path + [p] }) if p.y + l <= MAX_Y
end
puts p.path

丑化为C ++留给读者的练习:p

答案 1 :(得分:3)

需要构建图形,使用矩阵上的一次扫描动态编程可以很容易地解决这个问题。

您可以在开始时将距离矩阵D [i,j]设置为+ inf,D [0,0] = 0.在遍历矩阵时,您只需

if (D[i,j] < +inf) {
  int d = a[i, j];
  if (i + d < M) {
    D[i + d, j] = min(D[i,j] + 1, D[i + d, j]);
  }
  if (j + d < N) {
    D[i, j + d] = min(D[i,j] + 1, D[i, j + d]);
  }
}

最终的最小距离是D [M -1,N-1]。如果你想重建路径,你可以保留一个单独的矩阵,标记最短路径的来源。

答案 2 :(得分:2)

构建一个未加权的有向图:

  1. N x M个顶点。在下文中,顶点v对应于网格方v
  2. 如果您可以在一次移动中从网格方格u跳到方格v,则会有从顶点uv的圆弧。
  3. 现在应用从右上角到左下角的最短路径算法。

    最后,请注意您实际上并不需要构建图形。您可以根据原始网格简单地实现最短路径算法。

答案 3 :(得分:0)

从蛮力方法开始,让它发挥作用,然后从那里进行优化。蛮力是直截了当的:递归地运行它。采取你的两个动作,递归那些,依此类推。收集所有有效答案并保留最低要求。如果运行时间太长,那么您可以通过各种方式进行优化。例如,某些移动可能无效(因为它们超出了网格的维度)并且可以被消除,等等。保持优化,直到最坏情况输入以所需速度运行。

话虽如此,如果您使用相同的系统和输入,性能要求才有意义,即使这样,也有一些警告。 Big O表示法是一种更好的分析性能的方法,而且它可以指向算法并消除分析的需要。