{"id":3543,"date":"2018-08-16T08:45:07","date_gmt":"2018-08-16T15:45:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3543"},"modified":"2018-08-16T08:45:47","modified_gmt":"2018-08-16T15:45:47","slug":"leetcode-174-dungeon-game","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-174-dungeon-game\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 174. Dungeon Game"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 174. Dungeon Game - \u5237\u9898\u627e\u5de5\u4f5c EP6\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/pt-xIS6huIg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<div class=\"question-description__3U1T\">\n<p>The demons had captured the princess (<strong>P<\/strong>) 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 (<strong>K<\/strong>) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.<\/p>\n<p>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.<\/p>\n<p>Some of the rooms are guarded by demons, so the knight loses health (<em>negative<\/em>\u00a0integers) upon entering these rooms; other rooms are either empty (<em>0&#8217;s<\/em>) or contain magic orbs that increase the knight&#8217;s health (<em>positive<\/em>\u00a0integers).<\/p>\n<p>In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.<\/p>\n<p><strong>Write a function to determine the knight&#8217;s minimum initial health so that he is able to rescue the princess.<\/strong><\/p>\n<p>For example, given the dungeon below, the initial health of the knight must be at least\u00a0<strong>7<\/strong>\u00a0if he follows the optimal path\u00a0<code>RIGHT-&gt; RIGHT -&gt; DOWN -&gt; DOWN<\/code>.<\/p>\n<table class=\"dungeon\">\n<tbody>\n<tr>\n<td>-2 (K)<\/td>\n<td>-3<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>-5<\/td>\n<td>-10<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>30<\/td>\n<td>-5 (P)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>The knight&#8217;s health has no upper bound.<\/li>\n<li>Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.<\/li>\n<\/ul>\n<h1><strong>Solution: DP<\/strong><\/h1>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(mn) -&gt; O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int calculateMinimumHP(vector&lt;vector&lt;int&gt;&gt;&amp; dungeon) {      \r\n    const int m = dungeon.size();\r\n    const int n = dungeon[0].size();\r\n\r\n    \/\/ hp[y][x]: min hp required to reach bottom right (P).\r\n    vector&lt;vector&lt;int&gt;&gt; hp(m + 1, vector&lt;int&gt;(n + 1, INT_MAX));\r\n    hp[m][n - 1] = hp[m - 1][n] = 1;\r\n\r\n    for (int y = m - 1; y &gt;= 0; --y)\r\n      for (int x = n - 1; x &gt;= 0; --x)\r\n        hp[y][x] = max(1, min(hp[y + 1][x], hp[y][x + 1]) - dungeon[y][x]);\r\n\r\n    return hp[0][0];\r\n  }\r\n};<\/pre>\n<p>O(n) space<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int calculateMinimumHP(vector&lt;vector&lt;int&gt;&gt;&amp; dungeon) {      \r\n    const int m = dungeon.size();\r\n    const int n = dungeon[0].size();\r\n\r\n    \/\/ hp[y][x]: min hp required to reach bottom right (P).\r\n    vector&lt;int&gt; hp(n + 1, INT_MAX);\r\n    hp[n - 1] = 1;\r\n\r\n    for (int y = m - 1; y &gt;= 0; --y)\r\n      for (int x = n - 1; x &gt;= 0; --x)\r\n        hp[x] = max(1, min(hp[x], hp[x + 1]) - dungeon[y][x]);\r\n\r\n    return hp[0];\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem 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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,278,217,366],"class_list":["post-3543","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-grid","tag-hard","tag-omn","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3543"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3543\/revisions"}],"predecessor-version":[{"id":3546,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3543\/revisions\/3546"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}