{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "3716ea4367754f8f959faa56b01429e1",
  "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": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint totalNQueens(int n)\n\t{\n\t\tvector<int> stack(n);\n\t\treturn dfs(n, 0, stack);\n\t}\nprivate:\n\tint dfs(int n, int row, vector<int> &stack)\n\t{\n\t\tint count = 0;\n\t\tif (row == n)\n\t\t{\n\t\t\treturn count + 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (int i = 0; i < n; i++)\n\t\t\t{\n\t\t\t\tif (row == 0 || !conflict(stack, row, i))\n\t\t\t\t{\n\t\t\t\t\tstack[row] = i;\n\t\t\t\t\tcount += dfs(n, row + 1, stack);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn count;\n\t\t}\n\t}\n\tbool conflict(vector<int> &stack, int row, int col)\n\t{\n\t\tfor (int i = 0; i < row; i++)\n\t\t{\n\t\t\tif (col == stack[i] || abs(row - i) == abs(col - stack[i]))\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-4522f669896d4e32bae4b5a741d160ad",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470922"
}