当前位置:网站首页>Violence recursion to dynamic programming 01 (robot movement)
Violence recursion to dynamic programming 01 (robot movement)
2022-07-29 03:27:00 【Taotao can't learn English】
1. subject

2. Recurrence of violence
/** * @param length Total length * @param cur The current position * @param steps Remaining steps * @param target Target location * @return */
public static int getWays(int length, int cur, int steps, int target) {
// If the remaining steps are 0
if (steps == 0) {
// Judge whether the current position is the target position
if (cur == target) {
return 1;
} else {
return 0;
}
}
// Left boundary
if (cur == 1) {
// Move to 2 Location
return getWays(length, 2, steps - 1, target);
}
// Right border
if (cur == length) {
// Move to the penultimate position on the right
return getWays(length, length - 1, steps - 1, target);
}
// In the middle
return getWays(length, cur - 1, steps - 1, target) + getWays(length, cur + 1, steps - 1, target);
}
Dynamic programming
/** * @param length Total length * @param cur The current position * @param steps Remaining steps * @param target Target location * @return */
public static int getWays2(int length, int cur, int steps, int target) {
// Longitudinal by route , Horizontal row is the number of remaining steps
int[][] dp = new int[length + 1][steps + 1];
// The first 1 Column initialization , When the number of steps is 0 When , If
dp[target][0] = 1;
for (int i = 1; i <= steps; i++) {
// The first 0 Behavior length=0, non-existent
// The first line can be initialized directly , Because it is located on the left boundary , You can only move to the second position
dp[1][i] = dp[2][i - 1];
for (int j = 2; j < length; j++) {
// Except for one line and the last line ( Left and right ), The rest of the lines are the sum of left and right
dp[j][i] = dp[j - 1][i - 1] + dp[j + 1][i - 1];
}
// If you encounter the right boundary , Go straight to the penultimate
dp[length][i] = dp[length - 1][i - 1];
}
// Return the number of target locations
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));
}
边栏推荐
- A simple and general method to obtain the size of function stack space
- 如何判定是stun协议
- Reproduce 20 character short domain name bypass and XSS related knowledge points
- Unity game special effects
- Code speed optimization
- The difference between /g /m /i of JS regular expressions
- Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it
- C obtains JSON format data asynchronously from the web address
- Mathematical modeling -- analytic hierarchy process model
- [freeswitch development practice] unimrcp compilation and installation
猜你喜欢

Shardingsphere's level table practice (III)

【科技1】
![[freeswitch development practice] unimrcp compilation and installation](/img/ef/b82326152326293bf98e89da28b887.png)
[freeswitch development practice] unimrcp compilation and installation

Alibaba Sentinel - workflow and principle analysis

Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names

mysql的timestamp存在的时区问题怎么解决

Various minor problems of jupyter notebook, configuration environment, code completion, remote connection, etc

MySQL流程控制之while、repeat、loop循环实例分析
![[technology 1]](/img/eb/63baf1ae3931a156a0a5b377a9b7d1.jpg)
[technology 1]

How to solve the time zone problem in MySQL timestamp
随机推荐
Whole process record of yolov3 target detection
Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it
Microcomputer principle operation
Suffix automata (SAM) board from Jly
mysql的timestamp存在的时区问题怎么解决
Naive Bayes -- continuous data
RTP 发送 和接收 h265
A case of gradually analyzing the splitting of classes -- colorful ball collisions
Tonight at 7:30 | is the AI world in the eyes of Lianjie, Jiangmen, Baidu and country garden venture capital continue to be advanced or return to the essence of business
如何判定是stun协议
Introduction and advanced MySQL (XIV)
VISO fast rendering convolution block
Idea configuration web container and war packaging
Rongyun IM & RTC capabilities on new sites
复现20字符短域名绕过以及xss相关知识点
AI_ Drug: VAE of molecular generation model (I)
STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
Simple understanding of CDN, SDN and QoS
Rongyun real-time community solution
Shortcut key for adjusting terminal size in ROS