码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Unique Paths"

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

标签:style   blog   color   io   for   re   

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"

标签:style   blog   color   io   for   re   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!