[LeetCode] N-Queens
2015-07-01 21:16
573 查看
The idea is to use backtracking. In fact, the code below uses DFS, which involves backtracking in a recursive manner.
The idea is also very simple. Starting from the first row, try each column. If it does not induce any attack, move on to the next row based on the configurations of the previous rows. Otherwise, backtrack to the current row and try another selection of the column position. Once we reach the last row, add the current setting to a vector<vector<string> >.
The code below is referenced to this link, which records the positions of the queens using a nice 1d array like a[row] = col to indicate there is a queen at (row, col).
The code is as follows.
Well, if you have solved N-Queens II, you may know that problem has a nice bit-manipulation solution (you may refer to this passage). In fact, that bit-manipulation idea can also be used to solve this problem. The code is as follows.
The idea is also very simple. Starting from the first row, try each column. If it does not induce any attack, move on to the next row based on the configurations of the previous rows. Otherwise, backtrack to the current row and try another selection of the column position. Once we reach the last row, add the current setting to a vector<vector<string> >.
The code below is referenced to this link, which records the positions of the queens using a nice 1d array like a[row] = col to indicate there is a queen at (row, col).
The code is as follows.
class Solution { public: vector<vector<string>> solveNQueens(int n) { vector<vector<string> > queens; vector<int> colPos(n, 0); solve(colPos, n, 0, queens); return queens; } private: bool noAttack(vector<int>& colPos, int row, int col) { for (int r = row - 1, ld = col - 1, rd = col + 1; r >= 0; r--, ld--, rd++) if (colPos[r] == col || colPos[r] == ld || colPos[r] == rd) return false; return true; } vector<string> queenStr(vector<int>& colPos) { int n = colPos.size(); vector<string> queen(n, string(n, '.')); for (int i = 0; i < n; i++) queen[i][colPos[i]] = 'Q'; return queen; } void solve(vector<int>& colPos, int n, int row, vector<vector<string> >& queens) { if (row == n) { queens.push_back(queenStr(colPos)); return; } for (int col = 0; col < n; col++) { colPos[row] = col; if (noAttack(colPos, row, col)) solve(colPos, n, row + 1, queens); } } };
Well, if you have solved N-Queens II, you may know that problem has a nice bit-manipulation solution (you may refer to this passage). In fact, that bit-manipulation idea can also be used to solve this problem. The code is as follows.
class Solution { public: vector<vector<string>> solveNQueens(int n) { vector<vector<string> > queens; vector<string> queen; int limit = (1 << n) - 1; solve(0, 0, 0, n, limit, queen, queens); return queens; } private: void solve(int hProj, int lProj, int rProj, int n, int limit, vector<string>& queen, vector<vector<string> >& queens) { if (hProj == limit) { queens.push_back(queen); return; } int pos = ~(hProj | lProj | rProj); for (int i = 0; i < n; i++) { int p = (1 << i); if (pos & p) { string line(n ,'.'); line[i] = 'Q'; queen.push_back(line); solve(hProj | p, (lProj | p) << 1, (rProj | p) >> 1, n, limit, queen, queens); queen.pop_back(); } } } };
相关文章推荐
- UI基础:UITextField
- UI基础:UITextField 分类: iOS学习-UI 2015-07-01 21:07 68人阅读 评论(0) 收藏
- 自定义UIView整理(1)
- UICollectionView详解
- Majority Element
- UITextField的leftView不贴边处理
- Bioinformatics: Sequencing Antibiotics (week 3-4)
- leetcode N-Queens I && N-Queens II
- UI基础:UILabel.UIFont
- UI基础:UILabel.UIFont 分类: iOS学习-UI 2015-07-01 19:38 107人阅读 评论(0) 收藏
- DesignBuilder.Software.Ltd.DesignBuilder.v4.2.0.054 1CD建筑能耗动态模拟程序
- Repeated DNA Sequences -- leetcode
- iOS中使用UITableView加载数据项
- uitextField单词的方法和抖动的限制
- iOS控件UITextView 实现placeholder效果和解决UITableView键盘遮挡
- 如何更好地限制一个UITextField的输入长度
- SemanticException Column xx Found in more than One Tables/Subqueries
- requirejs测试
- request.getRealPath()方法
- UITextInputDelegate 的使用