当前位置:网站首页>暴力递归到动态规划 01 (机器人移动)
暴力递归到动态规划 01 (机器人移动)
2022-07-29 03:20:00 【涛涛英语学不进去】
1. 题目

2. 暴力递归
/** * @param length 总长度 * @param cur 当前位置 * @param steps 剩余步数 * @param target 目标位置 * @return */
public static int getWays(int length, int cur, int steps, int target) {
//如果剩余步数为0
if (steps == 0) {
//判断当前位置是不是目标位置
if (cur == target) {
return 1;
} else {
return 0;
}
}
//左边界
if (cur == 1) {
//移动到2位置
return getWays(length, 2, steps - 1, target);
}
//右边界
if (cur == length) {
//移动到右边倒数第二个位置
return getWays(length, length - 1, steps - 1, target);
}
//中间位置
return getWays(length, cur - 1, steps - 1, target) + getWays(length, cur + 1, steps - 1, target);
}
动态规划
/** * @param length 总长度 * @param cur 当前位置 * @param steps 剩余步数 * @param target 目标位置 * @return */
public static int getWays2(int length, int cur, int steps, int target) {
//纵行 为 路径,横行为剩余步数
int[][] dp = new int[length + 1][steps + 1];
//第1列初始化,当步数为0的时候,如果
dp[target][0] = 1;
for (int i = 1; i <= steps; i++) {
//第0行为length=0,不存在
//第一行可以直接初始化,因为是位于左边界,只能移动到第二个位置
dp[1][i] = dp[2][i - 1];
for (int j = 2; j < length; j++) {
//除了一行和最后一行(左右边界),其余行都是左右之和
dp[j][i] = dp[j - 1][i - 1] + dp[j + 1][i - 1];
}
//如果遇到右边界,直接走倒数第二位
dp[length][i] = dp[length - 1][i - 1];
}
//返回目标位置的数量
return dp[cur][steps];
}
public static void main(String[] args) {
System.out.println(getWays(4, 2, 4, 4));
System.out.println(getWays(5, 2, 6, 4));
System.out.println(getWays2(4, 2, 4, 4));
System.out.println(getWays2(5, 2, 6, 4));
}
边栏推荐
- C陷阱与缺陷 第2章 语法“陷阱” 2.6 “悬挂”else引发的问题
- [technology 1]
- 年内首个“三连跌” 95号汽油回归“8元时代“
- 2. Nodejs -- path (\dirname, \filname), URL URL, querystring module, mime module, various paths (relative paths), web page loading (interview questions *)
- Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
- July 28, 2022 Gu Yujia's study notes
- During the year, the first "three consecutive falls" of No. 95 gasoline returned to the "8 Yuan era"“
- Shell编程规范与变量
- GJB common confused concepts
- Tp5.0 applet users do not need to log in and directly obtain the user's mobile number.
猜你喜欢

How to solve the time zone problem in MySQL timestamp

Producer consumer model of concurrent model

Example analysis of while, repeat and loop loops in MySQL process control

"PHP Basics" output approximate value of PI
![[freeswitch development practice] unimrcp compilation and installation](/img/ef/b82326152326293bf98e89da28b887.png)
[freeswitch development practice] unimrcp compilation and installation

makefile详解

04 | background login: login method based on account and password (Part 1)

Singleton mode (hungry and lazy)
![[open the door to the new world] see how the old bird of testing plays API testing between applause](/img/79/1bc836cefef24d23e09d9865ff1fba.png)
[open the door to the new world] see how the old bird of testing plays API testing between applause

12_ UE4 advanced_ Change a more beautiful character model
随机推荐
Sanzi chess (player + computer)
C陷阱与缺陷 第2章 语法“陷阱” 2.6 “悬挂”else引发的问题
SAP 中国本地化内容汇总
C traps and defects Chapter 2 syntax "traps" 2.6 problems caused by "hanging" else
照片比例校正工具:DxO ViewPoint 3 直装版
Digital image processing Chapter 10 - image segmentation
MySQL流程控制之while、repeat、loop循环实例分析
04 | background login: login method based on account and password (Part 1)
西瓜书学习第六章---SVM
LeetCode 1331 数组序号转换[Map] HERODING的LeetCode之路
C traps and defects Chapter 3 semantic "traps" 3.1 pointers and arrays
Shell programming specifications and variables
Minesweeping simple version
C陷阱与缺陷 第3章 语义“陷阱” 3.3 作为参数的数组声明
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
Alibaba Sentinel - workflow and principle analysis
Introduction to JVM foundation I (memory structure)
Alibaba Sentinel - 工作流程及原理解析
Watermelon book learning Chapter 6 -- SVM
C陷阱与缺陷 第3章 语义“陷阱” 3.4 避免“举偶法”