{"id":1813,"date":"2019-04-14T18:06:31","date_gmt":"2019-04-14T10:06:31","guid":{"rendered":"https:\/\/coderbee.net\/?p=1813"},"modified":"2019-04-14T21:50:57","modified_gmt":"2019-04-14T13:50:57","slug":"8%e7%9a%87%e5%90%8e%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/algorithm\/20190414\/1813","title":{"rendered":"8\u7687\u540e\u95ee\u9898"},"content":{"rendered":"<h2>\u4e00\u3001\u95ee\u9898<\/h2>\n<p>\u4e00\u4e2a 8&#215;8 \u7684\u68cb\u76d8\uff0c\u5e0c\u671b\u5f80\u91cc\u653e 8 \u4e2a\u68cb\u5b50\uff0c\u6bcf\u4e2a\u68cb\u6240\u5728\u7684\u884c\u3001\u5217\u3001\u5bf9\u89d2\u7ebf\u4e0a\u90fd\u4e0d\u80fd\u6709\u5176\u4ed6\u68cb\u5b50\u3002\u627e\u51fa\u6240\u6709\u6ee1\u8db3\u8981\u6c42\u7684\u5e03\u5c40\u3002<\/p>\n<h2>\u4e8c\u3001\u5206\u6790\u4e0e\u89e3<\/h2>\n<p>\u9996\u5148\u753b\u51fa\u4e0b\u9762\u8fd9\u6837\u4e00\u4e2a\u68cb\u76d8\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/04\/nqueen-layout.jpg\" alt=\"\" width=\"980\" height=\"768\" class=\"alignnone size-full wp-image-1819\" \/><\/p>\n<p>\u884c\u3001\u5217\u7684\u4e0b\u6807\u90fd\u662f\u4ece 0 \u5f00\u59cb\u3002<\/p>\n<p>\u9010\u884c\u6446\u653e\u68cb\u5b50\uff0c\u6bd4\u5982\u5f53\u524d\u8981\u6446\u653e\u7b2c R \u884c\u7684\u68cb\u5b50\u65f6\uff0c\u8ba4\u4e3a 0 \u81f3 R-1 \u884c\u7684\u68cb\u5b50\u662f\u7b26\u5408\u8981\u6c42\u7684\uff0c\u90a3\u4e48\u5bf9\u4e8e\u7b2c R \u884c\uff0c\u5c1d\u8bd5\u628a\u68cb\u5b50\u6446\u653e\u5230\u7b2c C \u5217\uff0c\u68c0\u6d4b\u662f\u5426\u6ee1\u8db3\u8981\u6c42\uff0c\u5982\u679c\u6ee1\u8db3\u5219\u7ee7\u7eed\u4e0b\u4e00\u884c\u6446\u653e\u4e0b\u4e00\u884c\uff0c\u4e0d\u6ee1\u8db3\u5219\u5219\u5c1d\u8bd5\u653e\u81f3 C+1 \u5217\u3002\u5982\u679c\u6446\u653e\u5230\u4e86\u7b2c 8 \u884c\uff0c\u5219\u8ba4\u4e3a\u5f97\u5230\u4e86\u4e00\u4e2a\u89e3\u3002<\/p>\n<p>\u68c0\u6d4b\u7b97\u6cd5\uff1a\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u51fa\uff0c\u53ea\u9700\u8981\u68c0\u67e5\u4e09\u79cd\u573a\u666f\uff0c\u5bf9\u4e8e\u4f4d\u7f6e(row, col)\uff1a<\/p>\n<ol>\n<li>\u5de6\u4e0b\u5bf9\u89d2\u7ebf\uff1b\u8be5\u5bf9\u89d2\u7ebf\u4e0a\u7684\u70b9\u7b26\u5408 <code>col &gt;= 0 &amp;&amp; (row--, col--)<\/code>\u3002<\/li>\n<li>\u5782\u76f4\u5217\uff1b(row&#8211;, col)\uff0c \u884c \u6e10\u51cf\uff0c\u5217\u4e0d\u53d8\u3002<\/li>\n<li>\u53f3\u4e0b\u5bf9\u89d2\u7ebf\u3002\u8be5\u5bf9\u89d2\u7ebf\u4e0a\u7684\u70b9\u7b26\u5408 <code>col &lt; 8 &amp;&amp; (row--, col++)<\/code>\u3002<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<h2>\u4e09\u3001\u89e3\u6cd5\u4e00<\/h2>\n<p>\u4ee5\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u4f5c\u4e3a\u68cb\u76d8\uff0c\u6446\u653e\u6709\u68cb\u5b50\u7684\u4f4d\u7f6e\u8bbe\u4e3a 1\uff0c\u6ca1\u6709\u7684\u4e3a 0 \u3002<\/p>\n<pre><code class=\"java\">import java.util.Arrays;\n\npublic class NQueen {\n    private final int[][] arr;\n    private final int num;\n    private int resultCount = 0; \/\/ \u89e3\u7a7a\u95f4\u6570\u91cf\n\n    public NQueen(int n) {\n        if (n &lt; 1) {\n            throw new IllegalArgumentException(\"n &lt; 1\");\n        }\n        num = n;\n        arr = new int[num][num];\n    }\n\n    public void play() {\n        \/\/ \u4ece\u7b2c 0 \u884c\u5f00\u59cb\u6446\u653e\n        run(0);\n    }\n\n    \/**\n     * \u4ee5 \u884c \u4e3a\u6b65\u9aa4\uff0c\u9010\u884c\u653e\u7f6e\u68cb\u5b50\u3002\n     *\n     * @param row\n     *\/\n    private void run(int row) {\n        if (row == 8) {\n            resultCount++;\n            print();\n            return;\n        }\n\n        for (int c = 0; c &lt; num; c++) {\n            \/\/ \u5bf9\u4e8e\u7ed9\u5b9a\u7684\u884c\uff0c\u904d\u5386\u6240\u6709\u7684\u5217\uff0c\u5982\u679c\u884c\u3001\u5217\u7ec4\u5408\u6ee1\u8db3\u5219\u8fdb\u5165\u4e0b\u4e00\u884c\u3002\n            if (isValid(row, c)) {\n                arr[row][c] = 1;\n                run(row + 1);\n\n                \/\/ \u56de\u6eaf\u5230\u5f53\u524d\u884c\uff0c\u7ee7\u7eed\u4e0b\u4e00\u5217\n                arr[row][c] = 0;\n            }\n        }\n    }\n\n    private boolean isValid(int row, int col) {\n        int leftDown = col - 1;\n        int rightDown = col + 1;\n\n        for (int r = row - 1; r &gt;= 0; r--) {\n            if (arr[r][col] == 1) { \/\/ \u68c0\u6d4b\u5217\u4e0a\u662f\u5426\u6709\n                return false;\n            }\n\n            if (leftDown &gt;= 0 &amp;&amp; arr[r][leftDown] == 1) { \/\/ \u68c0\u6d4b\u5de6\u4e0b\u5bf9\u89d2\u7ebf\n                return false;\n            }\n\n            if (rightDown &lt; num &amp;&amp; arr[r][rightDown] == 1) { \/\/ \u68c0\u6d4b\u53f3\u4e0b\u5bf9\u89d2\u7ebf\n                return false;\n            }\n\n            leftDown--;\n            rightDown++;\n        }\n\n        return true;\n    }\n\n    private void print() {\n        System.out.println(\"\u7b2c \" + resultCount + \" \u4e2a\u89e3\");\n        for (int r = 0; r &lt; arr.length; r++) {\n            String s = Arrays.toString(arr[r]);\n            System.out.println(s.substring(1, s.length() - 1).replaceAll(\",\", \"\"));\n        }\n    }\n\n}\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7684\u7b2c\u4e00\u4e2a\u89e3\u5982\u56fe\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"132\" height=\"174\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/04\/NQueen-result-1.jpg\" alt=\"\" class=\"alignnone size-full wp-image-1815\" \/><\/p>\n<h2>\u56db\u3001\u89e3\u6cd5\u4e8c<\/h2>\n<p>\u8fd9\u4e2a\u89e3\u6cd5\u6765\u81ea\u6781\u5ba2\u65f6\u95f4\u4e13\u680f\u300a<a href=\"https:\/\/time.geekbang.org\/column\/intro\/126?code=KPx9mAjH-nRXWF00EGIfhkxBMzgad7Uuy39%2FItmyzL0%3D&#038;utm_term=SPoster\" rel=\"noopener noreferrer\" target=\"_blank\">\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u7f8e<\/a>\u300b\u3002<\/p>\n<p>\u4ece\u89e3\u6cd5\u4e00\u7684\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u4e8c\u7ef4\u6570\u7ec4\u91cc\u57fa\u672c\u5168\u662f 0\uff0c\u540c\u4e00\u884c\u53ea\u6709\u4e00\u4e2a\u5217\u4e0a\u6709\u503c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u7ef4\u6570\u7ec4\u6765\u8868\u793a\u884c\uff0c\u7528\u6570\u7ec4\u5143\u7d20\u7684\u503c\u8868\u793a\u628a\u68cb\u5b50\u653e\u5230\u54ea\u4e00\u5217\u3002\u6bd4\u5982 arr[1] = 3 \u8868\u793a\u7b2c 1 \u884c\u3001\u7b2c 3 \u5217\u4e0a\u653e\u68cb\u5b50\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">public class GeekTimeNQueen {\n    private int num;\n    private int[] arr;\n    private int resultCount;\n\n    public GeekTimeNQueen(int n) {\n        if (n &lt; 1) {\n            throw new IllegalArgumentException(\"n &lt; 1\");\n        }\n        num = n;\n        arr = new int[num];\n    }\n\n    public void play(){\n        run(0);\n    }\n\n    private void run(int row) {\n        if (row == num) {\n            resultCount++;\n            print();\n            return;\n        }\n\n        for (int c = 0; c &lt; num; c++) {\n            if(isValid(row, c)) {\n                arr[row] = c;\n                run(row + 1);\n            }\n        }\n    }\n\n    private boolean isValid(int row, int col) {\n        int leftDown = col - 1;\n        int rightDown = col + 1;\n\n        for(int r = row - 1; r &gt;= 0; r--) {\n            if (arr[r] == col) { \/\/ \u68c0\u6d4b\u5782\u76f4\u5217\n                return false;\n            }\n\n            if (leftDown &gt;= 0 &amp;&amp; arr[r] == leftDown) { \/\/ \u68c0\u6d4b\u5de6\u4e0b\n                return false;\n            }\n\n            if (rightDown &lt; num &amp;&amp; arr[r] == rightDown) { \/\/ \u68c0\u6d4b\u53f3\u4e0b\n                return false;\n            }\n\n            leftDown--;\n            rightDown++;\n        }\n\n        return true;\n    }\n\n    private void print() {\n        System.out.println(\"\u7b2c \" + resultCount + \" \u4e2a\u89e3\");\n        for (int r = 0; r &lt; num; r++) {\n            for (int c = 0; c &lt; num; c++) {\n                if (arr[r] == c) {\n                    System.out.print(\"1 \");\n                } else {\n                    System.out.print(\"0 \");\n                }\n            }\n            System.out.println();\n        }\n        System.out.println();\n    }\n\n}\n<\/code><\/pre>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u95ee\u9898 \u4e00\u4e2a 8&#215;8 \u7684\u68cb\u76d8\uff0c\u5e0c\u671b\u5f80\u91cc\u653e 8 \u4e2a\u68cb\u5b50\uff0c\u6bcf\u4e2a\u68cb\u6240\u5728\u7684\u884c &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/algorithm\/20190414\/1813\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[310,311],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1813"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1813"}],"version-history":[{"count":8,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1813\/revisions"}],"predecessor-version":[{"id":1824,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1813\/revisions\/1824"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}