首页 > 其他 > 详细

LeetCode "Unique Paths"

时间:2014-07-22 22:40:24      阅读:303      评论:0      收藏:0      [点我收藏+]

Now I believe thoughts leading to DP is brutal DFS.. DFS is brutal force enumeration, but that‘s too much and naive. We need pruning. So, DFS + Pruning leads to DP! Equation is simple in this case. 1A!

class Solution {
public:
    #define MAXN 101
    int uniquePaths(int m, int n) {
        //    Initial thought was DFS, which is brutal force.
        //    So try pruning.. DFS + Pruning means DP
        int A[MAXN][MAXN];
        for (int i = 0; i <= m; i++) A[i][0] = 0;
        for (int i = 0; i <= n; i++) A[0][i] = 0;
        A[1][1] = 1;
        for (int i = 1; i <= m; i ++)
        for (int j = 1; j <= n; j ++)
        {
            if (i == 1 && j == 1) continue;
            A[i][j] = A[i - 1][j] + A[i][j - 1];
        }
        return A[m][n];
    }
};

LeetCode "Unique Paths",布布扣,bubuko.com

LeetCode "Unique Paths"

原文:http://www.cnblogs.com/tonix/p/3860251.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!