【发布时间】:2013-11-30 18:54:10
【问题描述】:
我的数据结构:
class Cell
{
public:
struct CellLink
{
Cell *cell;
int weight;
};
public:
int row;
int column;
vector<CellLink> neighbors;
State state;
int totalCost = 0;
};
主要功能:
void AI::IterativeDeepeningSearch(Cell* cell)
{
Cell* temp;
int bound = 0;
while (true)
{
naturalFailure = false;
temp = IDShelper(cell, bound);
if (IsExit(temp))
{
break;
}
bound++;
}
}
助手:
Cell* AI::IDShelper(Cell* cell, int bound)
{
Cell* temp = cell;
SetEnvironment(cell, State::visited);
PrintEnvironment();
if (bound > 0)
{
for (int i = 0; i < cell->neighbors.size(); i++)
{
temp = IDShelper(cell->neighbors[i].cell, bound - 1);
if (IsExit(temp))
{
naturalFailure = true;
return temp;
}
}
}
else if (IsExit(cell))
{
return cell;
}
return temp;
}
我对迷宫进行了迭代深化搜索。问题在于,在 21x21 的迷宫中完成搜索实际上需要数小时,而其他算法则需要几秒钟。
我知道 IDS 应该很慢,但它应该那么慢吗?
【问题讨论】:
-
其他算法之类的?最接近的比较是 BFS,但结果仍然很大程度上取决于目标节点的位置(但如果 BFS 需要几秒钟而 DFS ID 需要几小时,则可能有问题)。
-
@Dukeling 我已经实现了 BFS 和 UFC,它们只需要大约 3-4 秒
-
似乎不太可能慢几个数量级。我会检查您的递归,以确保您不会不小心一遍又一遍地重新访问相同的东西。例如,self->neighbor->neighbor 回到自己..
-
@JoeZ 这似乎正是发生了什么。
-
Joe 可能是对的,您不应该在递归之前或在递归函数开始时检查是否访问了单元格吗?
标签: c++ algorithm search c++11 recursion