{"id":2933,"date":"2020-02-13T16:16:02","date_gmt":"2020-02-13T08:16:02","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2933"},"modified":"2020-02-13T16:16:02","modified_gmt":"2020-02-13T08:16:02","slug":"%e3%80%90leetcode%e3%80%9100510052-n%e7%9a%87%e5%90%8e","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2933\/","title":{"rendered":"\u3010LeetCode\u30110051&amp;0052 N\u7687\u540e"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/n-queens\/\">https:\/\/leetcode-cn.com\/problems\/n-queens\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def solveNQueens(self, n):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> n: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: List[List[str]]\n        \"\"\"\n        <\/em>def check(i, j, temp_dir):\n            for x in range(n):\n                # \u68c0\u67e5\u6a2a\u7ad6\n                if temp_dir[x][j] is 'Q' or temp_dir[i][x] is 'Q':\n                    return False\n                # \u68c0\u67e5\u53f3\u4e0a\u659c\u7ebf\n                if i + x &lt; n and j - x >= 0:\n                    if temp_dir[i + x][j - x] is 'Q':\n                        return False\n                # \u68c0\u67e5\u5de6\u4e0b\u659c\u7ebf\n                if i - x >= 0 and j + x &lt; n:\n                    if temp_dir[i - x][j + x] is 'Q':\n                        return False\n                # \u68c0\u67e5\u5de6\u4e0a\u659c\u7ebf\n                if i - x >= 0 and j - x >= 0:\n                    if temp_dir[i - x][j - x] is 'Q':\n                        return False\n                # \u68c0\u67e5\u53f3\u4e0b\u659c\u7ebf\n                if i + x &lt; n and j + x &lt; n:\n                    if temp_dir[i + x][j + x] is 'Q':\n                        return False\n            return True\n        def back(x, from_x_idx, temp_dir):\n            if x == n:\n                import copy\n                res.append(copy.deepcopy(temp_dir))\n                return\n            if from_x_idx > n - 1:\n                return\n            for i in range(from_x_idx, n):\n                for j in range(n):\n                    if temp_dir[i][j] is not 'Q' and check(i, j, temp_dir):\n                        temp_dir[i][j] = 'Q'\n                        back(x + 1, i + 1, temp_dir[:])\n                        temp_dir[i][j] = '.'\n        res = []\n        init_map = [['.' for _ in range(n)] for _ in range(n)]\n        back(0, 0, init_map)\n        res_final = []\n        for i in range(len(res)):\n            temp_res = []\n            for j in range(n):\n                temp_res.append(\"\".join(res[i][j]))\n            res_final.append(temp_res)\n        return res_final\nif __name__ == '__main__':\n    n = 8\n    print(Solution().solveNQueens(n))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u56de\u6eaf\u6cd5\uff0c\u5224\u65ad\u5f53\u524d\u4f4d\u7f6e\u662f\u5426\u53ef\u4ee5\u6446\u653e\u7136\u540e\u4e00\u6b65\u4e00\u6b65\u56de\u6eaf\u5373\u53ef\u3002<\/p>\n\n\n<p>\u503c\u5f97\u63d0\u51fa\u7684\u662f\uff0c\u7531\u4e8e\u5bf9\u4e8e\u8f83\u5927\u7684n\u8fd0\u884c\u65f6\u95f4\u8f83\u957f\uff0c\u56e0\u6b64\u8981\u8fdb\u884c\u72b6\u6001\u538b\u7f29\u4e5f\u5c31\u662f\u526a\u679d\uff0c\u6bd4\u5982\u5f53\u524d\u4f4d\u7f6e\u7684\u6a2a\u7eb5\u659c\u4e0d\u9700\u8981\u5224\u65ad\uff0c\u6bd4\u5f53\u524d\u4f4d\u7f6e\u7684x\u5750\u6807\u5c0f\u7684\u4e0d\u9700\u8981\u5224\u65ad\u7b49\u3002\u53ef\u4ee5\u518d\u505a\u4e00\u4e2a\u53ef\u8bbf\u95ee\u5730\u56fe\u526a\u679d\u5373\u53ef\u3002<\/p>\n\n\n<p><\/p>\n\n\n<p>0052 \u6c42\u4e2a\u957f\u5ea6\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/n-queens\/ # -*- c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":2233,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2933"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2933"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2933\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2933"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}