Skip to content

Commit f7b2099

Browse files
committed
Runtime: 7 ms (Top 64.18%) | Memory: 6 MB (Top 83.58%)
1 parent 8d03e93 commit f7b2099

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
// Runtime: 7 ms (Top 64.18%) | Memory: 6 MB (Top 83.58%)
12
class Solution {
23
public:
34
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);
67
}
7-
8+
89
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;
1011
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;
1819
}
19-
};
20+
};

0 commit comments

Comments
 (0)