题目链接

题目大意:给一个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的更多相关文章

  1. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  2. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  7. 62. Unique Paths

    题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...

  8. 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 ...

  9. 【一天一道LeetCode】#62. Unique Paths

    一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...

  10. [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 ...

随机推荐

  1. hdu 6375 百度之星 度度熊学队列

    题目链接 Problem Description 度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣. 初始时有 N 个空的双端队列(编号为 1 到 N ),你要支持度度熊的 Q 次操作. ①1 ...

  2. Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)

    题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上 ...

  3. android面试(1)----布局

    1.说出android 五中布局,并说出各自作用? FrameLayout: 堆叠布局,也是就可以堆在一起.最长应用于Fragment的使用上. LinearLayout: 线性布局,可以是竖排或水平 ...

  4. 解题:ZJOI 2006 游戏排名系统

    题面 跟i207M学了学重载运算符后找前驱后继,然后就是练练无旋树堆 #include<map> #include<cstdio> #include<string> ...

  5. 【bzoj1502】月下柠檬树

    Portal -->bzoj1502 Solution 额其实说实在这题我一开始卡在了..这个阴影长啥样上QwQ 首先因为是平行光线然后投影到了一个水平面上所以这个投影一定是..若干个圆再加上这 ...

  6. python基础----递归函数(二分法、最大深度递归)

    递归函数 定义:在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. #例子1 # age()=age()+ n= age(n)=age(n-)+ # age()=ag ...

  7. Codeforces Round #404 (Div. 2)A B C二分

    A. Anton and Polyhedrons time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. array_udiff、array_udiff_assoc、array_udiff_uassoc 使用方法

      <?php // array_udiff 用自定义函数比较数组的差值(array_diff 使用内置函数) // 使用该函数我们通过进行更复杂的比较 class Rectangle { pu ...

  9. Android Studio 打包自定义apk文件名

    使用Android Studio打包的时候,我们有时候需要自定义apk的文件名,在此记录一下. 在app的build.gradle中,根节点下使用关键词def声明一个全局变量,用于获取打包的时间,格式 ...

  10. [php]unset函数

    unset($var); 释放一个变量空间 unset($var1, $var2...);释放多个变量空间 unset(var['数组元素内容']);释放数组元素 注意: 1.在函数内部释放全局变量和 ...