【问题标题】:Shortest path in a grid between two points. With a catch网格中两点之间的最短路径。有一个陷阱
【发布时间】:2012-12-18 23:02:38
【问题描述】:

我有这个问题,我必须通过向右或向下移动来找到从 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;
}

程序现在打印正确的答案。现在我必须优化(运行时间太大了)。关于这个有什么提示吗?优化是我最讨厌的一件事。


答案

最终,解决方案似乎只包含少量代码。越少越好,因为我喜欢它。感谢 Dejan Jovanović 的出色解决方案

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

【问题讨论】:

  • 问题是,我该怎么做呢?我不在乎简单的答案,但提示会让我继续前进。
  • 只要像对待任何其他有向图一样对待它,并使用正常的寻路算法。你的困惑在哪里?

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


【解决方案1】:

需要构建图形,这可以通过使用一次扫描矩阵的动态规划轻松解决。

您可以在开始时将距离矩阵 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] 中。如果你想重建路径,你可以保留一个单独的矩阵来标记最短路径的来源。

【讨论】:

  • 天哪...我的意思是哦,我的你!如果可以做到这一点,那么我应该阅读一下动态编程 o_o 这就像一个魅力
  • @OlaviMustanoja 这是图遍历的特定实现,而不是分治法的动态编程。与其他解决方案不同,它不能适应为负数向上或向左移动。计算复杂度实际上大于广度优先搜索,另外还优化了不排队移动到已经可到达的节点。 (保证 BFS 最多访问每个节点一次;这将访问每个节点恰好一次。)
  • @Potatoswatter 动态编程是一个普遍的想法。实际上 Dijkstra 的算法和 BFS 可以看作是动态规划的一个实例。该算法本质上与 Dykstra 的算法/BFS 相同,因为它按顺序访问节点,使得当一个节点被选中时,它的最短路径是已知的。就复杂性而言,这种方法是在您需要构建完整图(访问所有节点和边)的同时完成的。
  • @DejanJovanović 根据维基百科的说法,动态编程可以概括为带记忆的分而治之,这更符合我所学的。 Dijkstra 和 BFS 是贪心算法。如果没有选择分区点的步骤,就很难将某物表征为 DP。但这确实使用了记忆部分。
【解决方案2】:

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

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

【讨论】:

  • 嗯,此时我知道客户的规格。他/他们在 2Ghz Linux 机器上运行。我忘了提到的另一个限制因素是内存限制,即 128 MB
  • 啊,对不起。当你写“作业”时,我从字面上理解它:P。您是否分析过蛮力(递归)方法?
  • 不,我还没有。不应该花太长时间 :D 但是像无数秒这样的递归方法不是比迭代方法慢吗?
  • 递归方法(您可能指的是深度优先),正如 NPE 有效评论的那样,要求您搜索整个问题空间,然后找到成本最低的问题空间。广度优先搜索是最优的,因为它通过增加深度进行搜索,并在最短的可能解决方案处停止。
  • @Amadan 两种方法都是递归的,只是它们的递归模式不同。我明白你对搜索空间的意思;只要调用者注意不要重新访问节点,那么是的,您在广度优先中遍历的次数更少。将广度优先与剪枝技术相结合会产生更好的解决方案,我认为我在优化阶段进行剪枝的建议暗示了广度优先。
【解决方案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

Uglifying into C++ 作为练习留给读者:p

【讨论】:

  • 这会让你探索每条有效路径,不是吗?
  • @NPE:我是个白痴,不应该在深夜回答问题。 BFS,而不是我所描述的 DFS,而不是我命名的。 BFS 自动寻找最短路径。无论如何,已编辑;感谢您指出脑残。
  • 你有一个观点,它看起来很有希望。非常感谢
【解决方案4】:

构建未加权有向图:

  1. NxM 顶点。下面,顶点v对应方格v
  2. 从顶点uv 有一条弧线,如果您可以一步从方格u 跳到方格v

现在应用从右上角顶点到左下角的最短路径算法。

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

【讨论】:

  • 我尝试构建图表,但失败了。我能想到的构建图表的唯一方法是广度优先搜索。现在这没有意义,对吧?不过,我想我会接受你的第二条建议:o
  • 这很有意义。 BFS 是最优解。直到现在我才看到你几乎完全做到了这一点。
  • 我的意思是首先使用 BFS 分析图形,然后使用 BFS 搜索它:D BFS BFS BFS
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 2016-03-04
  • 2011-01-27
  • 1970-01-01
  • 1970-01-01
  • 2023-01-15
  • 2019-04-20
  • 2021-12-18
相关资源
最近更新 更多