美文网首页
穷举搜索

穷举搜索

作者: ustclcl | 来源:发表于2019-08-22 22:13 被阅读0次

穷举搜索

实质是创建一个状态树,边建立边剪枝,得到最终状态输出
步骤有:

  1. 列出表示状态的数据结构
  2. 列出在状态之间迁移的动作的数据结构
  3. 列出两个状态转换的所有动作列表
  4. 创建一个deque存储搜索的状态
  5. 从deque尾端取出状态,判断是否是最终状态,是的话打印当前deque,进行搜索search,循环所有动作,执行动作searchOnOneAction
  6. 判断新状态是否有效,有效则加入deque,继续递归调用搜索

有很多问题用到穷举搜索,比如过河问题

三个和尚和三个妖怪过河
船只能载两个
任何时候只要妖怪数量大于和尚数量
妖怪就要吃掉和尚
求解过河方案

这个问题首先确定状态的数据结构,状态就是两岸monk和monster的数量,同时有一些配套操作

  • 判断是否是最终状态
  • 状态迁移
  • 判断状态是否有效
class ItemState{
friend  ostream& operator<<(ostream&, const ItemState&);
public:
    bool operator==(const ItemState&);
    bool isFinalState();
    bool takeAction(ItemState& next, const ACTION_EFFECTION& action);
    bool isValid();
    int local_monster;
    int local_monk;
    int remote_monster;
    int remote_monk;
    BOAT_LOCATION boat;
};

动作有如下定义

typedef enum{
    ONE_MONSTER_GO = 0,
    TWO_MONSTER_GO,
    ONE_MONK_GO,
    TWO_MONK_GO,
    ONR_MONSTER_ONE_MONK_GO,
    ONE_MONSTER_BACK,
    TWO_MONSTER_BACK,
    ONE_MONK_BACK,
    TWO_MONK_BACK,
    ONR_MONSTER_ONE_MONK_BACK,
    INVALID_ACTION_NAME,
}ACTION_NAME;   
    
typedef struct{
    ACTION_NAME act;
    BOAT_LOCATION boat_to;
    int move_monster;
    int move_monk;
}ACTION_EFFECTION;

得到action的列表,作为穷举的依据

static ACTION_EFFECTION actEffect[] =
{
    {ONE_MONSTER_GO,            REMOTE, -1, 0},
    {TWO_MONSTER_GO,            REMOTE, -2, 0},
    {ONE_MONK_GO,               REMOTE, 0,  -1},
    {TWO_MONK_GO,               REMOTE, 0,  -2},
    {ONR_MONSTER_ONE_MONK_GO,   REMOTE, -1, -1},
    {ONE_MONSTER_BACK,          LOCAL,  1,  0},
    {TWO_MONSTER_BACK,          LOCAL,  2,  0},
    {ONE_MONK_BACK,             LOCAL,  0,  1},
    {TWO_MONK_BACK,             LOCAL,  0,  2},
    {ONR_MONSTER_ONE_MONK_BACK, LOCAL,  1,  1}
};

搜索时

void searchState(deque<ItemState> &states)
{
    int act;
    ItemState current = states.back();
    if(current.isFinalState())
    {
        printResult(states);
        return;
    }
    for(act = 0; act < INVALID_ACTION_NAME; act++)
    {
        //debug_out << act <<endl;
        searchStateOnOneAction(states, current, actEffect[act]);
    }
}

void searchStateOnOneAction(deque<ItemState> &states, ItemState &current, ACTION_EFFECTION& actEff)
{
    ItemState next;
    bool canTackAction;
    canTackAction = current.takeAction(next, actEff);
    //debug_out << next;
    if(canTackAction && !isProcessed(states, next))
    {
        //debug_out << "valid" << endl;
        // printResult(states);
        states.push_back(next);
        searchState(states);
        states.pop_back();
    }
}

两个函数起到了递归的作用,同时使用deque避免出现环路

bool isProcessed(deque<ItemState> &states, ItemState& state)
{
    auto it = states.end();
    it = find_if(states.begin(), states.end(), [&](ItemState& s){return s==state;});
    return(it!=states.end());
}

完整代码见https://github.com/lclei/algorithm_fun/tree/master/MonkAndMonster

最后的找到了四种方案

o stands for monk, and x stands for monster.
3o  ||  0o
3x  ||  0x
boat||    

3o  ||  0o
1x  ||  2x
    ||boat

3o  ||  0o
2x  ||  1x
boat||    

3o  ||  0o
0x  ||  3x
    ||boat

3o  ||  0o
1x  ||  2x
boat||    

1o  ||  2o
1x  ||  2x
    ||boat

