{"id":5691,"date":"2019-10-02T08:53:05","date_gmt":"2019-10-02T15:53:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5691"},"modified":"2019-10-02T08:53:59","modified_gmt":"2019-10-02T15:53:59","slug":"leetcode-52-n-queens-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-52-n-queens-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 52. N-Queens II"},"content":{"rendered":"\n<p>The&nbsp;<em>n<\/em>-queens puzzle is the problem of placing&nbsp;<em>n<\/em>&nbsp;queens on an&nbsp;<em>n<\/em>\u00d7<em>n<\/em>&nbsp;chessboard such that no two queens attack each other.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/10\/12\/8-queens.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Given an integer&nbsp;<em>n<\/em>, return the number of&nbsp;distinct solutions to the&nbsp;<em>n<\/em>-queens puzzle.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 4\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are two distinct solutions to the 4-queens puzzle as shown below.\n[\n&nbsp;[\".Q..\", &nbsp;\/\/ Solution 1\n&nbsp; \"...Q\",\n&nbsp; \"Q...\",\n&nbsp; \"..Q.\"],\n\n&nbsp;[\"..Q.\", &nbsp;\/\/ Solution 2\n&nbsp; \"Q...\",\n&nbsp; \"...Q\",\n&nbsp; \".Q..\"]\n]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n!)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int totalNQueens(int n) {\n    vector<int> cols(n);\n    vector<int> diag1(2 * n - 1);\n    vector<int> diag2(2 * n - 1);\n    int ans = 0;\n    \n    function<void(int)> dfs = [&](int r) {\n      if (r == n) {\n        ++ans;\n        return;\n      }\n\n      for (int i = 0; i < n; i++) {\n        int&#038; c = cols[i];\n        int&#038; d1 = diag1[r + i];\n        int&#038; d2 = diag2[r - i + n - 1];\n        if (c || d1 || d2) continue;\n        c = d1 = d2 = 1;\n        dfs(r + 1);\n        c = d1 = d2 = 0;\n      }\n    };\n    \n    dfs(0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-51-n-queens\/\">https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-51-n-queens\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-36-valid-sudoku\/\">https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-36-valid-sudoku\/<\/a><\/li><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-37-sudoku-solver\/\">https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-37-sudoku-solver\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;n-queens puzzle is the problem of placing&nbsp;n&nbsp;queens on an&nbsp;n\u00d7n&nbsp;chessboard such that no two queens attack each other. Given an integer&nbsp;n, return the number of&nbsp;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":[33,217,376],"class_list":["post-5691","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-hard","tag-on","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5691","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=5691"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5691\/revisions"}],"predecessor-version":[{"id":5693,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5691\/revisions\/5693"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}