当前位置:网站首页>暴力递归到动态规划 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));
}
边栏推荐
- 照片比例校正工具:DxO ViewPoint 3 直装版
- 01-SDRAM:初始化模块的代码
- Three military product baselines (functional baseline, distribution baseline, product baseline) and the documents contained in the baseline
- shell脚本总结
- 【打开新世界大门】看测试老鸟如何把API 测试玩弄在鼓掌之间
- Example analysis of while, repeat and loop loops in MySQL process control
- 军品研制过程-转阶段
- Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names
- Introduction and advanced level of MySQL (11)
- 3D advanced renderer: artlandis studio 2021.2 Chinese version
猜你喜欢

单例模式(饿汉式 懒汉式)

Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation

Calculation of array serial number of force deduction questions (daily question 7/28)

During the year, the first "three consecutive falls" of No. 95 gasoline returned to the "8 Yuan era"“

How does DataGrid export and recover the entire database data, using a single SQL file

年内首个“三连跌” 95号汽油回归“8元时代“

3D高级渲染器:Artlantis studio 2021.2中文版

Redis之sentinel哨兵集群怎么部署
![Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding](/img/be/d429d0c437dc5ed7cb4448e223a83a.png)
Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding

ShardingSphere之水平分表实战(三)
随机推荐
C陷阱与缺陷 第3章 语义“陷阱” 3.7 求值顺序
服务器运行管理制度
Alibaba Sentinel - 工作流程及原理解析
HDU多校第二场 1011 DOS Card
[open the door to the new world] see how the old bird of testing plays API testing between applause
Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names
C traps and defects Chapter 3 semantic "traps" 3.3 array declaration as parameters
【C】 Array
What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
Redis configuration cache expiration listening event trigger
Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?
增量实时灾备笔记
C language programming | exchange binary odd and even bits (macro Implementation)
Tp5.0 applet users do not need to log in and directly obtain the user's mobile number.
简历竟然敢写精通并发编程,那你说说AQS为什么要用双向链表?
MySQL installation and configuration super detailed tutorial and simple database and table building method
2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)
Calculation of array serial number of force deduction questions (daily question 7/28)
微信为之疯狂的Glide使用——之生命周期学习
MYCAT read / write separation configuration