{"id":9071,"date":"2024-05-15T09:01:01","date_gmt":"2024-05-15T01:01:01","guid":{"rendered":""},"modified":"2024-05-15T09:01:01","modified_gmt":"2024-05-15T01:01:01","slug":"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898, \u56de\u6eaf","status":"publish","type":"post","link":"https:\/\/mushiming.com\/9071.html","title":{"rendered":"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898, \u56de\u6eaf"},"content":{"rendered":"

\u516b\u7687\u540e\u7684\u6765\u6e90<\/strong><\/p>\n

\u516b\u7687\u540e\u95ee\u9898<\/strong>\u662f\u4e00\u4e2a\u4ee5\u56fd\u9645\u8c61\u68cb\u4e3a\u80cc\u666f\u7684\u95ee\u9898\uff1a\u5982\u4f55\u80fd\u591f\u57288\u00d78\u7684\u56fd\u9645\u8c61\u68cb\u68cb\u76d8\u4e0a\u653e\u7f6e\u516b\u4e2a\u7687\u540e\uff0c\u4f7f\u5f97\u4efb\u4f55\u4e00\u4e2a\u7687\u540e\u90fd\u65e0\u6cd5\u76f4\u63a5\u5403\u6389\u5176\u4ed6\u7684\u7687\u540e\uff1f\u4e3a\u4e86\u8fbe\u5230\u6b64\u76ee\u7684\uff0c\u4efb\u4e24\u4e2a\u7687\u540e\u90fd\u4e0d\u80fd\u5904\u4e8e\u540c\u4e00\u6761\u6a2a\u884c\u3001\u7eb5\u884c\u6216\u659c\u7ebf\u4e0a\u3002\u516b\u7687\u540e\u95ee\u9898\u53ef\u4ee5\u63a8\u5e7f\u4e3a\u66f4\u4e00\u822c\u7684n\u7687\u540e\u6446\u653e\u95ee\u9898\uff1a\u8fd9\u65f6\u68cb\u76d8\u7684\u5927\u5c0f\u53d8\u4e3an\u00d7n\uff0c\u800c\u7687\u540e\u4e2a\u6570\u4e5f\u53d8\u6210n\u3002\u5f53\u4e14\u4ec5\u5f53n = 1\u6216n \u2265 4\u65f6\u95ee\u9898\u6709\u89e3\u3002<\/p>\n

\u516b\u7687\u540e\u95ee\u9898\u6700\u65e9\u662f\u7531\u56fd\u9645\u8c61\u68cb\u68cb\u624b\u9a6c\u514b\u65af\u00b7\u8d1d\u745f\u5c14\uff08Max Bezzel\uff09\u4e8e1848\u5e74\u63d0\u51fa\u3002\u7b2c\u4e00\u4e2a\u89e3\u57281850\u5e74\u7531\u5f17\u6717\u5179\u00b7\u8bfa\u514b\uff08Franz Nauck\uff09\u7ed9\u51fa\u3002\u5e76\u4e14\u5c06\u5176\u63a8\u5e7f\u4e3a\u66f4\u4e00\u822c\u7684n\u7687\u540e\u6446\u653e\u95ee\u9898\u3002\u8bfa\u514b\u4e5f\u662f\u9996\u5148\u5c06\u95ee\u9898\u63a8\u5e7f\u5230\u66f4\u4e00\u822c\u7684n\u7687\u540e\u6446\u653e\u95ee\u9898\u7684\u4eba\u4e4b\u4e00\u3002<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

 <\/p>\n

\u95ee\u9898\u5206\u6790<\/strong><\/p>\n

 <\/p>\n

\u6211\u4eec\u5148\u4e0d\u770b8\u7687\u540e\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b4\u7687\u540e\uff0c\u5176\u5b9e\u539f\u7406\u90fd\u662f\u4e00\u6837\u7684\uff0c\u6bd4\u5982\u5728\u4e0b\u9762\u76844*4\u7684\u683c\u5b50\u91cc\uff0c\u5982\u679c\u6211\u4eec\u5728\u5176\u4e2d\u4e00\u4e2a\u683c\u5b50\u91cc\u8f93\u5165\u4e86\u7687\u540e\uff0c\u90a3\u4e48\u5728\u8fd9\u4e00\u884c\u8fd9\u4e00\u5217\u548c\u8fd9\u5de6\u53f3\u4e24\u8fb9\u7684\u5bf9\u89d2\u7ebf\u4e0a\u90fd\u4e0d\u80fd\u6709\u7687\u540e\u3002<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u6240\u4ee5\u6709\u4e00\u79cd\u65b9\u5f0f\u5c31\u662f\u6211\u4eec\u4e00\u4e2a\u4e2a\u53bb\u8bd5<\/p>\n

 <\/p>\n

\u7b2c\u4e00\u884c<\/strong><\/p>\n

\u6bd4\u5982\u6211\u4eec\u5728\u7b2c\u4e00\u884c\u7b2c\u4e00\u5217\u8f93\u5165\u4e86\u4e00\u4e2a\u7687\u540e<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u7b2c\u4e8c\u884c<\/strong><\/p>\n

\u7b2c\u4e8c\u884c\u6211\u4eec\u5c31\u4e0d\u80fd\u5728\u7b2c\u4e00\u5217\u548c\u7b2c\u4e8c\u5217\u8f93\u5165\u7687\u540e\u4e86\uff0c\u56e0\u4e3a\u6709\u51b2\u7a81\u4e86\u3002\u4f46\u6211\u4eec\u53ef\u4ee5\u5728\u7b2c3\u5217\u8f93\u5165\u7687\u540e<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u7b2c\u4e09\u884c<\/strong><\/p>\n

\u7b2c\u4e09\u884c\u6211\u4eec\u53d1\u73b0\u5728\u4efb\u4f55\u4f4d\u7f6e\u8f93\u5165\u90fd\u4f1a\u6709\u51b2\u7a81\u3002\u8fd9\u8bf4\u660e\u6211\u4eec\u4e4b\u524d\u9009\u62e9\u7684\u662f\u9519\u8bef\u7684\uff0c\u518d\u56de\u5230\u4e0a\u4e00\u6b65\uff0c\u6211\u4eec\u53d1\u73b0\u7b2c\u4e8c\u6b65\u4e0d\u5149\u80fd\u9009\u62e9\u7b2c3\u5217\uff0c\u800c\u4e14\u8fd8\u80fd\u9009\u62e9\u7b2c4\u5217\uff0c\u65e2\u7136\u9009\u62e9\u7b2c3\u5217\u4e0d\u5408\u9002\uff0c\u90a3\u6211\u4eec\u5c31\u9009\u62e9\u7b2c4\u5217\u5427<\/p>\n

 <\/p>\n

\u7b2c\u4e8c\u884c\uff08\u91cd\u65b0\u9009\u62e9\uff09<\/strong><\/p>\n

\u7b2c\u4e8c\u884c\u6211\u4eec\u9009\u62e9\u7b2c4\u5217<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u7b2c\u4e09\u884c<\/strong>\uff08\u91cd\u65b0\u9009\u62e9\uff09<\/p>\n

\u7b2c3\u884c\u6211\u4eec\u53ea\u6709\u9009\u62e9\u7b2c2\u5217\u4e0d\u4f1a\u6709\u51b2\u7a81<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u7b2c\u56db\u884c<\/strong><\/p>\n

\u6211\u4eec\u53d1\u73b0\u7b2c4\u884c\u53c8\u6ca1\u6709\u53ef\u9009\u62e9\u7684\u4e86\u3002\u7b2c\u4e00\u6b21\u91cd\u8bd5\u5931\u8d25<\/p>\n

 <\/p>\n

\u7b2c\u4e8c\u6b21\u91cd\u8bd5<\/strong><\/p>\n

\u5230\u8fd9\u91cc\u6211\u4eec\u53ea\u6709\u91cd\u65b0\u56de\u5230\u7b2c\u4e00\u6b65\u4e86\uff0c\u8fd9\u8bf4\u660e\u6211\u4eec\u4e4b\u524d\u7b2c\u4e00\u884c\u9009\u62e9\u7b2c\u4e00\u5217\u662f\u65e0\u89e3\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u7b2c\u4e00\u884c\u4e0d\u5e94\u8be5\u9009\u62e9\u7b2c\u4e00\u5217\uff0c\u6211\u4eec\u518d\u6765\u9009\u62e9\u7b2c\u4e8c\u5217\u6765\u8bd5\u8bd5<\/p>\n

\u7b2c\u4e00\u884c<\/strong><\/p>\n

\u8fd9\u4e00\u884c\u6211\u4eec\u9009\u62e9\u7b2c2\u5217<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u7b2c\u4e8c\u884c<\/strong><\/p>\n

\u7b2c\u4e8c\u884c\u6211\u4eec\u524d3\u4e2a\u90fd\u4e0d\u80fd\u9009\uff0c\u53ea\u80fd\u9009\u7b2c4\u5217<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u7b2c\u4e09\u884c<\/strong><\/p>\n

\u7b2c\u4e09\u884c\u6211\u4eec\u53ea\u80fd\u9009\u7b2c1\u5217<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u7b2c\u56db\u884c<\/strong><\/p>\n

\u7b2c\u56db\u884c\u6211\u4eec\u53ea\u80fd\u9009\u7b2c3\u5217<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u6700\u540e\u6211\u4eec\u7ec8\u4e8e\u627e\u5230\u4e86\u4e00\u7ec4\u89e3\u3002\u9664\u4e86\u8fd9\u7ec4\u89e3\u8fd8\u6709\u6ca1\u6709\u5176\u4ed6\u89e3\u5462\uff0c\u80af\u5b9a\u8fd8\u662f\u6709\u7684\uff0c\u56e0\u4e3a4\u7687\u540e\u662f\u6709\u4e24\u7ec4\u89e3\u7684\uff0c\u8fd9\u91cc\u6211\u4eec\u5c31\u4e0d\u5728\u4e00\u4e2a\u4e2a\u8bd5\u4e86\u3002<\/p>\n

 <\/p>\n

\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u4ed6\u67e5\u627e\u7684\u8fc7\u7a0b\uff0c\u5c31\u662f\u5148\u4ece\u7b2c1\u884c\u7684\u7b2c1\u5217\u5f00\u59cb\u5f80\u4e0b\u627e\uff0c\u7136\u540e\u518d\u4ece\u7b2c1\u884c\u7684\u7b2c2\u5217\u2026\u2026\uff0c\u4e00\u76f4\u5230\u7b2c1\u884c\u7684\u7b2cn\u5217\u3002\u4ee3\u7801\u8be5\u600e\u4e48\u5199\u5462\uff0c\u770b\u5230\u8fd9\u91cc\u4f30\u8ba1\u5927\u5bb6\u90fd\u5df2\u7ecf\u60f3\u5230\u4e86\uff0c\u8fd9\u4e0d\u5c31\u662f\u4e00\u68f5N\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386\u5417\uff0c\u6211\u4eec\u6765\u770b\u4e0b\uff0c\u662f\u4e0d\u662f\u4e0b\u9762\u8fd9\u6837\u7684\u3002<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386\u600e\u4e48\u5199\uff0c\u4e0d\u660e\u767d\u7684\u53ef\u4ee5\u53c2\u8003\u4e0b373\uff0c\u6570\u636e\u7ed3\u6784-6,\u6811<\/p>\n

public static void preOrder(TreeNode tree) {\n  \n  <\/code>    if (tree == null)<\/code>        return;<\/code>    System.out.printf(tree.val + \"\");<\/code>    preOrder(tree.left);<\/code>    preOrder(tree.right);<\/code>}<\/code><\/pre>\n

\u4e8c\u53c9\u6811\u662f\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\uff0c\u90a3\u4e48N\u53c9\u6811\u5f53\u7136\u662f\u6709N\u4e2a\u5b50\u8282\u70b9\u4e86\uff0c\u6240\u4ee5N\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386\u662f\u8fd9\u6837\u7684<\/p>\n

public static void preOrder(TreeNode tree) {\n  \n  <\/code>    if (tree == null)<\/code>        return;<\/code>    System.out.printf(tree.val + \"\");<\/code>    preOrder(\"\u7b2c1\u4e2a\u5b50\u8282\u70b9\");<\/code>    preOrder(\"\u7b2c2\u4e2a\u5b50\u8282\u70b9\");<\/code>    \u2026\u2026<\/code>    preOrder(\"\u7b2cn\u4e2a\u5b50\u8282\u70b9\");<\/code>}<\/code><\/pre>\n

\u5982\u679cN\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u503c\uff0c\u8fd9\u6837\u5199\u8981\u5199\u6b7b\u4eba\u4e86\uff0c\u6211\u4eec\u4e00\u822c\u4f7f\u7528\u5faa\u73af\u7684\u65b9\u5f0f<\/p>\n

public static void preOrder(TreeNode tree) {\n  \n  <\/code>    if (tree == null)<\/code>        return;<\/code>    System.out.printf(tree.val + \"\");<\/code>    for (int i = 0; i <n ; i++) {\n  \n  <\/code>        preOrder(\"\u7b2ci\u4e2a\u5b50\u8282\u70b9\");<\/code>    }<\/code>}<\/code><\/pre>\n

\u641e\u61c2\u4e86\u4e0a\u9762\u7684\u5206\u6790\u8fc7\u7a0b\uff0c\u90a3\u4e48\u8fd9\u9898\u7684\u4ee3\u7801\u8f6e\u5ed3\u5c31\u547c\u4e4b\u6b32\u51fa\u4e86<\/p>\n

\n private void solve(char[][] chess, int row) {\n     \"\u7ec8\u6b62\u6761\u4ef6\"\n     return;\n \n     for (int col = 0; col < chess.length; col++) {\n         \/\/\u5224\u65ad\u8fd9\u4e2a\u4f4d\u7f6e\u662f\u5426\u53ef\u4ee5\u653e\u7687\u540e\n         if (valid(chess, row, col)) {\n             \/\/\u5982\u679c\u53ef\u4ee5\u653e\uff0c\u6211\u4eec\u5c31\u628a\u539f\u6765\u7684\u6570\u7ec4chess\u590d\u5236\u4e00\u4efd\uff0c\n             char[][] temp = copy(chess);\n            \/\/\u7136\u540e\u5728\u8fd9\u4e2a\u4f4d\u7f6e\u653e\u4e0a\u7687\u540e\n            temp[row][col] = 'Q';\n            \/\/\u4e0b\u4e00\u884c\n            solve(temp, row + 1);\n         }\n    }\n}<\/code><\/pre>\n

\u6211\u4eec\u6765\u5206\u6790\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u56e0\u4e3a\u662f\u9012\u5f52\u6240\u4ee5\u5fc5\u987b\u8981\u6709\u7ec8\u6b62\u6761\u4ef6\uff0c\u90a3\u4e48\u8fd9\u9898\u7684\u7ec8\u6b62\u6761\u4ef6\u5c31\u662f\u6211\u4eec\u6700\u540e\u4e00\u884c\u5df2\u7ecf\u8d70\u5b8c\u4e86\uff0c\u4e5f\u5c31\u662f<\/p>\n

if (row == chess.length) {\n  \n  <\/code>         return;<\/code>}<\/code><\/pre>\n

\u7b2c7\u884c\u5c31\u662f\u5224\u65ad\u5728\u8fd9\u4e2a\u4f4d\u7f6e\u6211\u4eec\u80fd\u4e0d\u80fd\u653e\u7687\u540e\uff0c\u5982\u679c\u4e0d\u80fd\u653e\u6211\u4eec\u5c31\u5224\u65ad\u8fd9\u4e00\u884c\u7684\u4e0b\u4e00\u5217\u80fd\u4e0d\u80fd\u653e\u2026\u2026\uff0c\u5982\u679c\u80fd\u653e\u6211\u4eec\u5c31\u628a\u539f\u6765\u6570\u7ec4chess\u590d\u5236\u4e00\u4efd\uff0c\u7136\u540e\u628a\u7687\u540e\u653e\u5230\u8fd9\u4e2a\u4f4d\u7f6e\uff0c\u7136\u540e\u518d\u5224\u65ad\u4e0b\u4e00\u884c\uff0c\u8fd9\u548c\u6211\u4eec\u4e0a\u9762\u753b\u56fe\u7684\u8fc7\u7a0b\u975e\u5e38\u7c7b\u4f3c\u3002\u6ce8\u610f\u8fd9\u91cc\u7684\u7b2c9\u884c\u4e3a\u4ec0\u4e48\u8981\u590d\u5236\u4e00\u4efd\uff0c\u56e0\u4e3a\u6570\u7ec4\u662f\u5f15\u7528\u4f20\u9012\uff0c\u8fd9\u6d89\u53ca\u5230\u9012\u5f52\u7684\u65f6\u5019\u5206\u652f\u6c61\u67d3\u95ee\u9898\uff08\u540e\u9762\u6709\u65f6\u95f4\u6211\u4f1a\u4e13\u95e8\u5199\u4e00\u7bc7\u5173\u4e8e\u9012\u5f52\u7684\u65f6\u5019\u5206\u652f\u6c61\u67d3\u95ee\u9898\uff09\u3002\u5f53\u7136\u4e0d\u590d\u5236\u4e00\u4efd\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u6211\u4eec\u4e0b\u9762\u518d\u8bb2\u3002\u5f53\u6211\u4eec\u628a\u4e0a\u9762\u7684\u95ee\u9898\u90fd\u641e\u61c2\u7684\u65f6\u5019\uff0c\u4ee3\u7801\u4e5f\u5c31\u5f88\u5bb9\u6613\u5199\u51fa\u6765\u4e86\uff0c\u6211\u4eec\u6765\u770b\u4e0bN\u7687\u540e\u7684\u6700\u7ec8\u4ee3\u7801<\/p>\n

 1public void solveNQueens(int n) {\n 2    char[][] chess = new char[n][n];\n 3    for (int i = 0; i < n; i++)\n 4        for (int j = 0; j < n; j++)\n 5            chess[i][j] = '.';\n 6    solve(chess, 0);\n 7}\n 8\n 9private void solve(char[][] chess, int row) {\n10    if (row == chess.length) {\n11        \/\/\u81ea\u5df1\u5199\u7684\u4e00\u4e2a\u516c\u5171\u65b9\u6cd5\uff0c\u6253\u5370\u4e8c\u7ef4\u6570\u7ec4\u7684\uff0c\n12        \/\/ \u4e3b\u8981\u7528\u4e8e\u6d4b\u8bd5\u6570\u636e\u7528\u7684\n13        Util.printTwoCharArrays(chess);\n14        System.out.println();\n15        return;\n16    }\n17    for (int col = 0; col < chess.length; col++) {\n18        if (valid(chess, row, col)) {\n19            char[][] temp = copy(chess);\n20            temp[row][col] = 'Q';\n21            solve(temp, row + 1);\n22        }\n23    }\n24}\n25\n26\/\/\u628a\u4e8c\u7ef4\u6570\u7ec4chess\u4e2d\u7684\u6570\u636e\u6d4b\u4e0bcopy\u4e00\u4efd\n27private char[][] copy(char[][] chess) {\n28    char[][] temp = new char[chess.length][chess[0].length];\n29    for (int i = 0; i < chess.length; i++) {\n30        for (int j = 0; j < chess[0].length; j++) {\n31            temp[i][j] = chess[i][j];\n32        }\n33    }\n34    return temp;\n35}\n36\n37\/\/row\u8868\u793a\u7b2c\u51e0\u884c\uff0ccol\u8868\u793a\u7b2c\u51e0\u5217\n38private boolean valid(char[][] chess, int row, int col) {\n39    \/\/\u5224\u65ad\u5f53\u524d\u5217\u6709\u6ca1\u6709\u7687\u540e,\u56e0\u4e3a\u4ed6\u662f\u4e00\u884c\u4e00\u884c\u5f80\u4e0b\u8d70\u7684\uff0c\n40    \/\/\u6211\u4eec\u53ea\u9700\u8981\u68c0\u67e5\u8d70\u8fc7\u7684\u884c\u6570\u5373\u53ef\uff0c\u901a\u4fd7\u4e00\u70b9\u5c31\u662f\u5224\u65ad\u5f53\u524d\n41    \/\/\u5750\u6807\u4f4d\u7f6e\u7684\u4e0a\u9762\u6709\u6ca1\u6709\u7687\u540e\n42    for (int i = 0; i < row; i++) {\n43        if (chess[i][col] == 'Q') {\n44            return false;\n45        }\n46    }\n47    \/\/\u5224\u65ad\u5f53\u524d\u5750\u6807\u7684\u53f3\u4e0a\u89d2\u6709\u6ca1\u6709\u7687\u540e\n48    for (int i = row - 1, j = col + 1; i >= 0 && j < chess.length; i--, j++) {\n49        if (chess[i][j] == 'Q') {\n50            return false;\n51        }\n52    }\n53    \/\/\u5224\u65ad\u5f53\u524d\u5750\u6807\u7684\u5de6\u4e0a\u89d2\u6709\u6ca1\u6709\u7687\u540e\n54    for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {\n55        if (chess[i][j] == 'Q') {\n56            return false;\n57        }\n58    }\n59    return true;\n60}\n<\/code><\/pre>\n

\u4ee3\u7801\u770b\u8d77\u6765\u6bd4\u8f83\u591a\uff0c\u6211\u4eec\u4e3b\u8981\u770b\u4e0bsolve\u65b9\u6cd5\u5373\u53ef\uff0c\u5176\u4ed6\u7684\u65b9\u6cd5\u4e0d\u770b\u4e5f\u53ef\u4ee5\uff0c\u77e5\u9053\u6709\u8fd9\u4e2a\u529f\u80fd\u5c31\u884c\u3002solve\u4ee3\u7801\u4e2d\u5176\u6838\u5fc3\u4ee3\u7801\u662f\u572817-23\u884c\uff0c\u4e0a\u9762\u662f\u7ec8\u6b62\u6761\u4ef6\u7684\u5224\u65ad\uff0c\u6211\u4eec\u5c31\u75284\u7687\u540e\u6765\u6d4b\u8bd5\u4e00\u4e0b<\/p>\n

solveNQueens(4);<\/code><\/pre>\n

\u770b\u4e00\u4e0b\u6253\u5370\u7684\u7ed3\u679c<\/p>\n

\"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898,<\/p>\n

\u6211\u4eec\u770b\u52304\u7687\u540e\u7684\u65f6\u5019\u6709\u4e24\u7ec4\u89e3\uff0c\u5176\u4e2d\u7b2c\u4e00\u7ec4\u548c\u6211\u4eec\u4e0a\u9762\u56fe\u4e2d\u5206\u6790\u7684\u5b8c\u5168\u4e00\u6837\u3002<\/p>\n

4\u7687\u540e\u89e3\u51b3\u4e86\uff0c\u90a3\u4e488\u7687\u540e\u4e5f\u4e00\u6837\uff0c\u6211\u4eec\u53ea\u8981\u5728\u51fd\u6570\u8c03\u7528\u7684\u65f6\u5019\u4f20\u51658\u5c31\u53ef\u4ee5\u4e86\u3002\u540c\u7406\uff0c\u8981\u60f3\u8ba1\u7b9710\u7687\u540e\uff0c20\u7687\u540e\uff0c100\u7687\u540e\u2026\u2026\u4e5f\u90fd\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n

 <\/p>\n

\u56de\u6eaf\u89e3\u51b3<\/strong><\/p>\n

\u4e0a\u9762\u4ee3\u7801\u4e2d\u6bcf\u6b21\u9047\u5230\u80fd\u653e\u7687\u540e\u7684\u65f6\u5019\uff0c\u6211\u4eec\u90fd\u4f1a\u628a\u539f\u6570\u7ec4\u590d\u5236\u4e00\u4efd\uff0c\u8fd9\u6837\u5bf9\u65b0\u6570\u636e\u7684\u4fee\u6539\u5c31\u4e0d\u4f1a\u5f71\u54cd\u5230\u539f\u6765\u7684\uff0c\u4e5f\u5c31\u662f\u4e0d\u4f1a\u9020\u6210\u5206\u652f\u6c61\u67d3\u3002\u4f46\u8fd9\u6837\u6bcf\u6b21\u5c1d\u8bd5\u7684\u65f6\u5019\u90fd\u90fd\u628a\u539f\u6570\u7ec4\u590d\u5236\u4e00\u4efd\uff0c\u5f71\u54cd\u6548\u7387\uff0c\u6709\u6ca1\u6709\u5176\u4ed6\u7684\u65b9\u6cd5\u4e0d\u590d\u5236\u5462\uff0c\u662f\u6709\u7684\u3002\u5c31\u662f\u6bcf\u6b21\u6211\u4eec\u9009\u62e9\u628a\u8fd9\u4e2a\u4f4d\u7f6e\u653e\u7f6e\u7687\u540e\u7684\u65f6\u5019\uff0c\u5982\u679c\u6700\u7ec8\u4e0d\u80fd\u6210\u529f\uff0c\u90a3\u4e48\u8fd4\u56de\u7684\u65f6\u5019\u6211\u4eec\u5c31\u8fd8\u8981\u628a\u8fd9\u4e2a\u4f4d\u7f6e\u8fd8\u539f\u3002\u8fd9\u5c31\u662f\u56de\u6eaf\u7b97\u6cd5\uff0c\u4e5f\u662f\u8bd5\u63a2\u7b97\u6cd5\u3002\u6211\u4eec\u6765\u770b\u4e0b\u4ee3\u7801<\/p>\n

 1private void solve(char[][] chess, int row) {\n 2    if (row == chess.length) {\n 3        \/\/\u81ea\u5df1\u5199\u7684\u4e00\u4e2a\u516c\u5171\u65b9\u6cd5\uff0c\u6253\u5370\u4e8c\u7ef4\u6570\u7ec4\u7684\uff0c\n 4        \/\/ \u4e3b\u8981\u7528\u4e8e\u6d4b\u8bd5\u6570\u636e\u7528\u7684\n 5        Util.printTwoCharArrays(chess);\n 6        System.out.println();\n 7        return;\n 8    }\n 9    for (int col = 0; col < chess.length; col++) {\n10        if (valid(chess, row, col)) {\n11            chess[row][col] = 'Q';\n12            solve(chess, row + 1);\n13            chess[row][col] = '.';\n14        }\n15    }\n16}\n<\/code><\/pre>\n

\u4e3b\u8981\u6765\u770b\u4e0b11-13\u884c\uff0c\u5176\u4ed6\u7684\u90fd\u6ca1\u53d8\uff0c\u8fd8\u548c\u4e0a\u9762\u7684\u4e00\u6837\u3002\u8fd9\u548c\u6211\u4eec\u4e4b\u524d\u8bb2\u7684391\uff0c\u56de\u6eaf\u7b97\u6cd5\u6c42\u7ec4\u5408\u95ee\u9898\u5f88\u7c7b\u4f3c\u3002\u4ed6\u662f\u5148\u5047\u8bbe[row][col]\u8fd9\u4e2a\u4f4d\u7f6e\u53ef\u4ee5\u653e\u7687\u540e\uff0c\u7136\u540e\u5f80\u4e0b\u627e\uff0c\u65e0\u8bba\u627e\u5230\u627e\u4e0d\u5230\u6700\u540e\u90fd\u4f1a\u56de\u5230\u8fd9\u4e2a\u5730\u65b9\uff0c\u56e0\u4e3a\u8fd9\u91cc\u662f\u9012\u5f52\u8c03\u7528\uff0c\u56de\u5230\u8fd9\u4e2a\u5730\u65b9\u7684\u65f6\u5019\u6211\u4eec\u518d\u628a\u5b83\u590d\u539f\uff0c\u7136\u540e\u8d70\u4e0b\u4e00\u4e2a\u5206\u652f\u3002\u6700\u540e\u6211\u4eec\u518d\u6765\u770b\u4e0b\u4f7f\u7528\u56de\u6eaf\u7b97\u6cd5\u89e3N\u7687\u540e\u7684\u5b8c\u6574\u4ee3\u7801<\/p>\n

 public void solveNQueens(int n) {\n     char[][] chess = new char[n][n];\n     for (int i = 0; i < n; i++)\n         for (int j = 0; j < n; j++)\n             chess[i][j] = '.';\n     solve(chess, 0);\n }\n \n private void solve(char[][] chess, int row) {\n    if (row == chess.length) {\n        \/\/\u81ea\u5df1\u5199\u7684\u4e00\u4e2a\u516c\u5171\u65b9\u6cd5\uff0c\u6253\u5370\u4e8c\u7ef4\u6570\u7ec4\u7684\uff0c\n        \/\/ \u4e3b\u8981\u7528\u4e8e\u6d4b\u8bd5\u6570\u636e\u7528\u7684\n        Util.printTwoCharArrays(chess);\n        System.out.println();\n        return;\n    }\n    for (int col = 0; col < chess.length; col++) {\n        if (valid(chess, row, col)) {\n            chess[row][col] = 'Q';\n            solve(chess, row + 1);\n            chess[row][col] = '.';\n        }\n    }\n}\n\n\/\/row\u8868\u793a\u7b2c\u51e0\u884c\uff0ccol\u8868\u793a\u7b2c\u51e0\u5217\nprivate boolean valid(char[][] chess, int row, int col) {\n    \/\/\u5224\u65ad\u5f53\u524d\u5217\u6709\u6ca1\u6709\u7687\u540e,\u56e0\u4e3a\u4ed6\u662f\u4e00\u884c\u4e00\u884c\u5f80\u4e0b\u8d70\u7684\uff0c\n    \/\/\u6211\u4eec\u53ea\u9700\u8981\u68c0\u67e5\u8d70\u8fc7\u7684\u884c\u6570\u5373\u53ef\uff0c\u901a\u4fd7\u4e00\u70b9\u5c31\u662f\u5224\u65ad\u5f53\u524d\n    \/\/\u5750\u6807\u4f4d\u7f6e\u7684\u4e0a\u9762\u6709\u6ca1\u6709\u7687\u540e\n    for (int i = 0; i < row; i++) {\n        if (chess[i][col] == 'Q') {\n            return false;\n        }\n    }\n    \/\/\u5224\u65ad\u5f53\u524d\u5750\u6807\u7684\u53f3\u4e0a\u89d2\u6709\u6ca1\u6709\u7687\u540e\n    for (int i = row - 1, j = col + 1; i >= 0 && j < chess.length; i--, j++) {\n        if (chess[i][j] == 'Q') {\n            return false;\n        }\n    }\n    \/\/\u5224\u65ad\u5f53\u524d\u5750\u6807\u7684\u5de6\u4e0a\u89d2\u6709\u6ca1\u6709\u7687\u540e\n    for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {\n        if (chess[i][j] == 'Q') {\n            return false;\n        }\n    }\n    return true;\n}\n<\/code><\/pre>\n

 <\/p>\n

\u603b\u7ed3<\/strong><\/p>\n

8\u7687\u540e\u95ee\u9898\u5176\u5b9e\u662f\u4e00\u9053\u5f88\u7ecf\u5178\u7684\u56de\u6eaf\u7b97\u6cd5\u9898\uff0c\u6211\u4eec\u8fd9\u91cc\u5e76\u6ca1\u6709\u4e13\u95e8\u9488\u5bf98\u7687\u540e\u6765\u8bb2\uff0c\u6211\u4eec\u8fd9\u91cc\u8bb2\u7684\u662fN\u7687\u540e\uff0c\u5982\u679c\u8fd9\u9053\u9898\u641e\u61c2\u4e86\uff0c\u90a3\u4e488\u7687\u540e\u81ea\u7136\u4e5f\u5c31\u61c2\u4e86\uff0c\u56e0\u4e3a\u8fd9\u91cc\u7684N\u53ef\u4ee5\u662f\u4efb\u4f55\u6b63\u6574\u6570\u3002<\/p>\n

 <\/p>\n

\u9012\u5f52\u4e00\u822c\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\u5206\u652f\uff0c\u6211\u4eec\u8981\u4fdd\u8bc1\u6bcf\u4e2a\u5206\u652f\u7684\u4fee\u6539\u4e0d\u4f1a\u6c61\u67d3\u5176\u4ed6\u5206\u652f\uff0c\u4e5f\u5c31\u662f\u4e0d\u8981\u5bf9\u5176\u4ed6\u5206\u652f\u9020\u6210\u5f71\u54cd\uff0c\u8fd9\u4e00\u70b9\u5f88\u91cd\u8981\u3002\u7531\u4e8e\u4e00\u4e9b\u8bed\u8a00\u4e2d\u9664\u4e86\u57fa\u672c\u7c7b\u578b\u4ee5\u5916\uff0c\u5176\u4ed6\u7684\u5927\u90e8\u5206\u90fd\u662f\u5f15\u7528\u4f20\u9012\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u4e00\u4e2a\u5206\u652f\u4fee\u6539\u4e4b\u540e\u53ef\u80fd\u5c31\u4f1a\u5bf9\u5176\u4ed6\u5206\u652f\u4ea7\u751f\u4e00\u4e9b\u6211\u4eec\u4e0d\u60f3\u8981\u7684\u5783\u573e\u6570\u636e\uff0c\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u5c31\u6709\u4e24\u4e2d\u89e3\u51b3\u65b9\u5f0f\uff0c\u4e00\u79cd\u5c31\u662f\u6211\u4eec\u4e0a\u9762\u8bb2\u5230\u7684\u7b2c\u4e00\u79cd\uff0c\u590d\u5236\u4e00\u4efd\u65b0\u7684\u6570\u636e\uff0c\u8fd9\u6837\u6bcf\u4e2a\u5206\u652f\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e9b\u65b0\u7684\u6570\u636e\uff0c\u6240\u4ee5\u5373\u4f7f\u4fee\u6539\u4e86\u4e5f\u4e0d\u4f1a\u5bf9\u5176\u4ed6\u5206\u652f\u6709\u5f71\u54cd\u3002\u8fd8\u4e00\u79cd\u65b9\u5f0f\u5c31\u662f\u6211\u4eec\u6bcf\u6b21\u4f7f\u7528\u5b8c\u4e4b\u540e\u518d\u628a\u5b83\u590d\u539f\uff0c\u4e00\u822c\u7684\u60c5\u51b5\u4e0b\u6211\u4eec\u90fd\u4f1a\u9009\u62e9\u7b2c\u4e8c\u79cd\uff0c\u56e0\u4e3a\u8fd9\u79cd\u4ee3\u7801\u66f4\u7b80\u6d01\u4e00\u4e9b\uff0c\u4e5f\u4e0d\u4f1a\u91cd\u590d\u7684\u590d\u5236\u6570\u636e\uff0c\u9020\u6210\u5927\u91cf\u7684\u5783\u573e\u6570\u636e\u3002<\/p>\n

 <\/p>\n

\u8f6c\u81ea\uff1ahttps:\/\/mp.weixin.qq.com\/s?__biz=MzU0ODMyNDk0Mw==&mid=2247487459&idx=1&sn=f45fc8231198edb3de2acc69e17986ea&chksm=fb419cc3cc3615d5d31d55072d5c2f58e4002aa8c1b637e89f1ef577e253943c04efc39c1b1d&scene=21#wechat_redirect<\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"2021-01-11\u7ecf\u5178\u7684\u516b\u7687\u540e\u95ee\u9898\u548cN\u7687\u540e\u95ee\u9898, \u56de\u6eaf\u516b\u7687\u540e\u7684\u6765\u6e90\u516b\u7687\u540e\u95ee\u9898\u662f\u4e00\u4e2a\u4ee5\u56fd\u9645\u8c61\u68cb\u4e3a\u80cc\u666f\u7684\u95ee\u9898\uff1a\u5982\u4f55\u80fd\u591f\u57288\u00d78\u7684\u56fd\u9645\u8c61...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/9071"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=9071"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/9071\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=9071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=9071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=9071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}