LeetCode之机器人移动(动态规划)
来源:互联网 发布:java 线程优先级 编辑:程序博客网 时间:2024/06/03 17:42
题目描述
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
本题是求解机器人按指定可移动路径所有可以走的总路径数,每一步只能向右或者向下,相当于从每一点移动有两条路径可走,
不妨逆向思考下,走到当前点的路径数是到达其左边和上边路径数之和。那么状态转移方程就确定了,设到达每一点的路径数为F[i][j] (i,j分别为行数和列数), 则F[i][j]=F[i-1][j]+F[i][j-1]。
所以到达最右端底部的路径数即为F[m-1][n-1](建立一个二维数组[m][n]),代码如下。
public class Solution { public int uniquePaths(int m, int n) { if(m==0||n==0) return 1; int[][] F=new int[m][n]; for(int i=0;i<m;i++){ F[i][0]=1; } for(int j=0;j<n;j++) F[0][j]=1; for(int i=1;i<m;i++){ for(int j=1;j<n;j++) { //F[i][j]为到这个点的路径有几条 F[i][j]=F[i-1][j]+F[i][j-1]; } } return F[m-1][n-1]; }}
阅读全文
0 0
- LeetCode之机器人移动(动态规划)
- 动态规划-移动智能机器人
- LeetCode 之动态规划
- 动态规划之机器人走方格
- LeetCode之动态规划 (二)
- 【动态规划】登山机器人
- leetcode之动态规划(二)
- leetcode之动态规划(一)
- leetcode之动态规划(三)
- 2718:移动路线(2.6基本算法之动态规划)
- LeetCode之DP(动态规划)之House Robber
- LeetCode 之 Palindrome Partitioning II(动态规划)
- leetcode之palindrome-partitioning-ii(动态规划)
- 算法学习之动态规划(leetcode 91 Decode Ways)
- 算法学习之动态规划(leetcode 44 Wildcard Matching)
- 算法学习之动态规划(leetcode 174. Dungeon Game)
- 算法学习之动态规划(leetcode 72. Edit Distance)
- 算法学习之动态规划(leetcode 85. Maximal Rectangle)
- 欧几里德,斐波那契,牛顿迭代法java编程实现
- GetSidSubAuthorityCount
- Shell系列—Shell 文件包含
- 网络中立:YouTube联名上书
- php去除换行(回车换行)的三种方法
- LeetCode之机器人移动(动态规划)
- Superset安装文档及遇到的问题解决
- vlc源码分析(二) 播放流程
- Java学习之编程技巧
- git配置全局ignore
- C#SpinWait和volatile一点温习
- MD5加密使用方式
- 语义化版本 2.0.0
- cordova splashscreen插件