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. 实战小项目之ffmpeg推流yolo视频实时检测

    之前实现了yolo图像的在线检测,这次主要完成远程视频的检测.主要包括推流--収流--检测显示三大部分 首先说一下推流,主要使用ffmpeg命令进行本地摄像头的推流,为了实现首屏秒开使用-g设置gop ...

  2. POJ 1921 Paper Cut(计算几何の折纸问题)

    Description Still remember those games we played in our childhood? Folding and cutting paper must be ...

  3. wpa_supplicant下行接口浅析

    wpa_supplicant通过socket通信机制实现下行接口,与内核进行通信,获取信息或下发命令. 以下摘自http://blog.csdn.net/fxfzz/article/details/6 ...

  4. 《剑指Offer》题五十一~题六十

    五十一.数组中的逆序对 题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数.例如,在数组{7, 5, 6, 4}中,一共存 ...

  5. Crawling is going on - Beta版本测试报告

    [Crawling is going on - Beta版本] 测试报告 文件状态: [] 草稿 [√] 正式发布 [] 正在修改 报告编号: 当前版本: 2.0.2 编写人: 周萱.刘昊岩.居玉皓 ...

  6. Java容器之Collections

    Collections 类来源于 java.util.Collections,从 java.lang.object继承. 此类完全由在 collection 上进行操作或返回 collection 的 ...

  7. lintcode-179-更新二进制位

    179-更新二进制位 给出两个32位的整数N和M,以及两个二进制位的位置i和j.写一个方法来使得N中的第i到j位等于M(M会是N中从第i为开始到第j位的子串) 注意事项 In the function ...

  8. MVC4 DropDownList (二) — 省市联动

    1.添加省份和城市类 //省份 public class Province { public int Id { get; set; } public string Name { get; set; } ...

  9. <T extends Comparable<? super T>>

    在看Collections源代码中,看到如下代码: public static <T extends Comparable<? super T>> void sort(List ...

  10. 伟大的淘宝IP库的API接口竟然提示503挂掉了

    1 淘宝IP库惊现503错误 吃完晚饭,大概6点半了,天色已暗,太阳早就落山了.回到宿舍打开博客一看,傻眼了:博客每篇文章的评论者的地理信息全部处于“正在查询中……”的状态.这神马情况,不会是被淘宝封 ...