Description

Follow up for N-Queens problem.

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

思路

  • 递归方式
  • 主要是要判断当前位置是否是合理位置,参考isValid函数

代码

class Solution {
public:
int totalNQueens(int n) {
int res = 0;
if(n == 0) return 0; vector<string> flag(n, string(n, '.'));
getResult(flag, n, 0, res); return res;
} void getResult(vector<string> &flag, int n, int rows, int &res){
if(rows == n){
res++;
return;
}
else{
for(int i = 0; i < n; ++i){
if(isValid(flag, rows, i, n)){
flag[rows][i] = 'Q';
getResult(flag, n, rows + 1, res);
flag[rows][i] = '.';
}
}
}
}
bool isValid(vector<string> &nums, int rows, int cols, int n){
for(int i = 0; i < rows; ++i)
if(nums[i][cols] == 'Q') return false; for(int i = rows - 1, j = cols - 1; i >= 0 && j >= 0; --i, --j)
if(nums[i][j] == 'Q') return false; for(int i = rows - 1, j = cols + 1; i >= 0 && j < n; --i, ++j)
if(nums[i][j] == 'Q') return false; return true;
}
};

Leetcode:52. N-QueensII的更多相关文章

  1. [LeetCode] 52. N-Queens II N皇后问题之二

    The n-queens puzzle is the problem of placing nqueens on an n×n chessboard such that no two queens a ...

  2. LeetCode - 52. N-Queens II

    52. N-Queens II Problem's Link --------------------------------------------------------------------- ...

  3. Leetcode 52 N-Queens II 回溯搜索

    对于N-Queens的每种情况,回答出每种情况的N-Queens的排列数. l,r和c是每种类型的格子是否有棋子. l判断的是这样的对角线的格子                   r判断的是这样的对 ...

  4. [LeetCode] 52. N-Queens II N皇后问题 II

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  5. Java实现 LeetCode 52 N皇后 II

    52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...

  6. LeetCode(52)-Remove Linked List Elements

    题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 --> ...

  7. [leetcode]52. N-Queens II N皇后

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  8. Leetcode 52

    //N皇后的基础上改了一点class Solution { public: int totalNQueens(int n) { ; vector<); DFS(pos,,res); return ...

  9. [LeetCode] 52. N皇后 II

    题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...

随机推荐

  1. 创建https证书

    第一个里程碑:创建https证书 创建文件认证目录 mkdir /application/nginx/key/ -p 在认证目录下创建认证文件 openssl req -new -x509 -node ...

  2. Linux 添加虚拟网卡

    使用的Linux版本是Centos 7: [root@vnode33 bin]# cat /etc/redhat-release CentOS Linux release (Core) 使用ifcon ...

  3. php常用方法汇总

    xml格式转成array <?php $str='<xml><node><![CDATA[content]]></node></xml> ...

  4. ViewPager的简单使用说明

    前提:工程中使用ViewPager,需要导入google提供的jar包(android-support-v4.jar). 要学习ViewPager的使用,建议直接看官方文档 Creating Swip ...

  5. 11.22Daily Scrum(2)

    人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.984 数据库测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.985 实现视频浏览的功能 王 ...

  6. lintcode-153-数字组合 II

    153-数字组合 II 给出一组候选数字(C)和目标数字(T),找出C中所有的组合,使组合中数字的和为T.C中每个数字在每个组合中只能使用一次. 注意事项 所有的数字(包括目标数字)均为正整数. 元素 ...

  7. LintCode-381.螺旋矩阵 II

    螺旋矩阵 II 给你一个数n生成一个包含1-n^2的螺旋形矩阵 样例 n = 3 矩阵为 [     [ 1, 2, 3 ],     [ 8, 9, 4 ],     [ 7, 6, 5 ] ] 标 ...

  8. 转Web开发的发展史---Web开发技术的演变

    转自:http://blog.csdn.net/zzzkk2009/article/details/9849431 在接下来的几个月时间里,我打算写一系列关于完整web开发的文章.这第一篇文章虽然有所 ...

  9. 异步请求Python库 grequests的应用和与requests库的响应速度的比较

    requests库是python一个优秀的HTTP库,使用它可以非常简单地执行HTTP的各种操作,例如GET.POST等.不过,这个库所执行的网络请求都是同步了,即cpu发出请求指令后,IO执行发送和 ...

  10. WPF 分页控件的实现 -用户控件

    效果图: