博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] N-Queens II
阅读量:4493 次
发布时间:2019-06-08

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

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

 

思路:和 N-queens 一样,不过只统计个数而言,更加简单了

class Solution {    vector
x; int m_res; bool checkTwoPoints(int i, int j, int xi, int xj) { //cout << "check i\t" << i << endl; //cout << "check j\t" << j << endl; if(xi == xj) // same column return false; if( abs(xi-xj) == abs(i-j)) // diag return false; return true; } bool check(vector
x, int n) // check x[n] and x[0 .. n-1] { for(int i = 0; i < n; i++) { if(!checkTwoPoints(i, n, x[i], x[n])) return false; } return true; } void dfs(int n) { if(n == x.size() ) { //printVector(x); m_res ++; return; } for(int i = 0; i < x.size(); i++) { x[n] = i; // check if x[n] is available if(check(x, n)) dfs(n+1); } } public: int totalNQueens(int n) { x.resize(n); m_res = 0; dfs(0); return m_res; }};

 

转载于:https://www.cnblogs.com/diegodu/p/4313778.html

你可能感兴趣的文章
openstack云主机硬盘复制查询
查看>>
写个神经网络,让她认得我`(๑•ᴗ•๑)(Tensorflow,opencv,dlib,cnn,人脸识别)
查看>>
《程序是怎样跑起来的》第三章
查看>>
Jquery回到顶部效果
查看>>
开园第一笔
查看>>
Spark项目之电商用户行为分析大数据平台之(七)数据调研--基本数据结构介绍...
查看>>
原来fb可以在一个工程里面输出多个swf模块
查看>>
Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
查看>>
Codeforces Round #FF (Div. 2) D. DZY Loves Modification 优先队列
查看>>
【学习】logger
查看>>
Delphi APP 開發入門(十)REST Client 開發
查看>>
elk
查看>>
.net 模糊匹配路径
查看>>
用包来组织模型
查看>>
ORA-29857: 表空间中存在域索引和/或次级对象
查看>>
LeetCode58 Length of Last Word
查看>>
Python基础语法 系统学习
查看>>
推荐15款好用的JS开发工具
查看>>
ios开发之数据的持久化存储机制
查看>>
poj 3264
查看>>