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. 2017 ACM-ICPC网络赛 H.Skiing 有向图最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  2. 动态规划:HDU-2542-0-1背包问题:饭卡

    解题心得: 这题就是一个简单的0-1背包问题,只不过加了一系列的限制.可以想办法消去限制,直接转换成0-1背包问题的模板形式. 需要注意的几个点:首先对于剩余的5元钱的处理可以直接在总的钱数上将5减去 ...

  3. Linux多线程总结

    一.Linux线程 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linux本身只有进程的概念,而其所谓的“线程”本质 ...

  4. Word 2013发布博客测试

    Hello world ! I am from word2013! 测试修改 这里添加一行文字.   参考 1在 Word 中建立博客的相关帮助 2使用Word2013发布随笔到博客园 PS: 参考2 ...

  5. Android学习笔记之-----讯飞语音识别实例化RecognizerDialog参数出现错误的解决方法

    本人也是个小菜鸟,在做语音识别时也遇到了这个问题,空指针一直报错,app程序停止运行. 在网上搜了半天在这个帖子里找到了解决方法:http://bbs.xfyun.cn/forum.php?mo .. ...

  6. 矩阵儿快速幂 - POJ 3233 矩阵力量系列

    不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...

  7. 【Edit Distance】cpp

    题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...

  8. windows下命令行

    创建文件夹 mkdir 文件夹名字 创建文件 echo >文件名字 输入文件内容

  9. 史林枫:C#.NET利用ffmpeg操作视频实战(格式转换,加水印 一步到位)

    ffmpeg.exe是大名鼎鼎的视频处理软件,以命令行参数形式运行.网上也有很多关于ffmpeg的资料介绍.但是在用C#做实际开发时,却遇到了几个问题及注意事项,比如如何无损处理视频?如何在转换格式的 ...

  10. AutoDispose代替RxLifecycle优雅的解决RxJava内存泄漏问题

    使用过Rxjava的小伙伴都知道,在使用RxJava时如果处理不当,很可能会产生内存泄漏的问题. 我们使用rxjava最大的原因是响应式编程使我们的异步操作代码变得很优雅,在Android中,也使线程 ...