推箱子,增加箱子是否卡死的简单判断,别小看这段代码,可大量剪枝,效率提升可观。

10 篇文章 0 订阅
2 篇文章 0 订阅

// 推箱子,增加箱子是否卡死的简单判断,别小看这段代码,可大量剪枝,效率提升客观。

// 其实可以预先在棋盘上置上箱子不可以到达的位置,另外“死棋”的逻辑还可以增加。。。

 

//
//
// 判断箱子推到pt位置后,是否卡死
//
//
bool SokoState::isDead(Point& pt) const
{
    if ( !isDest(pt) && (
        isWall(pt.down())&&isWall(pt.left())
        || isWall(pt.down())&&isWall(pt.right())
        || isWall(pt.up())&&isWall(pt.left())
        || isWall(pt.up())&&isWall(pt.right())
        ) )
    {
        return true;
    }

    return false;
}

 

 

bool SokoState::moveBox(SokoState& r, Point& box, Point& soko, Point& nBox,
    char d) const
{
    if (!isSoko(soko) || !isSpace(nBox))
        return false;

    // Added by anning 2010年8月17日
    if (isDead(nBox))
        return false;

    r = *this;
    for (int i = 0; i < r.rows_; i++)  // 清除soko标志
    {
        for (int j = 0; j < r.cols_; j++)
        {
            r.map_[i][j] &= ~FlagSoko;
        }
    }
    r.map_[box.x_][box.y_] &= ~FlagBox;  // 移动箱子
    r.map_[box.x_][box.y_] |= FlagSoko;
    r.map_[nBox.x_][nBox.y_] |= FlagBox;
    r.stateEq();
    r.steps_.push_back(SokoStep(box.x_, box.y_, d));
    return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值