博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
N-Queens II 输出可行解数量
阅读量:4107 次
发布时间:2019-05-25

本文共 889 字,大约阅读时间需要 2 分钟。

题目:

解答:

输出可行解个数。参见上一篇博客。

代码:

class Solution {  public:	  int totalNQueens(int n) {		  int res = 0;		  vector
temp; search(0, n, temp, res); return res; } private: void search(int level, int n, vector
&temp, int &res) { if (level == n) { ++res; return; } string str (n, '.'); for(int i = 0; i < n; i++) { str[i] = 'Q'; if (judge(i, temp)) { temp.push_back(str); search(level + 1, n, temp, res); temp.pop_back(); } str[i] = '.'; } } bool judge(int pos, vector
&temp) { for (int i = 0; i < temp.size(); i++) { if (temp[i][pos] == 'Q') { return false; } for (int j = 0; j < temp[i].length(); j++) { if (temp[i][j] == 'Q') { if (abs(int(temp.size()) - i) == abs(j - pos)) return false; } } } return true; } };

转载地址:http://oytsi.baihongyu.com/

你可能感兴趣的文章
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(五):OpenFeign请求结果处理及重试控制
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
OpenFeign学习(七):Spring Cloud OpenFeign的使用
查看>>
Ribbon 学习(二):Spring Cloud Ribbon 加载配置原理
查看>>
Ribbon 学习(三):RestTemplate 请求负载流程解析
查看>>
深入理解HashMap
查看>>
XML生成(一):DOM生成XML
查看>>
XML生成(三):JDOM生成
查看>>
Ubuntu Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied)
查看>>
collect2: ld returned 1 exit status
查看>>
C#入门
查看>>
查找最大值最小值
查看>>
杨辉三角
查看>>
冒泡排序法
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
16、Memento 备忘录模式
查看>>
Java基础篇(一)
查看>>
数据库
查看>>
mysql update与group by
查看>>