{"id":160,"date":"2017-09-08T20:47:02","date_gmt":"2017-09-09T03:47:02","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=160"},"modified":"2018-04-19T08:35:36","modified_gmt":"2018-04-19T15:35:36","slug":"leetcode-51-n-queens","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-51-n-queens\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 51. N-Queens"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"LeetCode 51. N-Queens - \u82b1\u82b1\u9171 \u5237\u9898\u627e\u5de5\u4f5c EP41\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/Xa-yETqFNEQ?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><strong>Problem:<\/strong><\/p>\n<div class=\"question-description\">\n<p>The\u00a0<i>n<\/i>-queens puzzle is the problem of placing\u00a0<i>n<\/i>\u00a0queens on an\u00a0<i>n<\/i>\u00d7<i>n<\/i>\u00a0chessboard such that no two queens attack each other.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/leetcode.com\/static\/images\/problemset\/8-queens.png\" \/><\/p>\n<p>Given an integer\u00a0<i>n<\/i>, return all distinct solutions to the\u00a0<i>n<\/i>-queens puzzle.<\/p>\n<p>Each solution contains a distinct board configuration of the\u00a0<i>n<\/i>-queens&#8217; placement, where\u00a0<code>'Q'<\/code>\u00a0and\u00a0<code>'.'<\/code>\u00a0both indicate a queen and an empty space respectively.<\/p>\n<p>For example,<br \/>\nThere exist two distinct solutions to the 4-queens puzzle:<\/p>\n<pre class=\"\">[\r\n [\".Q..\",  \/\/ Solution 1\r\n  \"...Q\",\r\n  \"Q...\",\r\n  \"..Q.\"],\r\n\r\n [\"..Q.\",  \/\/ Solution 2\r\n  \"Q...\",\r\n  \"...Q\",\r\n  \".Q..\"]\r\n]\r\n<\/pre>\n<\/div>\n<div><\/div>\n<div id=\"interviewed-div\"><strong>Idea:<\/strong><\/div>\n<div><\/div>\n<div>Search<\/div>\n<div><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-164\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/div>\n<div><\/div>\n<div><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-163\" style=\"font-size: 1rem;\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/51-ep41-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/div>\n<div><\/div>\n<div><strong>Solution:<\/strong><\/div>\n<div>Changes: 12\/20\/2017<\/div>\n<div>type of sols_ changed from set&lt;vector&lt;string&gt;&gt; to vector&lt;vector&lt;string&gt;&gt;. Thanks to\u00a0Yun-Ting Lin.<\/div>\n<div>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 3 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;vector&lt;string&gt;&gt; solveNQueens(int n) {\r\n        sols_.clear();\r\n        board_ = vector&lt;string&gt;(n, string(n, '.'));\r\n        \r\n        cols_ = vector&lt;int&gt;(n, 0);\r\n        diag1_ = vector&lt;int&gt;(2 * n - 1, 0);\r\n        diag2_ = vector&lt;int&gt;(2 * n - 1, 0);\r\n        \r\n        nqueens(n, 0);\r\n        \r\n        return sols_;\r\n    }\r\nprivate:\r\n    vector&lt;string&gt; board_;\r\n    vector&lt;int&gt; cols_;\r\n    vector&lt;int&gt; diag1_;\r\n    vector&lt;int&gt; diag2_;\r\n    vector&lt;vector&lt;string&gt;&gt; sols_;\r\n    \r\n    bool available(int x, int y, int n) {\r\n        return !cols_[x] \r\n            &amp;&amp; !diag1_[x + y]\r\n            &amp;&amp; !diag2_[x - y + n - 1];\r\n    }\r\n    \r\n    void updateBoard(int x, int y, int n, bool is_put) {\r\n        cols_[x] = is_put;\r\n        diag1_[x + y] = is_put;\r\n        diag2_[x - y + n - 1] = is_put;\r\n        board_[y][x] = is_put ? 'Q' : '.';\r\n    }\r\n    \r\n    \/\/ Try to put the queen on y-th row\r\n    void nqueens(const int n, const int y) {        \r\n        if (y == n) {\r\n            \/\/ found one solution, add to the ans set\r\n            sols_.push_back(board_);\r\n            return;\r\n        }\r\n        \r\n        \/\/ Try every column\r\n        for (int x = 0; x &lt; n; ++x) {\r\n            if (!available(x, y, n)) continue;\r\n            updateBoard(x, y, n, true);\r\n            nqueens(n, y + 1);\r\n            updateBoard(x, y, n, false);\r\n        }\r\n    }    \r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<div><\/div>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem: The\u00a0n-queens puzzle is the problem of placing\u00a0n\u00a0queens on an\u00a0n\u00d7n\u00a0chessboard such that no two queens attack each other. Given an integer\u00a0n, return all distinct solutions&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-searching","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/160","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=160"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":2624,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/160\/revisions\/2624"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}