Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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];
    }
};
posted on 2014-07-22 12:12  Tonix  阅读(161)  评论(0编辑  收藏  举报