From a51f90c97d8f60bc4d4288e2d7e83ab2e4605b9f Mon Sep 17 00:00:00 2001 From: primetang Date: Wed, 12 Aug 2015 15:57:30 +0800 Subject: [PATCH] Update chapDFS.tex --- C++/chapDFS.tex | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/C++/chapDFS.tex b/C++/chapDFS.tex index 87c4c7bf..e53f7504 100644 --- a/C++/chapDFS.tex +++ b/C++/chapDFS.tex @@ -496,6 +496,38 @@ \subsubsection{代码} }; \end{Code} +\begin{Code} +class Solution { +public: + std::vector > solveNQueens(int n) { + std::vector > res; + std::vector nQueens(n, std::string(n, '.')); + /* + flag[0] to flag[n - 1] to indicate if the column had a queen before. + flag[n] to flag[3 * n - 2] to indicate if the 45° diagonal had a queen before. + flag[3 * n - 1] to flag[5 * n - 3] to indicate if the 135° diagonal had a queen before. + */ + std::vector flag(5 * n - 2, 1); + solveNQueens(res, nQueens, flag, 0, n); + return res; + } +private: + void solveNQueens(std::vector > &res, std::vector &nQueens, std::vector &flag, int row, int &n) { + if (row == n) { + res.push_back(nQueens); + return; + } + for (int col = 0; col != n; ++col) + if (flag[col] && flag[n + row + col] && flag[4 * n - 2 + col - row]) { + flag[col] = flag[n + row + col] = flag[4 * n - 2 + col - row] = 0; + nQueens[row][col] = 'Q'; + solveNQueens(res, nQueens, flag, row + 1, n); + nQueens[row][col] = '.'; + flag[col] = flag[n + row + col] = flag[4 * n - 2 + col - row] = 1; + } + } +}; +\end{Code} \subsubsection{相关题目} \begindot @@ -567,6 +599,28 @@ \subsubsection{代码} }; \end{Code} +\begin{Code} +class Solution { +public: + int totalNQueens(int n) { + std::vector flag(5 * n - 2, 1); + return totalNQueens(flag, 0, n); + } +private: + int totalNQueens(std::vector &flag, int row, int &n) { + if (row == n) + return 1; + int res = 0; + for (int col = 0; col != n; ++col) + if (flag[col] && flag[col + row + n] && flag[col - row + 4 * n - 2]) { + flag[col] = flag[col + row + n] = flag[col - row + 4 * n - 2] = 0; + res += totalNQueens(flag, row + 1, n); + flag[col] = flag[col + row + n] = flag[col - row + 4 * n - 2] = 1; + } + return res; + } +}; +\end{Code} \subsubsection{相关题目} \begindot