{"id":982,"date":"2025-12-16T05:27:59","date_gmt":"2025-12-15T21:27:59","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=982"},"modified":"2025-12-16T05:27:59","modified_gmt":"2025-12-15T21:27:59","slug":"leetcode-51-n-%e7%9a%87%e5%90%8e","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=982","title":{"rendered":"LeetCode 51 &#8211; N \u7687\u540e"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u6309\u7167\u56fd\u9645\u8c61\u68cb\u7684\u89c4\u5219\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u51fb\u4e0e\u4e4b\u5904\u5728\u540c\u4e00\u884c\u6216\u540c\u4e00\u5217\u6216\u540c\u4e00\u659c\u7ebf\u4e0a\u7684\u68cb\u5b50\u3002<br \/>\nn \u7687\u540e\u95ee\u9898 \u7814\u7a76\u7684\u662f\u5982\u4f55\u5c06 n \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\u653b\u51fb\u3002<br \/>\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570 n \uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u540c\u7684 n \u7687\u540e\u95ee\u9898 \u7684\u89e3\u51b3\u65b9\u6848\u3002<br \/>\n\u6bcf\u4e00\u79cd\u89e3\u6cd5\u5305\u542b\u4e00\u4e2a\u4e0d\u540c\u7684 n \u7687\u540e\u95ee\u9898 \u7684\u68cb\u5b50\u653e\u7f6e\u65b9\u6848\uff0c\u8be5\u65b9\u6848\u4e2d &#8216;Q&#8217; \u548c &#8216;.&#8217; \u5206\u522b\u4ee3\u8868\u4e86\u7687\u540e\u548c\u7a7a\u4f4d\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1an = 4<br \/>\n\u8f93\u51fa\uff1a[[&#8220;.Q..&#8221;,&#8221;&#8230;Q&#8221;,&#8221;Q&#8230;&#8221;,&#8221;..Q.&#8221;],[&#8220;..Q.&#8221;,&#8221;Q&#8230;&#8221;,&#8221;&#8230;Q&#8221;,&#8221;.Q..&#8221;]]<br \/>\n\u89e3\u91ca\uff1a\u5982\u4e0a\u56fe\u6240\u793a\uff0c4 \u7687\u540e\u95ee\u9898\u5b58\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u89e3\u6cd5\u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>DFS\uff0c\u6309\u6bcf\u4e00\u884c\u8fdb\u884c\u679a\u4e3e.<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">private List&lt;List&lt;String&gt;&gt; ans = new ArrayList&lt;&gt;();\n\npublic List&lt;List&lt;String&gt;&gt; solveNQueens(int n) {\n    dfs(new char[n][n], n, 0);\n    return ans;\n}\n\nprivate void dfs(char[][] board, int n, int row) {\n    if (row == n) {\n        List&lt;String&gt; list = new ArrayList&lt;&gt;();\n        for (int i = 0; i &lt; n; i++) {\n            StringBuilder sb = new StringBuilder();\n            for (int j = 0; j &lt; n; j++) {\n                if (board[i][j] == 0) {\n                    board[i][j] = '.';\n                }\n                sb.append(board[i][j]);\n            }\n            list.add(sb.toString());\n        }\n        ans.add(list);\n        return;\n    }\n\n    \/\/ \u679a\u4e3e\u5217\u53f7\n    for (int col = 0; col &lt; n; col++) {\n        \/\/ \u68c0\u67e5\u524d\u9762\u7684\u884c\n        int pre;\n        for (pre = 0; pre &lt; row; pre++) {\n            \/\/ \u540c\u4e00\u5217\u6709Q\n            if (board[pre][col] == 'Q') {\n                break;\n            }\n            \/\/ \u5bf9\u89d2\u7ebf\u6709Q\n            int distance = row - pre;\n            if ((col - distance &gt;= 0 &amp;&amp; board[pre][col - distance] == 'Q') || (col + distance &lt; n &amp;&amp; board[pre][col + distance] == 'Q')) {\n                break;\n            }\n        }\n        if (pre &lt; row) {\n            continue;\n        }\n\n        \/\/ \u7b26\u5408\u6761\u4ef6\n        board[row][col] = 'Q';\n        dfs(board, n, row + 1);\n        board[row][col] = '.';\n\n    }\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">private val ans = ArrayList&lt;List&lt;String&gt;&gt;()\n\nfun solveNQueens(n: Int): List&lt;List&lt;String&gt;&gt; {\n    dfs(Array(n) { CharArray(n) { '.' } }, n, 0)\n    return ans\n}\n\nprivate fun dfs(board: Array&lt;CharArray&gt;, n: Int, row: Int) {\n    if (row == n) {\n        val list = ArrayList&lt;String&gt;()\n        for (charArray in board) {\n            val sb = StringBuilder()\n            for (ch in charArray) {\n                sb.append(ch)\n            }\n            list.add(sb.toString())\n        }\n        ans.add(list)\n        return\n    }\n\n    for (col in 0 until n) {\n        var valid = true\n        for (pre in 0 until row) {\n            \/\/ \u540c\u4e00\u5217\n            if (board[pre][col] == 'Q') {\n                valid = false\n                break\n            }\n            \/\/ \u5bf9\u89d2\u7ebf\n            val distance = row - pre\n            if ((col - distance &gt;= 0 &amp;&amp; board[pre][col - distance] == 'Q') || (col + distance &lt; n &amp;&amp; board[pre][col + distance] == 'Q')) {\n                valid = false\n                break;\n            }\n        }\n        if (valid) {\n            board[row][col] = 'Q'\n            dfs(board, n, row + 1)\n            board[row][col] = '.'\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u6309\u7167\u56fd\u9645\u8c61\u68cb\u7684\u89c4\u5219\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u51fb\u4e0e\u4e4b\u5904\u5728\u540c\u4e00\u884c\u6216\u540c\u4e00\u5217\u6216\u540c\u4e00\u659c\u7ebf\u4e0a\u7684\u68cb\u5b50\u3002 n \u7687\u540e\u95ee\u9898 \u7814\u7a76\u7684\u662f\u5982 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[11,20,90],"class_list":["post-982","post","type-post","status-publish","format-standard","hentry","category-algo","tag-dfs","tag-leetcode","tag-90"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=982"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/982\/revisions"}],"predecessor-version":[{"id":983,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/982\/revisions\/983"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}