A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 52. N-Queens II, the solution in this repository is mainly a backtracking solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: backtracking.
The notes already sitting in the source point us in the right direction:
- Backtracking
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are backtrack, totalNQueens, used_col, used_pos_diag, used_neg_diag.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Backtracking
02//Runtime: 8 ms, faster than 52.40% of C++ online submissions for N-Queens II.
03//Memory Usage: 6 MB, less than 84.99% of C++ online submissions for N-Queens II.
04class Solution {
05public:
06 int n;
07
08 void backtrack(vector<bool>& used_col, vector<bool>& used_pos_diag,
09 vector<bool>& used_neg_diag, int r, int& ans){
10 if(r == n){
11 ++ans;
12 }else{
13 for(int c = 0; c < n; ++c){
14 if(!used_col[c] && !used_pos_diag[r-c+n-1] && !used_neg_diag[r+c]){
15 used_col[c] = true;
16 used_pos_diag[r-c+n-1] = true;
17 used_neg_diag[r+c] = true;
18
19 backtrack(used_col, used_pos_diag, used_neg_diag, r+1, ans);
20
21 used_col[c] = false;
22 used_pos_diag[r-c+n-1] = false;
23 used_neg_diag[r+c] = false;
24 }
25 }
26 }
27 };
28
29 int totalNQueens(int n) {
30 this->n = n;
31 int ans = 0;
32 vector<bool> used_col(n, false);
33 vector<bool> used_pos_diag(2*n-1, false);
34 vector<bool> used_neg_diag(2*n-1, false);
35
36 backtrack(used_col, used_pos_diag, used_neg_diag, 0, ans);
37
38 return ans;
39 }
40};
41
42//+speed up, vector<bool> -> vector<int>, build ans on the fly
43//Runtime: 0 ms, faster than 100.00% of C++ online submissions for N-Queens II.
44//Memory Usage: 6.2 MB, less than 56.45% of C++ online submissions for N-Queens II.
45class Solution {
46public:
47 int n;
48
49 void backtrack(vector<int>& used, int r, int& ans){
50 if(r == n){
51 ++ans;
52 }else{
53 for(int c = 0; c < n; ++c){
54 if(!used[c] && !used[n+r-c+n-1] && !used[n+2*n-1+r+c]){
55 used[c] = used[n+r-c+n-1] = used[n+2*n-1+r+c] = true;
56
57 backtrack(used, r+1, ans);
58
59 used[c] = used[n+r-c+n-1] = used[n+2*n-1+r+c] = false;
60 }
61 }
62 }
63 };
64
65 int totalNQueens(int n) {
66 this->n = n;
67 int ans = 0;
68 vector<int> used(n + 2*n-1 + 2*n-1, false);
69
70 backtrack(used, 0, ans);
71
72 return ans;
73 }
74};
Cost