在一个网格中两点之间的最短路径。但有一个陷阱。

4

我有一个问题,需要在一个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;
}

程序现在可以输出正确的答案。现在我需要优化代码(运行时间太长了)。有什么建议吗?优化是我最不擅长的事情。


答案

最终解决方案似乎只需要很少的代码。越少越好,我喜欢这样。感谢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;
}

问题是,我应该如何做到这一点?我不在乎简单的答案,但一个提示会让我有所启发。 - Olavi Mustanoja
只需将其视为任何其他有向图,并使用常规的路径查找算法。你有什么困惑吗? - BlueRaja - Danny Pflughoeft
4个回答

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

那会让你探索每一条有效的路径,是吗? - NPE
@NPE:我是个白痴,不应该在深夜回答问题。正如我所描述的那样,BFS而不是DFS。BFS自动找到最短路径。无论如何,已经编辑好了;感谢指出我的错误。 - Amadan
你说得很有道理,看起来很有前途。非常感谢。 - Olavi Mustanoja

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]。如果您希望重构路径,可以保留一个单独的矩阵,标记最短路径来自哪里。


哦天啊...我是说哦我的上帝!如果这可以完成,那么我应该读一下关于动态规划的内容 o_o 这个方法非常有效。 - Olavi Mustanoja
@OlaviMustanoja 这是图遍历的特殊实现,不是分治动态规划。与其他解决方案不同,它不能适应负数向上或向左移动的情况。计算复杂度实际上比广度优先搜索更大,但通过额外的优化,不将移动加入到已经可达的节点中。这保证了BFS最多只访问每个节点一次;而此实现恰好访问每个节点一次。 - Potatoswatter
@Potatoswatter 动态规划是一个通用的概念。实际上,Dijkstra算法和BFS可以看作是动态规划的一个实例。这个算法本质上与Dykstra算法/BFS相同,因为它按照节点的顺序访问节点,这样当选择一个节点时,它的最短路径已经知道了。就复杂度而言,这种方法所需的时间与构建完整图(访问所有节点和边)所需的时间相同。 - Dejan Jovanović
根据维基百科的说法,动态规划可以概括为带有记忆化的分治算法,这更符合我的学习经验。Dijkstra和BFS是贪心算法。如果没有选择分区点的步骤,很难将某些东西描述为DP。但是这确实使用了记忆化部分。 - Potatoswatter

2

构建一个无权有向图:

  1. N x M 个顶点,以下内容中,顶点 v 对应于网格方格 v
  2. 如果您可以在单次移动中从网格方格 u 跳到方格 v,则从顶点 uv 存在一条弧。

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

最后,注意您实际上不需要构建图。您可以直接使用原始网格来实现最短路径算法。


我尝试构建图,但失败了。我唯一想到的构建图的方法是广度优先搜索。现在这样做似乎没有意义,对吧?不过,我想我会理解你的第二条建议的 :o - Olavi Mustanoja
这是非常明智的决定。BFS 是最优解。现在我才意识到你已经几乎完全做到了这一点。 - Amadan
我是说先使用BFS对图进行分析,然后再使用BFS进行搜索 :D BFS BFS BFS - Olavi Mustanoja

0

首先采用暴力方法使其正常工作,然后再进行优化。暴力方法很简单:递归运行。取两个移动,对它们进行递归,以此类推。收集所有有效答案并保留最小值。如果运行时间太长,则可以通过各种方式进行优化。例如,一些移动可能无效(因为它们超出了网格的维度)并且可以被消除等等。不断优化,直到最坏情况下的输入以所需速度运行。

话虽如此,性能要求只有在使用相同的系统和输入时才有意义,即使如此也有一些注意事项。大O符号是分析性能的更好方法,而且它可以指向一个算法并消除了剖析的需要。


好的,现在我知道客户的规格了。他们正在一台2Ghz的Linux机器上运行。我忘了提到的另一个限制因素是内存限制,只有128 MB。 - Olavi Mustanoja
啊啊啊,那我很抱歉。当你写“作业”时,我就字面理解了:P。你有对暴力(递归)方法进行过分析吗? - RonaldBarzell
不,我还没有。不应该花太长时间:D 但递归方法不比迭代方法慢吗? - Olavi Mustanoja
递归方法(您可能指的是深度优先搜索),正如NPE所评论的那样,需要您搜索整个问题空间,然后找到最低成本的解决方案。广度优先搜索是最优的,因为它按递增深度进行搜索,并在最短可能的解决方案处停止。 - Amadan
@Amadan 两种方法都是递归的,它们只是在递归模式上有所不同。我明白你所说的搜索空间;只要调用者注意不要重复访问节点,那么在广度优先遍历中遍历的节点会更少。将广度优先与修剪技术相结合将导致更好的解决方案,我认为在优化阶段修剪时隐含了广度优先。 - RonaldBarzell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接