当前位置:网站首页>暴力递归到动态规划 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));
}
边栏推荐
- Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
- C traps and defects Chapter 3 semantic "traps" 3.4 avoid "couple method"
- 美联储再加息,75基点 鲍威尔“放鸽”,美股狂欢
- MySQL流程控制之while、repeat、loop循环实例分析
- Flask的创建的流程day05-06之创建项目
- C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界
- Engineering boy: under 20 years old, ordinary but not mediocre
- Kubernetes-1.24.x feature
- 照片比例校正工具:DxO ViewPoint 3 直装版
- 【C】 Array
猜你喜欢

mycat读写分离配置

力扣刷题之数组序号计算(每日一题7/28)

【打开新世界大门】看测试老鸟如何把API 测试玩弄在鼓掌之间

「PHP基础知识」输出圆周率的近似值

MySQL installation and configuration super detailed tutorial and simple database and table building method

基于单片机烟雾温湿度甲醛监测设计
![[freeswitch development practice] media bug obtains call voice flow](/img/14/9a359403606c312b30733d4a015fa5.png)
[freeswitch development practice] media bug obtains call voice flow

Shell编程规范与变量

Verilog:阻塞赋值和非阻塞赋值

Asynchronous callback future mode of concurrent mode
随机推荐
简历竟然敢写精通并发编程,那你说说AQS为什么要用双向链表?
What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
Design of smoke temperature, humidity and formaldehyde monitoring based on single chip microcomputer
[technology 1]
复现20字符短域名绕过以及xss相关知识点
西瓜书学习第六章---SVM
Digital image processing Chapter 10 - image segmentation
Producer consumer model of concurrent model
Introduction and advanced level of MySQL (11)
C traps and defects Chapter 3 semantic "traps" 3.7 evaluation order
2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)
Chapter 2 VRP command line
Introduction and advanced MySQL (13)
Photo scale correction tool: DxO viewpoint 3 direct mount version
Unity 之游戏特效
MYSQL入门与进阶(十一)
Rongyun IM & RTC capabilities on new sites
C traps and defects Chapter 3 semantic "traps" 3.4 avoid "couple method"
C和指针 第3章 语义“陷阱” 3.5 空指针并非字符串
【C】 Array