Leetcode -- N-Queens II
2015-10-27 15:19
471 查看
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
分析:
典型的回溯法题目。
Now, instead outputting board configurations, return the total number of distinct solutions.
分析:
典型的回溯法题目。
class Solution { public: int cnt=0; bool check(vector<int> pos,int r,int p) { for(int i=0;i<r;++i) if(pos[i]==p||abs(i-r)==abs(pos[i]-p)) return 0; return 1; } void fun(vector<int> pos,int r) { if(r==pos.size()) { cnt++; return; } for(int i=0;i<pos.size();++i) { if(check(pos,r,i)) { pos[r]=i; fun(pos,r+1); } } } int totalNQueens(int n) { vector<int> pos(n,0); fun(pos,0); return cnt; } };
相关文章推荐
- UITableView详解
- 滚动UIScrollView中子视图(按钮),实现 UIScrollView 滚动效果
- Volley内部UI层回调的实现代码
- 用Picasso的RequestHandler进行自定义加载的方法
- PowerDesigner15在生成SQL时报错Generation aborted due to errors detected during the verification of the mod
- UILabel使用技巧
- HDU 1815 Building roads
- ubuntu sublime text 3 build 3083 license
- 个人对StringBuffer和StringBuilder的使用心得
- NSInteger NSUInteger NSNumber
- iOS UITableView的下拉刷新
- Uicc之UiccCard(转)
- iOS7以后UITextView 技巧
- Subquery typo with using in(转)
- 【iOS】UITableView的上拉加载更多和下拉刷新原理
- iOS---UIImage
- HDU 1242 rescue and 优先级队列的条目
- Android UI学习——ListView 和Adapter
- 解决android stuio首次安装报错无法打开问题。
- Burp Suite使用介绍(一)