贪心:jump 游戏(获取最少跳跃的次数以及跳跃路径)

一个数组存储了非负整型数据,数组中的第i个元素a[i],代表了可以从数组第i个 位置最多向前跳跃a[i]步;已知数组各元素的情况下,求是否可以从数组的第0个位置跳跃到数组的最后一个元素的位置,返回最少跳跃的次数以及跳跃过程的路径(以数组下标标识)

例如:
nums = [2, 3, 1, 1, 4] ,可以从nums[0] = 2 跳跃至 nums[4] = 4; 最少跳跃次数为2,跳跃路径为[0,1,4]

该跳跃过程和判断最终节点是否可达类似,不过需要注意一点:为了保证获取到的跳跃次数是最少的,需要在每一次的跳跃范围中尽可能得跳跃更多的节点,且在第一次跳跃结束后获取到后续所有节点的跳跃范围之后再进行下一次跳跃。

贪心规律:
在无法到达更远的地方时,在这之前应该跳到一个可以到达更 远位置的位置!

贪心:jump 游戏(获取最少跳跃的次数以及跳跃路径)_第1张图片
类似如上,在index[i]的节点上无法跳跃到更远的节点了,此时应该选择index[i-2],使得其能够跳跃更远的节点。

同时如果想要获取跳跃的路径,即可将跳跃过程中发生的节点更替的时的下标记录下来,在跳跃的过程中加入到路径数组即可。

实现过程如下:

/*获取最少的跳跃次数*/
int get_judge_finish(vector<int> &stage) {
    if (stage.size() < 2) {
        return 0;
    }
    
    int current_max_pos = stage[0]; //当前能够跳跃到的最远的节点
    int pre_max_pos = stage[0];    //各个位置能够跳跃到的最远的节点
    int least_jum = 1;

    for (int i = 0;i < stage.size(); ++i) {
        if(i > current_max_pos) { //当遍历完当前节点当范围时进行跳跃,跳跃点选择各个位置能够跳跃当最远的点
            least_jum ++;
            current_max_pos = pre_max_pos;
        }
        if (pre_max_pos < stage[i] + i){ //获取各个位置能够跳跃到的最远的点
            pre_max_pos = stage[i] + i;
        }
    }
    return least_jum;
}

/*获取最少的跳跃路径*/
vector<int> get_jump_path(vector<int> &stage) {
    if (stage.size() < 2) {
        return stage;
    }
    
    int current_max_pos = stage[0];
    int pre_max_pos = stage[0];
    int pre_max_index = 0; //记录各个位置能够跳的最远的节点下标

    vector<int> jump_path; //记录跳跃路径
    jump_path.push_back(0); //开头节点起跳

    for (int i = 0;i < stage.size(); ++i) {
        if(i > current_max_pos) {
            current_max_pos = pre_max_pos;
            /*当开始跳时,记录的各个位置能够跳的最远的下标*/
            jump_path.push_back(pre_max_index); 
        }   
        if (pre_max_pos < stage[i] + i){
            pre_max_pos = stage[i] + i;
            pre_max_index = i;
        }
    }
    jump_path.push_back(stage.size()-1);
    return jump_path;
}

测试代码如下:

#include 
#include 

using namespace std;
/*判断是否能够完成跳跃*/
bool judge_finish(vector<int> &stage) {
    vector<int> index;
    for (int i = 0; i < stage.size(); ++i) {
        index.push_back(i + stage[i]);
    }

    int max_index = stage[0];
    int jump = 0;
    while(jump < index.size() && jump <= max_index) {
        if (max_index < index[jump]) {
            max_index = index[jump];
        }
        jump ++;
    }

    if (jump == index.size()) {
        return true;
    }
    return false;
}
/*获取最少的跳跃次数*/
int get_judge_finish(vector<int> &stage) {
    if (stage.size() < 2) {
        return 0;
    }
    
    int current_max_pos = stage[0];
    int pre_max_pos = stage[0];
    int least_jum = 1;

    for (int i = 0;i < stage.size(); ++i) {
        if(i > current_max_pos) {
            least_jum ++;
            current_max_pos = pre_max_pos;
        }
        if (pre_max_pos < stage[i] + i){
            pre_max_pos = stage[i] + i;
        }
    }
    return least_jum;
}
/*获取跳跃路径*/
vector<int> get_jump_path(vector<int> &stage) {
    if (stage.size() < 2) {
        return stage;
    }
    
    int current_max_pos = stage[0];
    int pre_max_pos = stage[0];
    int pre_max_index = 0;
    vector<int> jump_path;
    jump_path.push_back(0);

    for (int i = 0;i < stage.size(); ++i) {
        if(i > current_max_pos) {
            current_max_pos = pre_max_pos;
            jump_path.push_back(pre_max_index);
        }   
        if (pre_max_pos < stage[i] + i){
            pre_max_pos = stage[i] + i;
            pre_max_index = i;
        }
    }
    jump_path.push_back(stage.size()-1);
    return jump_path;
}

int main() {
    vector<int> s;
    int tmp;
    cout << "input arr " <<endl;
    for (int i =0;i < 5; ++i) {
        cin >> tmp;
        s.push_back(tmp);
    }

    cout << "the least times to jump is " << get_judge_finish(s) << endl;
    cout << "the true or false that judge the result is " << judge_finish(s) << endl;

    cout << "jump path is " << endl;
    vector<int> path = get_jump_path(s);
    for (int i = 0;i < path.size(); ++i) {
        cout << path[i] << " ";
    }

    return 0;
}

输出如下:

input arr 
3 2 2 0 5
the least times to jump is 2
the true or false that judge the result is 1
jump path is 
0 2 4

input arr 
2 3 1 1 4
the least times to jump is 2
the true or false that judge the result is 1
jump path is 
0 1 4 

你可能感兴趣的:(数据结构和算法,#,编程语言:C++,编程语言)