贪心:jump 游戏(获取最少跳跃的次数以及跳跃路径)
生活随笔
收集整理的這篇文章主要介紹了
贪心:jump 游戏(获取最少跳跃的次数以及跳跃路径)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個數組存儲了非負整型數據,數組中的第i個元素a[i],代表了可以從數組第i個 位置最多向前跳躍a[i]步;已知數組各元素的情況下,求是否可以從數組的第0個位置跳躍到數組的最后一個元素的位置,返回最少跳躍的次數以及跳躍過程的路徑(以數組下標標識)
例如:
nums = [2, 3, 1, 1, 4] ,可以從nums[0] = 2 跳躍至 nums[4] = 4; 最少跳躍次數為2,跳躍路徑為[0,1,4]
該跳躍過程和判斷最終節點是否可達類似,不過需要注意一點:為了保證獲取到的跳躍次數是最少的,需要在每一次的跳躍范圍中盡可能得跳躍更多的節點,且在第一次跳躍結束后獲取到后續所有節點的跳躍范圍之后再進行下一次跳躍。
貪心規律:
在無法到達更遠的地方時,在這之前應該跳到一個可以到達更 遠位置的位置!
類似如上,在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 <iostream>
#include <vector>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 4input 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
總結
以上是生活随笔為你收集整理的贪心:jump 游戏(获取最少跳跃的次数以及跳跃路径)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hey boy是哪首歌啊?
- 下一篇: 王者荣耀体验服怎么申请点券?