【发布时间】:2018-03-15 14:22:39
【问题描述】:
我正在尝试在 2D 网格上创建 A* 算法的实现,并且已经卡在我需要创建一组节点的邻居的点上。以下是我正在使用的结构。
// Holds values for x and y locations on the grid
struct Coord {
int x, y;
};
// holds data for each node required for A*
struct Node {
int type; // used for defining if this node is a blocker, empty, start or end
Coord location;
int g = 0;
int h = 0;
int f = g + h;
Node *parent_; // pointer to this node's parent
std::string debugmessage;
};
我在这里创建这个函数时出现错误:
// finds a node's neighbours for A*
std::set<Node> neighbours(Node& n_) {
std::set<Node> neighbours_;
Node temp = n_;
int x = temp.location.x;
int y = temp.location.y;
// start at the location belonging to 'n_'
for (y; y < HEIGHT; y++) {
for (x; x < WIDTH; x++) {
// east
if (x < WIDTH - 1) {
neighbours_.insert(astarArray[x + 1][y]);
}
// west
if (x > 0) {
neighbours_.insert(astarArray[x - 1][y]);
}
// south
if (y < HEIGHT - 1) {
neighbours_.insert(astarArray[x][y + 1]);
}
// north
if (y > 0) {
neighbours_.insert(astarArray[x][y -1]);
}
}
}
return neighbours_;
}
感谢您的宝贵时间。
【问题讨论】:
-
因此您阅读了
std::set文档,并从它所拥有的类型中看到了它需要什么才能发挥作用。那你为什么不把它包括在这里呢?
标签: c++ visual-studio-2015 path-finding a-star stdset