#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<stack>
#include<set>
#include<unordered_set>
#include<algorithm>
/********************************从左到右的尝试模型***********************************/
/*问题描述 一个机器人只能在arr上向左向右运动一步,当前为curlocation,经过K步,到达的目标为target 返回所有的运动可能数量*/
class Solution {
public:
/********************************暴力递归***********************************/
//返回当前curlocation位置,剩余reststeps的步数而到达target的方法数
int process(vector<int>arr, int curlocation, int reststeps ,int target ) {
if (reststeps == 0) {
if (curlocation == target) return 1;
else return 0;
}
int sumnum = 0;
if (curlocation == arr[0]) {
sumnum = process(arr, curlocation + 1, reststeps - 1, target);//返回当前curlocation右边一个位置位置,剩余reststeps-1的步数而到达target的方法数
return sumnum;
}
if (curlocation == arr[arr.size()-1]) {
sumnum = process(arr, curlocation -1, reststeps - 1, target);
return sumnum;
}
int nextstepright = process(arr, curlocation + 1, reststeps - 1, target);
int nextstepleft = process(arr, curlocation - 1, reststeps - 1, target);
return nextstepleft + nextstepright;
}
/********************************暴力递归***********************************/
/********************************记忆化搜索***********************************/
int process2(vector<int>arr, int curlocation, int reststeps, int target,vector<vector<int>>&dp) {
if (dp[curlocation][reststeps] != -1) {
return dp[curlocation][reststeps];
}
if (reststeps == 0) {
if (curlocation == target){
dp[curlocation][reststeps] = 1;
return dp[curlocation][reststeps];
}
else {
dp[curlocation][reststeps] = 0;
return dp[curlocation][reststeps];
}
}
int sumnum = 0;
if (curlocation == arr[0]) {
sumnum = process(arr, curlocation + 1, reststeps - 1, target);//返回当前curlocation右边一个位置位置,剩余reststeps-1的步数而到达target的方法数
dp[curlocation][reststeps] = sumnum;
return dp[curlocation][reststeps];
}
if (curlocation == arr[arr.size() - 1]) {
sumnum = process(arr, curlocation - 1, reststeps - 1, target);
dp[curlocation][reststeps] = sumnum;
return dp[curlocation][reststeps];
}
int nextstepright = process(arr, curlocation + 1, reststeps - 1, target);
int nextstepleft = process(arr, curlocation - 1, reststeps - 1, target);
dp[curlocation][reststeps] = nextstepleft + nextstepright;
return dp[curlocation][reststeps];
}
/********************************记忆化搜索***********************************/
};
/********************************dp***********************************/
int processingdp(vector<int>arr, int target,int reststeps) {
vector<vector<int>>dp;
dp.resize(arr.size() + 1, vector<int>(reststeps + 1,0));
dp[target][0] = 1;
for (int reststepsvar = 1; reststepsvar <= reststeps; reststepsvar++) {
dp[1][reststepsvar] = dp[1 + 1][reststepsvar - 1];
dp[arr.size()][reststepsvar] = dp[arr.size()-1][reststepsvar - 1];
for (int curlocation = 2; curlocation <= (arr.size() - 1); curlocation++) {
dp[curlocation][reststepsvar] = dp[curlocation + 1][reststepsvar - 1] + dp[curlocation - 1][reststepsvar - 1];
}
}
return dp[2][reststeps];
}
/********************************dp***********************************/
int main()
{
vector<int>arr = {1,2,3,4,5,6,7};
vector<vector<int>>dp;
int reststeps = 5;
dp.resize(arr.size() + 1, vector<int>(reststeps+1,-1));
Solution slt;
cout << slt.process(arr, 2, reststeps, 3) << endl;
cout << slt.process2(arr, 2, reststeps, 3, dp) << endl;
cout <<processingdp(arr, 3, reststeps) << endl;
system("pause");
return 0;
}
机器人运动递归法+dp
最新推荐文章于 2022-04-15 23:09:18 发布