LeetCode_N-Queens II

时间:2023-03-09 22:24:05
LeetCode_N-Queens II
Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

  LeetCode_N-Queens II

class Solution {
public:
bool isValid(int* row, int curRow)
{
for(int i = ; i< curRow; i++)
if(row[i] == row[curRow] || abs(row[i] - row[curRow]) == curRow - i)
return false;
return true;
}
void nqueue(int* row,int curRow)
{
if(curRow == n)
{
res++;
return ;
}
for(int i = ; i< n ;i++)
{
row[curRow] = i;
if(isValid(row,curRow))
nqueue(row,curRow+);
}
}
int totalNQueens(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res = ;
if( n < ) return res;
this->n = n;
int *row = new int[n];
nqueue(row, );
return res;
}
private:
int n;
int res;
};