{"id":559,"date":"2020-09-26T10:57:43","date_gmt":"2020-09-26T02:57:43","guid":{"rendered":"http:\/\/39.96.58.60\/?p=559"},"modified":"2022-10-18T16:38:54","modified_gmt":"2022-10-18T08:38:54","slug":"leetcode-51-n-%e7%9a%87%e5%90%8e","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=559","title":{"rendered":"Leetcode 51. N \u7687\u540e"},"content":{"rendered":"<p><em>n&nbsp;<\/em>\u7687\u540e\u95ee\u9898\u7814\u7a76\u7684\u662f\u5982\u4f55\u5c06 <em>n<\/em>&nbsp;\u4e2a\u7687\u540e\u653e\u7f6e\u5728 <em>n<\/em>\u00d7<em>n<\/em> \u7684\u68cb\u76d8\u4e0a\uff0c\u5e76\u4e14\u4f7f\u7687\u540e\u5f7c\u6b64\u4e4b\u95f4\u4e0d\u80fd\u76f8\u4e92\u653b\u51fb\u3002<\/p>\n<p><img src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/10\/12\/8-queens.png\"><\/p>\n<p><small>\u4e0a\u56fe\u4e3a 8 \u7687\u540e\u95ee\u9898\u7684\u4e00\u79cd\u89e3\u6cd5\u3002<\/small><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570 <em>n<\/em>\uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u540c\u7684&nbsp;<em>n&nbsp;<\/em>\u7687\u540e\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u6bcf\u4e00\u79cd\u89e3\u6cd5\u5305\u542b\u4e00\u4e2a\u660e\u786e\u7684&nbsp;<em>n<\/em> \u7687\u540e\u95ee\u9898\u7684\u68cb\u5b50\u653e\u7f6e\u65b9\u6848\uff0c\u8be5\u65b9\u6848\u4e2d <code>'Q'<\/code> \u548c <code>'.'<\/code> \u5206\u522b\u4ee3\u8868\u4e86\u7687\u540e\u548c\u7a7a\u4f4d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>4\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[\n\n [\".Q..\",  \/\/ \u89e3\u6cd5 1\n\n  \"...Q\",\n\n  \"Q...\",\n\n  \"..Q.\"],\n\n\n\n [\"..Q.\",  \/\/ \u89e3\u6cd5 2\n\n  \"Q...\",\n\n  \"...Q\",\n\n  \".Q..\"]\n\n]\n\n<strong>\u89e3\u91ca:<\/strong> 4 \u7687\u540e\u95ee\u9898\u5b58\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u89e3\u6cd5\u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u7687\u540e\u5f7c\u6b64\u4e0d\u80fd\u76f8\u4e92\u653b\u51fb\uff0c\u4e5f\u5c31\u662f\u8bf4\uff1a\u4efb\u4f55\u4e24\u4e2a\u7687\u540e\u90fd\u4e0d\u80fd\u5904\u4e8e\u540c\u4e00\u6761\u6a2a\u884c\u3001\u7eb5\u884c\u6216\u659c\u7ebf\u4e0a\u3002<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Hard<\/p>\n<p>**\u6807\u7b7e**: \u56de\u6eaf\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a76 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8657.29% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.05% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def solveNQueens(self, n: int) -&gt; List[List[str]]:\n        row = []    # \u7528\u4e8e\u4fdd\u5b58 \u4e0d\u80fd\u653e\u7f6e\u7684\u884c\uff0c\u4ee3\u8868\u6b64\u884c\u5df2\u6709Q\uff0c\u5df2\u88ab\u4f7f\u7528\n        col = []    # \u4e0d\u80fd\u653e\u7f6e\u7684\u5217\n        obl1 = []   # \u4e0d\u80fd\u653e\u7f6e\u7684\u659c\u884c1\n        obl2 = []   # \u4e0d\u80fd\u653e\u7f6e\u7684\u659c\u884c2\n        result = [] # \u6700\u7ec8\u7ed3\u679c\uff0c\u4fdd\u5b58\u68cb\u5b50\u7684\u5b58\u653e\u5904\n        def solve(r, current):  # \u5f53\u524d\u884cr, \u5f53\u524d\u5df2\u7ecf\u653e\u7f6e\u7684\u68cb\u5b50\n            if r == n:          # \u5982\u679c\u5df2\u7ecf\u5230\u6700\u540e\u4e00\u884c\uff0c\u5219\u6dfb\u52a0\u5230\u6700\u7ec8\u7ed3\u679c\u4e2d\n                result.append(current.copy())\n            for j in range(n):  # \u904d\u5386\u5217\n                if r not in row and j not in col and r-j not in obl1 and r+j not in obl2:   # \u653e\u7f6e\u4f4d\u7f6e\u9a8c\u8bc1\n                    row.append(r)   # \u653e\u7f6e\u68cb\u5b50\u540e\uff0c\u5c06\u5f53\u524d\u884c\u6dfb\u52a0\u5230\u5df2\u4f7f\u7528\u5217\u8868\u4e2d\n                    col.append(j)\n                    obl1.append(r-j)\n                    obl2.append(r+j)\n                    current.append((r,j))   # \u6dfb\u52a0\u5230\u5f53\u524d\u653e\u7f6e\u68cb\u5b50\u5217\u8868\u4e2d\n                    solve(r+1, current)     # \u5f00\u59cb\u4e0b\u4e00\u884c\n\n                    row.pop()   # \u56de\u6eaf\n                    col.pop()\n                    obl1.pop()\n                    obl2.pop()\n                    current.pop()\n\n        solve(0, [])    # \u4ece\u7b2c\u4e00\u884c\u5f00\u59cb\n\n        # \u5c06\u68cb\u5b50\u5750\u6807\u8f6c\u6362\u5230\u68cb\u76d8\n        board = [[['.' for _ in range(n)] for _ in range(n)] for _ in range(len(result))]\n        for n, rc in enumerate(result):\n            for r, c in rc:\n                board[n][r][c] = 'Q'\n                board[n][r] = ''.join(board[n][r])\n\n        return board<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>n&nbsp;\u7687\u540e\u95ee\u9898\u7814\u7a76\u7684\u662f\u5982\u4f55\u5c06 n&nbsp;\u4e2a\u7687\u540e\u653e\u7f6e\u5728 n\u00d7n \u7684\u68cb\u76d8\u4e0a\uff0c\u5e76\u4e14\u4f7f\u7687\u540e\u5f7c\u6b64\u4e4b\u95f4\u4e0d\u80fd\u76f8\u4e92&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/559"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=559"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/559\/revisions"}],"predecessor-version":[{"id":560,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/559\/revisions\/560"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=559"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}