LeetCode 52. N-Queens II
2016-05-16 08:56
585 查看
Get the idea from this post: https://developers.google.com/optimization/puzzles/queens
We only to track three arrays. One is horizontally, one is vertically, one is diagonally.
Horizontally is main[2 * n - 1], Vertically is col
, diagonally is anti[2 * n - 1];
We only to track three arrays. One is horizontally, one is vertically, one is diagonally.
Horizontally is main[2 * n - 1], Vertically is col
, diagonally is anti[2 * n - 1];
int totalNQueens(int n) { vector<bool> col(n, true); vector<bool> anti(2*n-1, true); vector<bool> main(2*n-1, true); int count = 0; dfs(0, col, main, anti, count); return count; } void dfs(int i, vector<bool> &col, vector<bool>& main, vector<bool> &anti, int &count) { if (i == col.size()) { count++; return; } for (int j = 0; j < col.size(); j++) { if (col[j] && main[i+j] && anti[i+col.size()-1-j]) { col[j] = main[i+j] = anti[i+col.size()-1-j] = false; dfs(i+1, col, main, anti, count); col[j] = main[i+j] = anti[i+col.size()-1-j] = true; } } }
相关文章推荐
- 定义一个UIEdgeInset全局变量
- 根据segue跳转
- 关于SqlSessionFactoryBuilder SqlSessionFactory SqlSession的作用范围
- Android官方开发文档Training系列课程中文版:高效显示位图之在UI中展示位图
- day65-Spark SQL下Parquet深入进阶
- day64 Spark SQL下Parquet的数据切分和压缩内幕详解
- LeetCode 304. Range Sum Query 2D - Immutable
- [PWA] 5. Hijacking one type of request
- 用UGUI简单实现Inventory案例
- java设计模式_________Builder模式
- 2016/05/16 UEditor 文本编辑器 使用教程与使用方法
- PHP问题 —— PDO no value
- alibaba druid mysql连接问题
- 1164 - Horrible Queries
- 使用Druid对数据库密码加密
- iOS UISearchBar 获取 cancelButton,UItextField
- 使用filterRules过滤ueditor的内容
- gradle:failed to find Build Tools revision 23.0.1
- priority queue 优化的 dijkstra 算法
- 请解释下在单线程模型中Message、Handler、MessageQueue、Looper之间的关系。