题目:
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
程序:
class Solution {
public:
int count = 0;
int x[9];
int totalNQueens(int n) {
backtrack(1,n);
return count;
}
void backtrack(int t,int n)
{
if(t>n)
count++;
else{
for(int i=1;i<=n;i++)
{
x[t] = i;
if(place(t))
backtrack(t+1,n);
}
}
}
bool place(int t)
{
for(int i=1;i<t;i++)
if(abs(x[t]-x[i]) == abs(t-i) || x[t] == x[i])
return false;
return true;
}
};
点评:
回溯,递归