https://oj.leetcode.com/problems/n-queens/

n皇后问题,1皇后有1个解,4皇后2个解,8皇后也有解……

每个皇后不能在同一行上,同一列上,以及同一条45度线上。

可以把皇后所在位置用一维数组表示,比如 2 0 3 1,表示第0个皇后在第0行的第二个位置上……

所以验证条件就是,不能有两个数相等,不能 位置差值 == 值的差值

N皇后问题,使用递归法。

对于一个皇后,它所属于列的所有位置,遍历,找出合理的位置,之后递归下一个皇后

class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
vector<vector<string> > ans;
if(n<=)
return ans; vector<int> places(n,-); placeQueen(, n, ans, places); return ans;
} // col and lines
bool valid(int i, int j, vector<int> &places)
{
for(int p = ; p<i; p++)
{
if(places[p] == j || abs(i - p) == abs(places[p] - j))
return false;
}
return true;
} void placeQueen(int i, int n, vector<vector<string> > &ans, vector<int> &places)
{
// one solution
if(i == n)
{
vector<string> ansPiece;
for(int p = ; p<n; p++)
{
string str;
str.resize(n);
for(int q = ; q<places[p]; q++)
str[q] = '.';
str[places[p]] = 'Q';
for(int q = places[p] + ; q < n; q++)
str[q] = '.';
ansPiece.push_back(str);
}
ans.push_back(ansPiece);
} for(int col = ; col < n; col++)
{
if(valid(i,col,places))
{
places[i] = col;
placeQueen(i+,n,ans,places); // next queue, next line
}
}
}
};

LeetCode OJ-- N-Queens **的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

随机推荐

  1. ubuntu下eclipse c++开发

    linux下eclipse运行C++程序出现Launch Failed. Binary Not Found.错误 在unbutu16.04上安装eclipse c++,运行一个hello world程 ...

  2. JMX浅谈

    一 JMX 是什么? JMX(Java Management Extensions,即Java管理扩展) JMX在Java编程语言中定义了应用程序以及网络管理和监控的体系结构.设计模式.应用程序接口以 ...

  3. VBA连接MySQL数据库以及ODBC的配置(ODBC版本和MySQL版本如果不匹配会出现驱动和应用程序的错误)

    db_connected = False '获取数据库连接设置dsn_name = Trim(Worksheets("加载策略").Cells(2, 5).Value)  ---- ...

  4. windows下虚拟环境中配置MySQL-python错误问题

    下载mysql 下载mysql-python 这两步基本没有问题怪就怪的 MySQL-python-1.2.3.win-amd64-py2.7 文件只能安装到python27  路径下 然后在虚拟环境 ...

  5. 学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用利用条件运算符的嵌套来完成此题:C表示。

    # -*- coding: utf8 -*- # Author:wxq #python 2.7 #题目:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用利用条件运算符 ...

  6. 观15级K班团队作业有感

    1.指尖加密 特点:通过可移动设备手机参与电脑文件的解密,使加密更加安全. 缺点:跟柯逍老师的想法差不多,UI简陋,操作不是很友好,或许可以加一个帮助文档. 2.youreyes 特点:可以检测路过的 ...

  7. 浅谈JavaScript中的函数问题

    前面的话:JavaScript可运行在所有主要平台的主流浏览器上,也可运行在每一个主流操作系统的服务器端上.所以呢,要想成为一名优秀的全栈工程师,必须懂得JavaScript语言.这是我整理的JS的部 ...

  8. Fragment控件初始化

    代码改变世界 Fragment控件初始化 @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup ...

  9. CodeForces875C[拓扑排序] Codeforces Round #440 [Div2E/Div1C]

    只要保存每相邻两行字符串 第一个不同位 即可.然后按照 第一个不同位上的字符有: " 来自下一行的 大于 来自上一行的" 构图,跑拓扑排序即可. 当然要判断一下有没有环构成, 有环 ...

  10. 基于kubuntu的C/C++开发环境搭建

    基于kubuntu的环境搭建 系统: kubuntu 14.04 中文输入法: SICM ibus fcitx:sougou 中文输入法的安装比较复杂,由于各种的不兼容,可能会出现各种的问题: 终端配 ...