5552. 到家的最少跳跃次数 BFS

有一只跳蚤的家在数轴上的位置 x 处。请你帮助它从位置 0 出发,到达它的家。

跳蚤跳跃的规则如下:

它可以 往前 跳恰好 a 个位置(即往右跳)。
它可以 往后 跳恰好 b 个位置(即往左跳)。
它不能 连续 往后跳 2 次。
它不能跳到任何 forbidden 数组中的位置。
跳蚤可以往前跳 超过 它的家的位置,但是它 不能跳到负整数 的位置。

给你一个整数数组 forbidden ,其中 forbidden[i] 是跳蚤不能跳到的位置,同时给你整数 a, b 和 x ,请你返回跳蚤到家的最少跳跃次数。如果没有恰好到达 x 的可行方案,请你返回 -1 。

示例 1:

输入:forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
输出:3
解释:往前跳 3 次(0 -> 3 -> 6 -> 9),跳蚤就到家了。
示例 2:

输入:forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
输出:-1
示例 3:

输入:forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
输出:2
解释:往前跳一次(0 -> 16),然后往回跳一次(16 -> 7),跳蚤就到家了。

提示:

1 <= forbidden.length <= 1000
1 <= a, b, forbidden[i] <= 2000
0 <= x <= 2000
forbidden 中所有位置互不相同。
位置 x 不在 forbidden 中。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-jumps-to-reach-home
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。\

注意,这里向后跳到达的点不需要标记为访问过。因为一个点可以向前跳到,也可以反向跳到。

class Solution {
public:
    struct Node {
        int idx;
        bool flag;
        Node (int idx, bool flag): idx(idx), flag(flag) {}
    };

    bool vis[4005];

    int minimumJumps(vector<int>& forbidden, int a, int b, int x) {
        if (x == 0) {
            return 0;
        }

        for (auto x : forbidden) {
            vis[x] = true;
        }

        int ans = 0;
        queue <Node> q;
        q.push(Node(0,false));
        vis[0] = true;
        while (!q.empty()) {
            ans++;
            int len = q.size();
            for (int i = 0; i < len; i++) {
                auto now = q.front();
                q.pop();
                int tmp = now.idx + a;
                if (tmp == x) {
                    return ans;
                }
                else
                if (tmp < 4000 && !vis[tmp]) {
                    vis[tmp] = true;
                    q.push(Node(tmp, false));
                }
                tmp = now.idx - b;
                if (tmp == x) {
                    return ans;
                }
                else
                if (tmp >= 0 && !vis[tmp] && !now.flag) {
                    q.push(Node(tmp, true));
                }
            }
        }

        return -1;
    }
};
posted @ 2020-11-15 16:08  _西瓜不甜  阅读(189)  评论(0编辑  收藏  举报