{"id":515,"date":"2017-10-01T23:18:42","date_gmt":"2017-10-02T06:18:42","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=515"},"modified":"2018-04-19T08:39:41","modified_gmt":"2018-04-19T15:39:41","slug":"688-knight-probability-in-chessboard","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/688-knight-probability-in-chessboard\/","title":{"rendered":"\u82b1\u82b1\u9171 688. Knight Probability in Chessboard"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 688. Knight Probability in Chessboard - \u5237\u9898\u627e\u5de5\u4f5c EP79\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/MyJvMydR2G4?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<p><a href=\"https:\/\/leetcode.com\/problems\/knight-probability-in-chessboard\/description\/\">https:\/\/leetcode.com\/problems\/knight-probability-in-chessboard\/description\/<\/a><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<div class=\"question-description\">\n<p>On an\u00a0<code>N<\/code>x<code>N<\/code>\u00a0chessboard, a knight starts at the\u00a0<code>r<\/code>-th row and\u00a0<code>c<\/code>-th column and attempts to make exactly\u00a0<code>K<\/code>moves. The rows and columns are 0 indexed, so the top-left square is\u00a0<code>(0, 0)<\/code>, and the bottom-right square is\u00a0<code>(N-1, N-1)<\/code>.<\/p>\n<p>A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/leetcode.com\/static\/images\/problemset\/knight.png\" \/>Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.<\/p>\n<p>The knight continues moving until it has made exactly\u00a0<code>K<\/code>\u00a0moves or has moved off the chessboard. Return the probability that the knight remains on the board after it has stopped moving.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"\">Input: 3, 2, 0, 0\r\nOutput: 0.0625\r\nExplanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.\r\nFrom each of those positions, there are also two moves that will keep the knight on \r\nthe board. The total probability the knight stays on the board is 0.0625.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ul>\n<li><code>N<\/code>\u00a0will be between 1 and 25.<\/li>\n<li><code>K<\/code>\u00a0will be between 0 and 100.<\/li>\n<li>The knight always initially starts on the board.<\/li>\n<\/ul>\n<\/div>\n<div><\/div>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<div id=\"interviewed-div\"><strong>Idea:<\/strong><\/div>\n<div>Dynamic programming<\/div>\n<div>Count the ways to reach (x, y) after k moves from start.<\/div>\n<div><\/div>\n<div><\/div>\n<div><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/688-ep79.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-522\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/688-ep79.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/688-ep79.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/688-ep79-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/688-ep79-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/688-ep79-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><strong>Time Complexity: <\/strong>O(k*n^2)<\/div>\n<div><\/div>\n<div><strong>Space Complexity: <\/strong>O(n^2)<\/div>\n<div><\/div>\n<div><strong>Solution:<\/strong><\/div>\n<div>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 6 ms\r\nclass Solution {\r\npublic:\r\n    double knightProbability(int N, int K, int r, int c) {\r\n        vector&lt;vector&lt;double&gt;&gt; dp0(N, vector&lt;double&gt;(N, 0.0));\r\n        dp0[r][c] = 1.0;\r\n        int dirs[8][2] = {{1, 2}, {-1, -2}, {1, -2}, {-1, 2},\r\n                          {2, 1}, {-2, -1}, {2, -1}, {-2, 1}};\r\n        for (int k = 0; k &lt; K; ++k) {            \r\n            vector&lt;vector&lt;double&gt;&gt; dp1(N, vector&lt;double&gt;(N, 0.0));\r\n            for (int i = 0; i &lt; N; ++i)\r\n                for (int j = 0; j &lt; N; ++j) \r\n                    for (int m = 0; m &lt; 8; ++m) {\r\n                        int x = j + dirs[m][0];\r\n                        int y = i + dirs[m][1];\r\n                        if (x &lt; 0 || y &lt; 0 || x &gt;= N || y &gt;= N) continue;\r\n                        dp1[i][j] += dp0[y][x];\r\n                    }\r\n            std::swap(dp0, dp1);\r\n        }\r\n        \r\n        double total = 0;\r\n        for (int i = 0; i &lt; N; ++i)\r\n            for (int j = 0; j &lt; N; ++j)\r\n                total += dp0[i][j];\r\n        \r\n        return total \/ pow(8, K);\r\n    }\r\n};<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>Related problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-62-unique-paths\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 62. Unique Paths<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-63-unique-paths-ii\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 63. Unique Paths II<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode.com\/problems\/knight-probability-in-chessboard\/description\/ Problem: On an\u00a0NxN\u00a0chessboard, a knight starts at the\u00a0r-th row and\u00a0c-th column and attempts to make exactly\u00a0Kmoves. The rows and columns are 0 indexed, so&#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":[119,18,118,120],"class_list":["post-515","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-chess","tag-dp","tag-knight","tag-probability","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/515","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=515"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/515\/revisions"}],"predecessor-version":[{"id":2685,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/515\/revisions\/2685"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}