c++ - 迭代深化搜索应该那么慢吗?

标签 c++ algorithm search c++11 recursion

我的数据结构:

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

helper :

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 应该很慢,但它应该那么慢吗?

最佳答案

我想我明白为什么这很慢了。

在你的助手中,你像这样访问邻居:

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

但您永远不会使用过去的结果。您将某些内容标记为已访问,但从不检查它是否已被访问。

关于c++ - 迭代深化搜索应该那么慢吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20033969/

相关文章:

c++ - 在关联容器中使用 emplace(args&& ...)

C++ 将二进制(64 位)转换为十进制

algorithm - 给定一个 preOrder 和 inOrder 序列,可能有多少级阶 BST 序列?

algorithm - 是否存在可以抵抗图像处理的数字图像隐写术算法?

algorithm - 不用语言提供的方法生成随机数

java - java中栈元素与对象的比较

iphone - UITableView 中的实时搜索

c++ - 为什么 std::move 采用 forward_reference 而不是 lvaue 引用

c++ - 根据 MSVC++ 中的 unicode 设置自动在 std::string 和 std::wstring 之间切换?

C# 搜索 "Did you mean"功能