题目链接

题目大意:给一个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. java 重写父类构造器

  2. 矩阵快速幂模板(pascal)

    洛谷P3390 题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格 ...

  3. bzoj5123 [Lydsy12月赛]线段树的匹配

    题意: 线段树是这样一种数据结构:根节点表示区间 [1, n]:对于任意一个表示区间 [l, r] 的节点,若 l < r, 则取 mid = ⌊l+r/2⌋,该节点的左儿子为 [l, mid] ...

  4. CRM 建设方案(01):CRM基础

    CRM 客户关系管理系统基础 客户关系管理简称CRM(Customer Relationship Management).CRM概念引入中国已有数年,其字面意思是客户关系管理,但其深层的内涵却有着许多 ...

  5. mysql索引长度的一些限制

    一.myisam存储引擎 1. 数据库版本:阿里云RDS MySQL5.1 mysql> select @@version;+-------------------------------+| ...

  6. 【刷题】LOJ 6038 「雅礼集训 2017 Day5」远行

    题目描述 Miranda 生活的城市有 \(N\) 个小镇,一开始小镇间没有任何道路连接.随着经济发现,小镇之间陆续建起了一些双向的道路但是由于经济不太发达,在建设过程中,会保证对于任意两个小镇,最多 ...

  7. python之快速排序

    快速排序(Quicksort)是对冒泡排序的一种改进. 快速排序由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另 ...

  8. 洛谷 P2659 美丽的序列 解题报告

    P2659 美丽的序列 题目背景 GD是一个热衷于寻求美好事物的人,一天他拿到了一个美丽的序列. 题目描述 为了研究这个序列的美丽程度,GD定义了一个序列的"美丽度"和" ...

  9. 【HEOI 2018】林克卡特树

    转载请注明出处:http://www.cnblogs.com/TSHugh/p/8776179.html 先说60分的.思路题解上很清晰: 问题似乎等价于选K+1条点不相交的链哎!F(x,k,0/1/ ...

  10. cmakelist 定义字符串,替换到脚本中。

    cmake_minimum_required(VERSION 2.6 FATAL_ERROR) cmake_policy(VERSION 2.6) # . Project Name project(s ...