2o  ||  1o
2x  ||  1x
boat||    

0o  ||  3o
2x  ||  1x
    ||boat

0o  ||  3o
3x  ||  0x
boat||    

0o  ||  3o
1x  ||  2x
    ||boat

0o  ||  3o
2x  ||  1x
boat||    

0o  ||  3o
0x  ||  3x
    ||boat

o stands for monk, and x stands for monster.
3o  ||  0o
3x  ||  0x
boat||    

3o  ||  0o
1x  ||  2x
    ||boat

3o  ||  0o
2x  ||  1x
boat||    

3o  ||  0o
0x  ||  3x
    ||boat

3o  ||  0o
1x  ||  2x
boat||    

1o  ||  2o
1x  ||  2x
    ||boat

2o  ||  1o
2x  ||  1x
boat||    

0o  ||  3o
2x  ||  1x
    ||boat

0o  ||  3o
3x  ||  0x
boat||    

0o  ||  3o
1x  ||  2x
    ||boat

1o  ||  2o
1x  ||  2x
boat||    

0o  ||  3o
0x  ||  3x
    ||boat

o stands for monk, and x stands for monster.
3o  ||  0o
3x  ||  0x
boat||    

2o  ||  1o
2x  ||  1x
    ||boat

3o  ||  0o
2x  ||  1x
boat||    

3o  ||  0o
0x  ||  3x
    ||boat

3o  ||  0o
1x  ||  2x
boat||    

1o  ||  2o
1x  ||  2x
    ||boat

2o  ||  1o
2x  ||  1x
boat||    

0o  ||  3o
2x  ||  1x
    ||boat

0o  ||  3o
3x  ||  0x
boat||    

0o  ||  3o
1x  ||  2x
    ||boat

0o  ||  3o
2x  ||  1x
boat||    

0o  ||  3o
0x  ||  3x
    ||boat

o stands for monk, and x stands for monster.
3o  ||  0o
3x  ||  0x
boat||    

2o  ||  1o
2x  ||  1x
    ||boat

3o  ||  0o
2x  ||  1x
boat||    

3o  ||  0o
0x  ||  3x
    ||boat

3o  ||  0o
1x  ||  2x
boat||    

1o  ||  2o
1x  ||  2x
    ||boat

2o  ||  1o
2x  ||  1x
boat||    

0o  ||  3o
2x  ||  1x
    ||boat

0o  ||  3o
3x  ||  0x
boat||    

0o  ||  3o
1x  ||  2x
    ||boat

1o  ||  2o
1x  ||  2x
boat||    

0o  ||  3o
0x  ||  3x
    ||boat

相关文章

  • 穷举搜索

    穷举搜索 实质是创建一个状态树,边建立边剪枝,得到最终状态输出步骤有: 列出表示状态的数据结构 列出在状态之间迁移...

  • 回溯算法

    定义 回溯算法也可以理解为穷举法,是穷举法的一个巧妙实现,也可以叫试探法,是一种系统搜索问题的解决办法,是暴力搜索...

  • 调参必备--Grid Search网格搜索

    什么是Grid Search 网格搜索? Grid Search:一种调参手段;穷举搜索:在所有候选的参数选择中,...

  • 人工智能期末复习笔记2018-01-03

    对抗搜索 博弈问题博弈问题穷举有时可以获得必胜结果,但是稍微复杂一点的问题就很难穷举了。 极大极小过程 alpha...

  • 最长公共子序列(LCS)问题

    LCS问题的解决思路 穷举法 解最长公共子序列问题时最容易想到的算法是穷举搜索法,即对X的每一个子序列,检查它是否...

  • 学习回溯法中的思考

    回溯法是一种用递归来实现的穷举法,是深度搜索优先算法。 我又联想到了,深度搜索优先和广度搜索优先,与栈和队...

  • 图遍历与回溯 图搜索->形成搜索树 穷举法。 贪心法。多步决策,每步选择使得构成一个问题的可能解,同时满足目标函数...

  • 回溯和深度优先遍历(一)

    本文谈谈回溯算法和深度优先搜索。 回溯法可以看成是一种暴力搜索,穷举。看起来“暴力”给人的印象似乎都是比较简单,而...

  • 《穷举》

    遇事不解莫慌张,暴力枚举来帮忙。穷举所有可能性,已明前路在何方。

  • 回溯法

    回溯法:穷举搜索解空间树(包含了问题求解过程中的每一种情况),以寻找最优解。一般采用深度优先方式搜索解空间,并在搜...

网友评论

      本文标题:穷举搜索

      本文链接:https://www.haomeiwen.com/subject/miqasctx.html