62.Unique Paths---dp
题目大意:给一个m*n的方格,从左上角走到右下角,中间无任何障碍,问有多少种走法。
法一:DFS,超时,简单模板深搜,无任何剪枝,结果一半的数据超时。代码如下:
public int uniquePaths(int m, int n) {
int f[][] = {{0, 1}, {1, 0}};
boolean vis[][] = new boolean[m][n];
return dfs(m, n, 0, 0, 0, f, vis);
}
public static int dfs(int m, int n, int x, int y, int cnt, int f[][], boolean vis[][]) {
if(x == m - 1 && y == n - 1) {
System.out.println("answer:" + cnt);
cnt++;
return cnt;
}
for(int i = 0; i < 2; i++) {
int cnt_x = x + f[i][0];
int cnt_y = y + f[i][1];
if(cnt_x < m && cnt_y < n && vis[cnt_x][cnt_y] == false) {
vis[cnt_x][cnt_y] = true;
cnt = dfs(m, n, cnt_x, cnt_y, cnt, f, vis);
vis[cnt_x][cnt_y] = false;
}
}
return cnt;
}
法二(借鉴):很简单的dp,dp[i][j]表示当终点坐标是[i,j]时,所有可能的路径总数代码如下(耗时1ms):
public int uniquePaths(int m, int n) {
int dp[][] = new int[m][n];
//初始化
//对于第一列和第一行,走的路径数应该初始化为1
for(int i = 0; i < m; i++) {
dp[i][0] = 1;
}
for(int i =0 ; i < n; i++) {
dp[0][i] = 1;
}
//计算dp
//对于dp[i][j],每一个坐标[i,j],都可以由其左侧和上侧走一步而来。类似于杨辉三角
for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
法三(借鉴):一维dp,还不是很懂,是怎么来的,暂且记一下。代码如下(耗时0ms):
public int uniquePaths(int m, int n) {
int dp[] = new int[n];
//初始化,dp[i]表示某一行第i列的路径总数
for(int i = 0; i < n; i++) {
dp[i] = 1;
}
//外层循环是每一行,第1行计算了,第2行就是利用了第1行的值,所以这里也是左+上的和得到当前值,只是节约了空间
for(int i = 1; i < m; i++) {
//对于每一行,逐一判定每一列的路径值。
for(int j = 1; j < n; j++) {
dp[j] += dp[j - 1];
}
}
//两层循环后,得到的最终的值就是所有路径的结果值。
return dp[n - 1];
}
法四(借鉴):数学方法,由左上到右下,向下要走m-1步,向右要走n-1步,也就是要走C(m+n-2, m-1)或C(m+n-2, n-1)步。而C(m, n) = A(m, n)/n!,又A(m, n)=m!/(m-n)!。则C(m+n-2, m-1)=m*(m+1)*(m+2)...*(m+n-2)/1*2*3...*(n-1)。代码如下:
public int uniquePaths(int m, int n) {
double res = 1;
//计算组合数
for(int i = 1; i <= n - 1; i++) {
res = res * (m + i - 1) / i;
}
return (int)res;
}
62.Unique Paths---dp的更多相关文章
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 刷题62. Unique Paths
一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...
- 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 62. Unique Paths (Graph; DP)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 62. Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 62. Unique Paths
题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...
- LeetCode OJ 62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【一天一道LeetCode】#62. Unique Paths
一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...
- [leetcode]62. Unique Paths 不同路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
随机推荐
- Importing/Indexing database (MySQL or SQL Server) in Solr using Data Import Handler--转载
原文地址:https://gist.github.com/maxivak/3e3ee1fca32f3949f052 Install Solr download and install Solr fro ...
- BZOJ4899 记忆的轮廓(概率期望+动态规划+决策单调性)
容易发现跟树没什么关系,可以预处理出每个点若走向分叉点期望走多少步才能回到上个存档点,就变为链上问题了.考虑dp,显然有f[i][j]表示在i~n中设置了j个存档点,其中i设置存档点的最优期望步数.转 ...
- l洛谷 (水题)P4144 大河的序列
题目戳 Solution: 这题前面都是废话,关键的一句就是本题求的是序列中连续一段的相与值(&)+相或值(|)最大,然后对这个值进行快速幂取模.考虑到两个数相与最大能得到的就是这两个数中的最 ...
- Codeforces707Div2
A Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was ups ...
- 中南多校对抗赛 第三场 E
E:Eulerian Flight Tour 题意: 给你一张无向图,要你给这个图加边使得其形成一个欧拉回路 题解: 首先使得所有节点的度都为偶数,然后将这个图联通起来 对于度为奇数的点,将将他和他的 ...
- codeforces 55D 数位dp
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 13.UiAutomator 辅助APK的使用
一.在测试中弹出提示框 UiAutomator无法直接实现,,但是可以通过让辅助APK接受UiAutomator发送的指令信息来实现,使用am命令 实例: 1.新建辅助apk,用来接收Uiautoma ...
- 单例 ------ C++实现
基础知识掌握: 单例考虑三点:内存何时释放.运行速度如何.多线程下能否保证只有一个实例 如果获取对象的返回值类型是引用,返回值赋值给变量而不是引用会进行对象的拷贝,这样就会出现两个对象,可以把显示声明 ...
- Vue 插槽详解
Vue插槽,是学习vue中必不可少的一节,当初刚接触vue的时候,对这些掌握的一知半解,特别是作用域插槽一直没明白. 后面越来越发现插槽的好用. 分享一下插槽的一些知识吧. 分一下几点: 1.插槽内可 ...
- IBatisNet+Oracle.ManagedDataAccess打造无需安装oracle客户端和ODP即可连接oracle数据库
库环境: Oracle.ManagedDataAccess 版本:4.122.1.0 IBatisNet 版本:1.6.2 其实很简单的,只需在驱动配置那里添加上Oracle.ManagedData ...