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. C++从键盘读入数组并存储

    C++从键盘读取任意长度的数组,现总结如下: //读取指定长度的数组 int main() { int n = 0; cin >> n; vector<int> p(n); f ...

  2. HDU - 5017 Ellipsoid(模拟退火)

    题意 给一个三维椭球面,求球面上距离原点最近的点.输出这个距离. 题解 模拟退火. 把\(z = f(x, y)\)函数写出来,这样通过随机抖动\(x\)和\(y\)坐标就能求出\(z\). 代码 / ...

  3. HDU 1506 Largest Rectangle in a Histogram(单调栈、笛卡尔树)

    题意:给定n个连续排列的矩形的高,矩形的宽都为1.问最大矩形覆盖. 例如:n = 7,h[i] = (2 1 4 5 1 3 3),最大覆盖为8. Sample Input 7 2 1 4 5 1 3 ...

  4. [BZOJ3312][USACO]不找零(状压DP)

    Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身上 ...

  5. HDU 4857

    HDU 4857 (反向拓扑排序 + 优先队列) 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须 ...

  6. luogu3338 [ZJOI2014]力

    我发现我的构造方法好像不太一样而且比较显然?--先读入 \(q\) 数组(下表从零开始). 记 \(i < j\) 时,\(a_{i-j}=-1/i^2\):\(i > j\) 时,\(a ...

  7. loj2046 「CQOI2016」路由表

    大傻逼trie树,更傻逼的是我这都没独立想出来,以后要少看题解,多多思考 #include <algorithm> #include <iostream> #include & ...

  8. bash shell命令与监测的那点事(一)

    bash shell命令与监测的那点事之ps 学习LInux,不得不谈谈bash shell命令,介绍Linux命令行与Shell脚本的书有很多很多,bash shell命令也有很多,此次我们只谈谈有 ...

  9. python 抽象类和接口类

    一.接口类 继承的两种用途: 1.继承基类的方法,并且做出自己的改变或者扩展(代码重用) 2.声明某个子类兼容于某个基类,定义一个接口类interface,接口类中定义了一些接口名(就是函数 名)  ...

  10. c++矩阵

    这里讲的矩阵有创建矩阵,矩阵加法,矩阵乘法,输出矩阵这些功能. #include<iostream> using namespace std; template<class T> ...