您的位置:首页 > 其它

Leetcode no. 52

2016-04-14 16:48 447 查看
52. N-Queens II

Follow up for N-Queens problem.

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



public class Solution {
int count=0;
public int totalNQueens(int n){
solveNQueens(0, 0, n, new int
);
return count;
}
private void solveNQueens(int row, int col, int n, int[] column){
if (row == n)
count++;
else{
for (int i = col; i < n; i++) {
column[row]= i;
boolean f= true;
for (int r = row-1, lc= i-1, rc= i+1; r>=0 ; r--,lc--,rc++ ) {
if (column[r]== i || column[r]==lc || column[r]==rc) f= false;
}
if (f) solveNQueens(row+1, 0, n, column);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: