跟前面的N皇后问题没区别,还更简单

 #include "000库函数.h"

 //使用回溯法
class Solution {
public:
int totalNQueens(int n) {
int res = ;
vector<int>x(n, -);//标记
NQueue(x, res, );
return res;
} void NQueue(vector<int>&x, int &num, int row) {
int n = x.size();
if (n == row)
num++;
for (int col = ; col < n; ++col)
if (Danger(x, row, col)) {
x[row] = col;
NQueue(x, num, row + );
x[row] = -;//回溯
}
} bool Danger(vector<int>x, int row, int col) {
for (int i = ; i < row; ++i)
if (col == x[i] || abs(row - i) == abs(x[i] - col))
//行列与斜边
return false;
return true;
}
}; void T052() {
Solution s;
cout << s.totalNQueens() << endl;
cout << s.totalNQueens() << endl;
}

力扣算法题—052N皇后问题2的更多相关文章

  1. 力扣算法题—051N皇后问题

    #include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...

  2. 力扣算法题—069x的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  3. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  4. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  5. 力扣算法题—147Insertion_Sort_List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  6. 力扣算法题—093复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...

  7. 力扣算法题—079单词搜索【DFS】

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...

  8. 力扣算法题—143ReorderList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...

  9. 力扣算法题—144Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

随机推荐

  1. 高负载集群实战之lvs负载均衡-技术流ken

    lvs简介 LVS的英文全称是Linux Virtual Server,即Linux虚拟服务器. 特点 跨平台:window,linux 作用 实现负载均衡 核心组件 ip_vs:linux的内核功能 ...

  2. [转]Docker学习之四:使用docker安装mysql

    本文转自:https://blog.csdn.net/qq_19348391/article/details/82998391 Docker学习之一:注册Docker Hub账号 Docker学习之二 ...

  3. 第一册:lesson forty

    原文: Penny's bag. A:Is that bag heavy,Penny? B:Not very. A:Here. Put it on this chair. What's in it? ...

  4. 第一册:lesson thirty seven。

    原文: Making a bookcase. A:You are working hard,George. What are you doing . B:I am making a bookcase. ...

  5. 行为型---状态者模式(State Pattern)

    状态者模式的介绍 每个对象都有其对应的状态,而每个状态又对应一些相应的行为,如果某个对象有多个状态时,那么就会对应很多的行为.那么对这些状态的判断和根据状态完成的行为,就会导致多重条件语句,并且如果添 ...

  6. IEnumerable,ICollection,IList接口问题

    最近有一个疑问:IList已经继承了ICollection<T>,而ICollection<T>继承了 IEnumerable<T>, IEnumerable,那为 ...

  7. c# json 序列化时遇到错误 error Self referencing loop detected for type

    参考网址:http://blog.csdn.net/adenfeng/article/details/41622255 在写redis缓存帮助类的时候遇到的这个问题,本来打算先序列化一个实体为json ...

  8. Laravel日志

    大家在使用 Log::info() 会让日志全部记录默认在 storage/logs/laravel.log 文件里,文件大了查找起来就比较麻烦.那么我可不可以单独记录在一个日志文件里呢? 只需在你的 ...

  9. Maven(六)Eclipse使用Maven插件创建项目

    1. 创建Maven版Java工程 1.1 具体步骤 1.2 更改默认JDK版本 默认JDK版本过低 可以通过配置setting.xml来更改JDK版本 加入如下代码 <profile> ...

  10. Java简单介绍及Java生态

    核心思想:面向对象编程,继承,高兼容(代码移植性强),开源,避免重复造轮子(使用mybatis,spring,redis等技术只需要将jar包依赖添加到项目中即可,jar包内就是技术核心代码,而这些框 ...