solution.md 5.7 KB
Newer Older
1
# N皇后 II
F
fix bug  
feilong 已提交
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<div class="notranslate">
    <p><strong>n&nbsp;皇后问题</strong> 研究的是如何将 <code>n</code>&nbsp;个皇后放置在 <code>n×n</code> 的棋盘上,并且使皇后彼此之间不能相互攻击。</p>

    <p>给你一个整数 <code>n</code> ,返回 <strong>n 皇后问题</strong> 不同的解决方案的数量。</p>

    <p>&nbsp;</p>

    <div class="original__bRMd">
        <div>
            <p><strong>示例 1:</strong></p>
            <img style="width: 600px; height: 268px;" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg"
                alt="">
            <pre><strong>输入:</strong>n = 4
<strong><br />输出:</strong>2
<strong><br />解释:</strong>如上图所示,4 皇后问题存在两个不同的解法。
    </pre>

            <p><strong>示例 2:</strong></p>

            <pre><strong>输入:</strong>n = 1
<strong><br />输出:</strong>1
    </pre>

            <p>&nbsp;</p>

            <p><strong>提示:</strong></p>

            <ul>
                <li><code>1 &lt;= n &lt;= 9</code></li>
                <li>皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。</li>
            </ul>
        </div>
    </div>
</div>
每日一练社区's avatar
每日一练社区 已提交
37
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
38

39
## aop
F
fix bug  
feilong 已提交
40

41
### before
F
fix bug  
feilong 已提交
42

43 44 45 46
```cpp
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
47

48
### after
F
fix bug  
feilong 已提交
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63
```cpp
int main()
{
    Solution sol;
    int n = 4;
    int res;

    res = sol.totalNQueens(n);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
```cpp
class Solution
{
public:
    int totalNQueens(int n)
    {
        int res = 0;
        int queenPos[100];
        NQueen(res, n, 0, queenPos);
        return res;
    }
    void NQueen(int &res, int N, int k, int queenPos[])
    {
        int i;
        if (k == N)
        {
            res++;
            return;
        }
        for (i = 0; i < N; i++)
        {
            int j;
            for (j = 0; j < k; j++)
            {
                if (queenPos[j] == i || abs(queenPos[j] - i) == abs(k - j))
                {
                    break;
                }
            }
            if (j == k)
            {
                queenPos[k] = i;
                NQueen(res, N, k, queenPos);
            }
        }
    }
};

```
## 选项

F
fix bug  
feilong 已提交
106

107
### A
F
fix bug  
feilong 已提交
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
```cpp
class Solution
{
public:
    int totalNQueens(int n)
    {
        vector<int> stack(n);
        return dfs(n, 0, stack);
    }

private:
    int dfs(int n, int row, vector<int> &stack)
    {
        int count = 0;
        if (row == n)
        {
            return count + 1;
        }
        else
        {
            for (int i = 0; i < n; i++)
            {
                if (row == 0 || !conflict(stack, row, i))
                {
                    stack[row] = i;
                    count += dfs(n, row + 1, stack);
                }
            }
            return count;
        }
    }
    bool conflict(vector<int> &stack, int row, int col)
    {
        for (int i = 0; i < row; i++)
        {
            if (col == stack[i] || abs(row - i) == abs(col - stack[i]))
            {
                return true;
            }
        }
        return false;
    }
};
```

### B
F
fix bug  
feilong 已提交
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
```cpp
class Solution
{
public:
    void recurse(vector<string> solution, int pos, vector<vector<bool>> validPos, int &result)
    {
        int n = solution[0].size();
        if (pos == n)
        {
            result++;
            return;
        }

        for (int i = 0; i < n; i++)
        {
            if (!validPos[pos][i])
                continue;

            vector<vector<bool>> newPos = validPos;
            for (int j = pos; j < n; j++)
            {
                newPos[j][i] = false;
                if (i - j + pos >= 0)
                    newPos[j][i - j + pos] = false;
                if (i + j - pos < n)
                    newPos[j][i + j - pos] = false;
            }
            solution[pos][i] = 'Q';

            recurse(solution, pos + 1, newPos, result);
            solution[pos][i] = '.';
        }

        return;
    }

    int totalNQueens(int n)
    {
        int result = 0;
        vector<string> solution(n, string(n, '.'));
        vector<vector<bool>> validPos = vector<vector<bool>>(n, vector<bool>(n, true));

        recurse(solution, 0, validPos, result);

        return result;
    }
};
```

### C
F
fix bug  
feilong 已提交
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
```cpp
class Solution
{
public:
    bool isvalid(vector<string> &temp, int i, int j)
    { //判断棋盘是否有效
        //for (int k = 0; k<temp[i].size(); ++k){//判断行。不用判断行了,每行放一个之后就会递归到下一行了
        //	if (temp[i][k] == 'Q') return false;
        //}
        for (int k = 0; k < i; ++k)
        { //判断列
            if (temp[k][j] == 'Q')
                return false;
        }
        for (int p = i - 1, q = j - 1; p >= 0 && q >= 0; --p, --q)
        { //判断左上对角线
            if (temp[p][q] == 'Q')
                return false;
        }
        for (int p = i - 1, q = j + 1; p >= 0 && q < temp.size(); --p, ++q)
        { //判断右上对角线
            if (temp[p][q] == 'Q')
                return false;
        }
        return true;
    }

    int dfs(int &count, vector<string> &temp, int i, int n)
    {
        if (i == n)
            return ++count;

        for (int j = 0; j < n; ++j)
        {
            if (isvalid(temp, i, j))
            {
                temp[i][j] = 'Q'; //递归前修改
                dfs(count, temp, i + 1, n);
            }
            temp[i][j] = '.'; //递归后恢复
        }
        return count;
    }

    int totalNQueens(int n)
    {
        int count = 0;
        string aa;
        for (int i = 0; i < n; ++i)
            aa += '.';
        vector<string> temp(n, aa);
        return dfs(count, temp, 0, n);
    }
};
```