鸣人和佐助问题——基于BFS算法的解决策略

问题描述:

已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置。地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸的手下才能到这些位置。鸣人有一定数量的查克拉,每一个单位的查克拉可以打败一个大蛇丸的手下。假设鸣人可以往上下左右四个方向移动,每移动一个距离需要花费1个单位时间,打败大蛇丸的手下不需要时间。如果鸣人查克拉消耗完了,则只可以走到没有大蛇丸手下的位置,不可以再移动到有大蛇丸手下的位置。佐助在此期间不移动,大蛇丸的手下也不移动。请问,鸣人要追上佐助最少需要花费多少时间?

输入 
输入的第一行包含三个整数:M,N,T。代表M行N列的地图和鸣人初始的查克拉数量T。0 < M,N < 200,0 ≤ T < 10
后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。

输出 
输出包含一个整数R,代表鸣人追上佐助最少需要花费的时间。如果鸣人无法追上佐助,则输出-1。

代码:

// 使用广度优先算法来解决鸣人和佐助问题联系
typedef struct NodeInfo_ {
        /* 到达此节点所花费的时间 */
        int times;
        /* 走到当前节点时查拉克的数量 */
        int tipCount;
        /* 节点坐标 */
        pair<int, int> pt;
} NodeInfo;

/*  
        @map_:地图
        @location1:初始鸣人的位置
        @location2:初始佐助的位置

        @@map_中节点取值:0:表示已访问节点
                          1:正常节点
                          2:大蛇丸
*/
int 
minTimeSearch(vector<vector<int>>& map_, pair<int, int> location1, 
                pair<int, int> location2, int tipsCount)
{
        queue<NodeInfo> q_;

        q_.push(NodeInfo{ 0, tipsCount, location1});
        map_[location1.first][location1.second] = 0;
  
        while (q_.size()) {

                NodeInfo node_ = q_.front();
                pair<int, int> pt_ = node_.pt;

                //检查是否赶上了佐助 
                if (node_.pt == location2) {
                        return node_.times;
                }

                NodeInfo tmp_;
                tmp_.tipCount = node_.tipCount;

                for (int i = 0; i < 4; i++) {
                        // 上
                        if (i == 0 && pt_.first > 0 && 
                                map_[pt_.first - 1][pt_.second] != 0) {

                                tmp_.pt = { pt_.first - 1, pt_.second };
                        }// 下
                        else if (i == 1 && pt_.first + 1 < map_.size() && 
                                map_[pt_.first + 1][pt_.second] != 0) {

                                tmp_.pt = { pt_.first + 1, pt_.second };
                        }// 左
                        else if (i == 2 && pt_.second > 0 &&
                                map_[pt_.first][pt_.second - 1] != 0) {

                                tmp_.pt = { pt_.first, pt_.second - 1 };
                        }// 右
                        else if (i == 3 && pt_.second + 1 < map_[0].size() &&
                                map_[pt_.first][pt_.second + 1] != 0) {

                                tmp_.pt = { pt_.first, pt_.second + 1};
                        }
                        else {
                                continue;
                        }

                        // 检查调整此时节点查拉克的数量
                        if (map_[tmp_.pt.first][tmp_.pt.second] == 2) {

                                if (node_.tipCount > 0) {
                                        tmp_.tipCount--;
                                }
                                else
                                        continue;
                        }

                        tmp_.times = node_.times + 1;
                        map_[tmp_.pt.first][tmp_.pt.second] = 0;
                        q_.push(tmp_);
                }
                q_.pop();
        }

        /* 不可解 */
        return -1;
}

 有问题留言,谢谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值