gpt4 book ai didi

c++ - 两点之间网格中的最短路径。有一个陷阱

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:15 25 4
gpt4 key购买 nike

我有这个问题,我必须通过仅向右或向下移动来找到从 A 点(总是左上角)到 B 点(总是右下角)的 NxM 网格中的最短路径。听起来很简单,是吗?那么问题来了:我只能移动我现在坐在的方 block 上显示的数字。让我举例说明:

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

最佳答案

需要构造图,这可以通过对矩阵进行一次扫描的动态规划轻松解决。

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

关于c++ - 两点之间网格中的最短路径。有一个陷阱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14158304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com