Title Link: N-queens
The N-queens puzzle is the problem of placing n queens in an NXN chessboard such, that no, and Queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the N-queens ' placement, where ' Q ' and '. ' Both indicate a queen and an empty space respectively.
For example,
There exist-distinct solutions to the 4-queens puzzle:
[ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."]]
The question asks for the N queen problem: The Queen of N is placed on the board of the n*n, so that any of its two queens cannot attack one another. Given the integer n, returns all possible cases where ' Q ' denotes the Queen, '. ' Represents an empty location.
Two Queens cannot attack each other, requiring the two queens not to be on the same line, the same column, and the same slash.
N Queen problem, a very classical problem, NP problem, the main idea is to use recursion to constantly deal with sub-problems, and the previous Sudoku Solver problem similar. Each time a 1 queen is placed on the current line on the board, it is necessary to enumerate all the cases in that row, and when it is not in conflict with the queen of the previous row, the remaining position can be recursively processed. When the current number of rows exceeds the last row, the result is saved and the recursion ends.
The key question is how to detect whether a column in a row is causing a conflict when the Queen is placed.
- You can use an array of n*n as a marker, and when you place a queen, the corresponding row, column, and Slash are set to not place the Queen state, so that when you drop a queen, you can directly determine whether the position can cause conflict. However, such spatial complexity is higher (O (N2)).
- In fact, since each row is allowed to place only 1 queens, it is possible to record the position of the Queen in each row with 1 shaped arrays of length n, so that the space complexity is reduced to O (n). At this point, it is necessary to traverse the previous queen before checking whether the Queen in the current position is in conflict with the previous queen. Because the Queen is not on the same line because it is recursive by row, you only need to detect if the values of the columns are the same, and the absolute value of the row and column differences is the same (judging if the same slash).
Time complexity: O (???)
Space complexity: O (N)
1 class Solution2 {3 Vector<Vector<string> > VVS;4 5 Public:6 Vector<Vector<string> > Solvenqueens(int N)7 {8 Vector<int> VI(N);9 Solvenqueens(VI, N, 0);Ten return VVS; One } A - Private: - void Solvenqueens(Vector<int> &VI, int N, int C) the { - if(C == N) - { - Vector<string> vs(N, string(N, '. ')); + for(int I = 0; I < N; ++ I) - vs[I][VI[I]] = ' Q '; + VVS.push_back(vs); A return; at } - - for(VI[C] = 0; VI[C] < N; ++ VI[C]) - if(Safe(VI, N, C)) - Solvenqueens(VI, N, C + 1); - } in - BOOL Safe(Vector<int> &VI, int N, int C) to { + for(int I = 0; I < C; ++ I) - if(VI[I] == VI[C] || ABS(VI[C] - VI[I]) == ABS(C - I)) the return false; * return true; $ }Panax Notoginseng };
Reprint please indicate source: Leetcode---51. N-queens
Leetcode---51. N-queens