{"id":2294,"date":"2016-10-16T17:11:11","date_gmt":"2016-10-16T09:11:11","guid":{"rendered":"http:\/\/boweihe.me\/?p=2294"},"modified":"2016-10-16T17:11:11","modified_gmt":"2016-10-16T09:11:11","slug":"leetcode-51-n-queens","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2294","title":{"rendered":"LeetCode 51. N-Queens"},"content":{"rendered":"<p>\u516b\u7687\u540e\u95ee\u9898\u7684\u5ef6\u4f38\u7248\uff0c\u56de\u6eaf\u5b9e\u73b0\u3002<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\nbool checkQueen(int** mat, int N, int r, int c) {\n\tfor (int row = 0; row &lt; N; row++) {\n\t\tif (row == r)\n\t\t\tcontinue;\n\t\tif (mat[row][c] == 1)\n\t\t\treturn false;\n\t}\n\t\/\/\n\tint row = r-1, col = c-1;\n\twhile (row &gt;= 0 &amp;&amp; col &gt;= 0) {\n\t\tif (mat[row][col] == 1)\n\t\t\treturn false;\n\t\trow--; col--;\n\t}\n\trow = r + 1, col = c + 1;\n\twhile (row &lt; N &amp;&amp; col &lt; N) {\n\t\tif (mat[row][col] == 1)\n\t\t\treturn false;\n\t\trow++; col++;\n\t}\n\t\/\/\n\t\/\/\n\trow = r - 1, col = c + 1;\n\twhile (row &gt;= 0 &amp;&amp; col &lt; N) {\n\t\tif (mat[row][col] == 1)\n\t\t\treturn false;\n\t\trow--; col++;\n\t}\n\trow = r + 1, col = c - 1;\n\twhile (row &lt; N &amp;&amp; col &gt;= 0) {\n\t\tif (mat[row][col] == 1)\n\t\t\treturn false;\n\t\trow++; col--;\n\t}\n\t\/\/\n\treturn true;\n}\nvoid nQueensSub(int** mat, int N, int row, int col, vector&lt;vector&lt;string&gt;&gt;&amp; results) {\n\tif (row == N) {\n\t\t\/\/print\n\t\tvector&lt;string&gt; result;\n\t\tfor (int i = 0; i &lt; N; i++) {\n\t\t\tstring row;\n\t\t\tfor (int j = 0; j &lt; N; j++) {\n\t\t\t\tif (mat[i][j] == 0)\n\t\t\t\t\trow += '.';\n\t\t\t\telse\n\t\t\t\t\trow += 'Q';\n\t\t\t}\n\t\t\tresult.push_back(row);\n\t\t}\n\t\tresults.push_back(result);\n\t}\n\telse {\n\t\tif (checkQueen(mat, N, row, col)) {\n\t\t\t\/\/put queen here\n\t\t\tmat[row][col] = 1;\n\t\t\t\/\/try next row\n\t\t\tnQueensSub(mat, N, row + 1, 0, results);\n\t\t\tmat[row][col] = 0;\n\t\t}\n\t\t\/\/try next col\n\t\tif (col+1 &lt; N) {\n\t\t\tnQueensSub(mat, N, row, col + 1, results);\n\t\t}\n\t}\n}\n    vector&lt;vector&lt;string&gt;&gt; solveNQueens(int n) {\n\t    int** mat = new int*[n];\n\t    for (int i = 0; i &lt; n; i++) {\n\t    \tmat[i] = new int[n];\n\t    \tfor (int j = 0; j &lt; n; j++) {\n\t    \t\tmat[i][j] = 0;\n    \t\t}\n    \t}\n    \tvector&lt;vector&lt;string&gt;&gt; results;\n    \tnQueensSub(mat, n, 0, 0, results);\n    \tfor (int i = 0; i &lt; n; i++) {\n    \t\tdelete[]mat[i];\n    \t}\n    \tdelete[] mat;\n    \treturn results;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u516b\u7687\u540e\u95ee\u9898\u7684\u5ef6\u4f38\u7248\uff0c\u56de\u6eaf\u5b9e\u73b0\u3002 class Solution { public: bool checkQueen(int** mat, int N, int r, int c) { for (int row = 0; row &lt; N; row++) { if (row == r) continue; if (mat[row][c] == 1) return false; } \/\/ int row = r-1, col = c-1; while (row &gt;= 0 &amp;&amp; col &gt;= 0) { if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-2294","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2294"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2294\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}