leetcode[52]N-Queens II
6098 ワード
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
Now, instead outputting board configurations, return the total number of distinct solutions.
class Solution {
public:
void init(vector<string> &temp, int n)
{
string strtemp(n,'.');
temp.insert(temp.end(),n,strtemp);
return;
}
bool checkij(vector<string> &temp, int i, int j)
{
for (int ii=i-1,jleft=j-1;ii>=0&&jleft>=0;ii--,jleft--)
{
if(temp[ii][jleft]=='Q')return false;
}
for (int ii=i-1,jright=j+1;ii>=0&&jright<temp.size();ii--,jright++)
{
if(temp[ii][jright]=='Q')return false;
}
for (int k=0;k<temp.size();k++)
{
if (k!=j&&temp[i][k]=='Q')return false;
if (k!=i&&temp[k][j]=='Q')return false;
}
return true;
}
bool solveOne(int &res,vector<string> &temp,int n, int index)
{
if(index==n)
{
res++;
return true;
}
for (int j=0;j<n;j++)
{
temp[index][j]='Q';
if (checkij(temp,index,j))
{
solveOne(res,temp,n,index+1);
}
temp[index][j]='.';
}
}
int totalNQueens(int n) {
int res=0;
vector<string> temp;
init(temp,n);
for(int i=0;i<n;i++)
{
temp[0][i]='Q';
solveOne(res,temp,n,1);
temp[0][i]='.';
}
return res;
}
};