机器人运动递归法+dp

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值