description:
八皇后
Note:Example:
Input: 4Output: 2Explanation: There are two distinct solutions to the 4-queens puzzle as shown below.[ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."]]
answer:
class Solution {public: int totalNQueens(int n) { int res = 0; vector pos(n, -1); helper(pos, 0, res); return res; } void helper(vector & pos, int row, int& res) { int n = pos.size(); if (row == n) ++res; for (int col = 0; col < n; ++col) { if (isValid(pos, row, col)) { pos[row] = col; helper(pos, row + 1, res); pos[row] = -1; } } } bool isValid(vector & pos, int row, int col) { for (int i = 0; i < row; ++i) { if (col == pos[i] || abs(row - i) == abs(col - pos[i])) { return false; } } return true; }};
relative point get√:
hint :
和上面那个题一样