题目:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201207194544758.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjcyMTQxMg==,size_16,color_FFFFFF,t_70)
分析:
参考视频
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201207195740580.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjcyMTQxMg==,size_16,color_FFFFFF,t_70)
代码:
class Solution {
public:
int ans=0;
void f(int n,int h,int r1,int r2,int r3)
{
if(h==n)
{
ans++;
return ;
}
for(int i=0;i<n;i++)
{
int c=2<<(i+1);
if(!(c&r1) && !(c&r2) && !(c&r3))
{
f(n,h+1,(c|r1),((c|r2)<<1),((c|r3)>>1));
}
}
}
int totalNQueens(int n) {
f(n,0,0,0,0);
return ans;
}
};