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?

最简单的回溯:

 int backtrack(int r, int c, int m, int n) {
if (r == m && c == n)
return ;
if (r > m || c > n)
return ; return backtrack(r+, c, m, n) + backtrack(r, c+, m, n);
}

用一个表记录状态,优化:

 const int M_MAX = ;
const int N_MAX = ; int backtrack(int r, int c, int m, int n, int mat[][N_MAX+]) {
if (r == m && c == n)
return ;
if (r > m || c > n)
return ; if (mat[r+][c] == -)
mat[r+][c] = backtrack(r+, c, m, n, mat);
if (mat[r][c+] == -)
mat[r][c+] = backtrack(r, c+, m, n, mat); return mat[r+][c] + mat[r][c+];
} int bt(int m, int n) {
int mat[M_MAX+][N_MAX+];
for (int i = ; i < M_MAX+; i++) {
for (int j = ; j < N_MAX+; j++) {
mat[i][j] = -;
}
}
return backtrack(, , m, n, mat);
}

动态规划,从最靠近终点的地方(子问题)开始算:

 const int M_MAX = ;
const int N_MAX = ; int dp(int m, int n) {
int mat[M_MAX+][N_MAX+] = {};
mat[m][n+] = ; for (int r = m; r >= ; r--)
for (int c = n; c >= ; c--)
mat[r][c] = mat[r+][c] + mat[r][c+]; return mat[][];
}

这里的空间可以进一步优化,因为最底行计算完之后可以直接用来计算上一行。

 class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> map(m+, );
map[]=;
for(int i=; i<n; i++){
for(int j=; j<=m; j++)
map[j] = map[j-]+map[j];
}
return map[m];
}
}

另外,这道题其实也是一个组合数学的题,结果就是C(m + n - 2, n - 1)。这里要注意计算是overflow。

 int gcd(int a, int b) {
while(b) {
int c = a%b;
a = b;
b = c;
}
return a;
} int C(int m, int n) {
if(m - n < n) {
n = m - n;
}
int result = ;
for(int i = ; i <= n; i++) {
int mult = m;
int divi = i;
int g = gcd(mult,divi);
mult /= g;
divi /= g;
result /= divi;
result *= mult;
m--;
}
return result;
}

这道题真是相当经典。

LeetCode | Unique Paths【摘】的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  2. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. [LeetCode] Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. LeetCode: Unique Paths I & II & Minimum Path Sum

    Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m  ...

  5. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  6. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  7. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  8. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

随机推荐

  1. jquery.validate.unobtrusive.js插件作用

    在 ASP.NET MVC 中启用 Unobtrusive JavaScript 功能,可以在运行时由服务器端根据Model中设置的验证规则,自动生成客户端验证js代码(结合jquery.valida ...

  2. hibernate的sqlQuery自动封装

    1.Query query = session.createSQLQuery("SQL").addEntity(Tree.class); //返回对象   List  list = ...

  3. hdu 1005:Number Sequence(水题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. C# 从入门到精通

    int i=null; int? i=null; 我们用一个问号 来指定这个值类型是可空的 属性是只读的

  5. PHP生成token防止表单重复提交

    .提交按钮置disabled 当用户提交后,立即把按钮置为不可用状态.这种用js来实现. 提交前代码如下: $()  {  $exec="insert into student (user_ ...

  6. laravel 安装及入门

    Composer安装过程 一.安装Composer 首先你需要安装Composer,Composer是PHP依赖管理工具,Laravel框架就是使用 Composer 执行安装和依赖管理.  注: ( ...

  7. 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数

    [bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...

  8. loj 1185(bfs)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26898 思路:我们可以给定有直接边相连的两点的距离为1,那么就是求 ...

  9. Marvelous Mazes

    F - Marvelous Mazes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  10. SU suamp命令学习