{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "6d30912d9be04c818e000311290651d0",
  "keywords": "回溯",
  "title": "N皇后 II",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p><strong>n&nbsp;皇后问题</strong> 研究的是如何将 <code>n</code>&nbsp;个皇后放置在 <code>n×n</code> 的棋盘上，并且使皇后彼此之间不能相互攻击。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>给你一个整数 <code>n</code> ，返回 <strong>n 皇后问题</strong> 不同的解决方案的数量。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<div class=\"original__bRMd\">\n<div>\n<p><strong>示例 1：</strong></p>\n<img style=\"width: 600px; height: 268px;\" src=\"https://assets.leetcode.com/uploads/2020/11/13/queens.jpg\"\nalt=\"\">\n<pre><strong>输入：</strong>n = 4\n<strong>输出：</strong>2\n<strong>解释：</strong>如上图所示，4 皇后问题存在两个不同的解法。\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 2：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>n = 1\n<strong>输出：</strong>1\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>1 &lt;= n &lt;= 9</code></li>\n<li>皇后彼此不能相互攻击，也就是说：任何两个皇后都不能处于同一条横行、纵行或斜线上。</li>\n</ul>\n</div>\n</div>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tprivate boolean col[];\n\tprivate boolean dia1[];\n\tprivate boolean dia2[];\n\tpublic int totalNQueens(int n) {\n\t\tcol = new boolean[n];\n\t\tdia1 = new boolean[2 * n - 1];\n\t\tdia2 = new boolean[2 * n - 1];\n\t\treturn putQueen(n, 0);\n\t}\n\tprivate int putQueen(int n, int index) {\n\t\tint res = 0;\n\t\tif (index == n) {\n\t\t\treturn 1;\n\t\t}\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tif (!col[i] && !dia1[i - index + n - 1] && !dia2[i + index]) {\n\t\t\t\tcol[i] = true;\n\t\t\t\tdia1[i - index + n - 1] = true;\n\t\t\t\tdia2[i + index] = true;\n\t\t\t\tres += putQueen(n, index + 1);\n\t\t\t\tcol[i] = false;\n\t\t\t\tdia1[i - index + n - 1] = false;\n\t\t\t\tdia2[i + index] = false;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-71a1af0422c54a1a9fa6959b4dcb04fc",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470922"
}