1
+ // Runtime: 7 ms (Top 64.18%) | Memory: 6 MB (Top 83.58%)
1
2
class Solution {
2
3
public:
3
4
int totalNQueens (int n) {
4
- vector<bool > col (n), diag (2 *n-1 ), anti_diag (2 *n-1 );
5
- return solve (col, diag, anti_diag, 0 );
5
+ vector<bool > col (n), diag (2 *n-1 ), anti_diag (2 *n-1 );
6
+ return solve (col, diag, anti_diag, 0 );
6
7
}
7
-
8
+
8
9
int solve (vector<bool >& col, vector<bool >& diag, vector<bool >& anti_diag, int row) {
9
- int n = size (col), count = 0 ;
10
+ int n = size (col), count = 0 ;
10
11
if (row == n) return 1 ;
11
- for (int column = 0 ; column < n; column++)
12
- if (!col[column] && !diag[row + column] && !anti_diag[row - column + n - 1 ]){
13
- col[column] = diag[row + column] = anti_diag[row - column + n - 1 ] = true ;
14
- count += solve (col, diag, anti_diag, row + 1 );
15
- col[column] = diag[row + column] = anti_diag[row - column + n - 1 ] = false ;
16
- }
17
- return count;
12
+ for (int column = 0 ; column < n; column++)
13
+ if (!col[column] && !diag[row + column] && !anti_diag[row - column + n - 1 ]){
14
+ col[column] = diag[row + column] = anti_diag[row - column + n - 1 ] = true ;
15
+ count += solve (col, diag, anti_diag, row + 1 );
16
+ col[column] = diag[row + column] = anti_diag[row - column + n - 1 ] = false ;
17
+ }
18
+ return count;
18
19
}
19
- };
20
+ };
0 commit comments