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

[leetcode]Dungeon Game

时间:2015-02-15 12:01:14      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

dp,精华是从右下角开始。

class Solution {
public:
    int calculateMinimumHP(vector<vector<int> > &dungeon) {
        int m = dungeon.size();
        int n = dungeon[0].size();
        vector<vector<int> > dp(m, vector<int>(n));
        dp[m-1][n-1] = max(-dungeon[m-1][n-1], 0);
        for (int i = m - 2; i >= 0; i--) {
            dp[i][n-1] = max(dp[i+1][n-1] - dungeon[i][n-1], 0);
        }
        for (int j = n - 2; j >= 0; j--) {
            dp[m-1][j] = max(dp[m-1][j+1] - dungeon[m-1][j], 0);
        }
        for (int i = m - 2; i >= 0; i--) {
            for (int j = n - 2; j >= 0; j--) {
                int down = max(dp[i+1][j] - dungeon[i][j], 0);
                int right = max(dp[i][j+1] - dungeon[i][j], 0);
                dp[i][j] = min(down, right);   
            }
        }
        return dp[0][0] + 1;
    }
};

  

[leetcode]Dungeon Game

标签:

原文地址:http://www.cnblogs.com/lautsie/p/4292665.html

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