The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN
.
-2 (K) | -3 | 3 |
-5 | -10 | 1 |
10 | 30 | -5 (P) |
Notes:
这题算是稍微复杂一点的动态规划问题
class Solution { public: int calculateMinimumHP(vector<vector<int>>& dungeon) { if (dungeon.empty() || dungeon[0].empty()) return 0; vector<int>leftHP(dungeon.size()), leftCur(dungeon.size()), upHP(dungeon[0].size()), upCur(dungeon[0].size()); leftCur[0] = 1, leftHP[0] = 1, upHP[0] = 1, upCur[0] = 1; for (int i = 0; i < dungeon.size(); i++) { leftCur[i] = leftCur[i] + dungeon[i][0]; if (leftCur[i] <= 0) { leftHP[i] += 1 - leftCur[i]; leftCur[i] = 1; } if (i + 1 < dungeon.size()) { leftHP[i + 1] = leftHP[i]; leftCur[i + 1] = leftCur[i]; } } for (int i = 0; i < dungeon[0].size(); i++) { upCur[i] = upCur[i] + dungeon[0][i]; if (upCur[i] <= 0) { upHP[i] += 1 - upCur[i]; upCur[i] = 1; } if (i + 1 < dungeon[0].size()) { upHP[i + 1] = upHP[i]; upCur[i + 1] = upCur[i]; } } if (dungeon.size() == 1) return upHP.back(); if (dungeon[0].size() == 1) return leftHP.back(); vector<vector<pair<int, int>>>info(dungeon[0].size());//pair<int,int>(curhp,neededhp) for (int i = 0; i < dungeon[0].size(); i++) info[i].push_back(pair<int, int>(upCur[i], upHP[i])); for (int i = 1; i < dungeon.size(); i++) { vector<pair<int, int>>aa; aa.push_back(pair<int, int>(leftCur[i], leftHP[i])); info[0] = aa; for (int j = 1; j < dungeon[0].size(); j++) { vector<pair<int, int>>candi; for (int k = 0; k < info[j - 1].size(); k++) { int cur = info[j - 1][k].first + dungeon[i][j]; int curhp = info[j - 1][k].second; if (cur <= 0) { curhp += 1 - cur; cur = 1; } candi.push_back(pair<int, int>(cur, curhp)); } for (int k = 0; k < info[j].size(); k++) { int cur = info[j][k].first + dungeon[i][j]; int curhp = info[j][k].second; if (cur <= 0) { curhp += 1 - cur; cur = 1; } candi.push_back(pair<int, int>(cur, curhp)); } //pair<int, int>ch = candi.front(); bool flag = true; while (candi.size()>1 && flag) { flag = false; for (int ii = 0; (ii < candi.size() - 1) && !flag; ii++) { for (int jj = ii + 1; (jj < candi.size()) && !flag; jj++) { if (candi[ii].first >= candi[jj].first&&candi[ii].second <= candi[jj].second) { flag = true; candi.erase(candi.begin() + jj, candi.begin() + jj + 1); } else if (candi[ii].first <= candi[jj].first&&candi[ii].second >= candi[jj].second) { flag = true; candi.erase(candi.begin() + ii, candi.begin() + ii + 1); } } } } info[j] = candi; } } int minret = info.back()[0].second; for (int i = 1; i < info.back().size(); i++) if (info.back()[i].second < minret) minret = info.back()[i].second; for (int i = 0; i < dungeon[0].size() - 1; i++) { vector<vector<pair<int, int>>>tempinfo(dungeon[0].size()); tempinfo[i] = info[i]; bool f = true; for (int j = i; (j < dungeon[0].size() - 1) && f; j++) { f = false; vector<pair<int, int>>candi; for (int k = 0; k < tempinfo[j].size(); k++) { if (tempinfo[j][k].second < minret) { f = true; int cur = tempinfo[j][k].first + dungeon[dungeon.size() - 1][j + 1]; int hp = tempinfo[j][k].second; if (cur <= 0) { hp += 1 - cur; cur = 1; } if (hp < minret) candi.push_back(pair<int, int>(cur, hp)); } } bool flag = true; while (candi.size() > 1 && flag) { flag = false; for (int ii = 0; (ii < candi.size() - 1) && !flag; ii++) { for (int jj = ii + 1; (jj < candi.size()) && !flag; jj++) { if (candi[ii].first >= candi[jj].first&&candi[ii].second <= candi[jj].second) { flag = true; candi.erase(candi.begin() + jj, candi.begin() + jj + 1); } else if (candi[ii].first <= candi[jj].first&&candi[ii].second >= candi[jj].second) { flag = true; candi.erase(candi.begin() + ii, candi.begin() + ii + 1); } } } } tempinfo[j + 1] = candi; } if (!tempinfo.empty()) { for (int i = 0; i < tempinfo.back().size(); i++) if (tempinfo.back()[i].second < minret) minret = tempinfo.back()[i].second; } } return minret; } };