leetcode 52. N-Queens II
2016-04-07 21:19
375 查看
题意
N皇后问题题解
使用dfs。代码
class Solution { public: int result; bool isOK(vector<int> cols, int pos) { for(int i = 0; i < pos; i++) { if(cols[i] == cols[pos] || abs(cols[i] - cols[pos]) == abs(i - pos)) return false; } return true; } void dfs(vector<int> cols, int pos, int n) { if(pos == n) { result++; } else { for(cols[pos] = 0; cols[pos] < n; cols[pos]++) { if(isOK(cols, pos)) dfs(cols, pos + 1, n); } } } int totalNQueens(int n) { result = 0; vector<int> cols(n); dfs(cols, 0, n); return result; } };
相关文章推荐
- atitit.atiHtmlUi web组件化方案与规范v1
- atitit.atiHtmlUi web组件化方案与规范v1
- atitit.atiHtmlUi web组件化方案与规范v1
- Atitit.web ui 组件化 vs mvc
- Atitit.web ui 组件化 vs mvc
- ionic $resource.query 访问,Access-Control-Allow-Origin问题
- Atitit.web ui 组件化 vs mvc
- 根据Request获取真实客户端IP
- 杭电5538House Building
- 05_斐波那契数列(Fibonacci sequence)
- DuiLib(9)——关于字体、html及默认属性
- 不要随便在UI界面SystemClock.sleep,会造成卡顿
- UICollectionView.h文件
- DuiLib库
- J2EE SDK安装报错could not find the required version of the Java(TM)2 Runtime Environment in '(null)'
- UI之FMDataBase
- IOS 开发-UI初级 (二 · 后续)View,ViewController实践和应用
- Google Chrome源码剖析【四】:UI绘制
- DuiLib(8)——Menu菜单的实现方式
- Android Bluetooth 框架简读 <1>