//看了一次解析后,一次AC,用一个pos记录行列。
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> res;
vector<int> pos(n,-);
DFS(pos,,res);
return res;
} void DFS(vector<int>& pos,int row,vector<vector<string>>& res){
int n = pos.size();
if(row == n){
vector<string> out(n,string(n,'.')); //string也有这种填充写法
for(int i=;i < n;i++){
out[i][pos[i]] = 'Q';
}
res.push_back(out);
}
else{
for(int i=;i < n;i++){
if(isfine(pos,row,i)){
pos[row] = i;
DFS(pos,row+,res);
pos[row] = -;
}
}
}
} bool isfine(vector<int>& pos,int row,int col){
for(int i=;i < row;i++){
if(pos[i] == col || abs(row-i) == abs(col-pos[i]))
return false;
}
return true;
}
};

Leetcode 51的更多相关文章

  1. [LeetCode] 51. N-Queens N皇后问题

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  2. LeetCode - 51. N-Queens

    51. N-Queens Problem's Link ------------------------------------------------------------------------ ...

  3. leetcode@ [51/52] N-Queens

    https://leetcode.com/problems/n-queens/ class Solution { public: void dfs(vector<vector<string ...

  4. leetcode 51. N-Queens 、52. N-Queens II

    51. N-Queens 使用isValid判断当前的位置是否合法 每次遍历一行,使用queenCol记录之前行的存储位置,一方面是用于判断合法,另一方面可以根据存储结果输出最终的结果 class S ...

  5. LeetCode: 51. N-Queens(Medium)

    1. 原题链接 https://leetcode.com/problems/n-queens/description/ 2. 题目要求 游戏规则:当两个皇后位于同一条线上时(同一列.同一行.同一45度 ...

  6. Java实现 LeetCode 51 N皇后

    51. N皇后 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问题的解决 ...

  7. leetcode 51. N皇后 及 52.N皇后 II

    51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...

  8. LeetCode(51)- Count and Say

    题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...

  9. LeetCode 51 N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  10. [leetcode]51. N-QueensN皇后

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

随机推荐

  1. poj1584 A round peg in a ground hole【计算几何】

    含[判断凸包],[判断点在多边形内],[判断圆在多边形内]模板  凸包:即凸多边形 用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边形,它能包含点集中所有的点. The ...

  2. Use of ‘const’ in Functions Return Values

    Use of 'Const' in Function Return Values 为什么要在函数的返回值类型中添加Const? 1.Features Of the possible combinati ...

  3. Java 笔记20180123

    在批量添加数据时候采用List或者Map或者SetSet<Student> students = new Set<Student>();// 客户端验证:js:服务器端验证:必 ...

  4. BaseLayout

    angularjs2 knockoutjs framework7 jquerymobile bootstrap html5 css [Activity(Label = "ActivityBa ...

  5. 很靠谱linux常用命令

    vim是打开vim编辑器,别的编辑器还有vi(功能没有vim 强大),nano,emacs等等,感觉还是vim最强大,其次是vi,别的就要差一些了. 我听我们老师说,用图形界面本身已经会被高手笑了,如 ...

  6. PhotoSwipe中文API(四)

    在幻灯片自定义HTML内容 为了使PhotoSwipe显示HTML内容的幻灯片,你需要在幻灯片对象定义html属性.它应该包含HTML字符串或DOM元素对象. var items = [ // sli ...

  7. 模块讲解----反射 (基于web路由的反射)

    一.反射的实际案例: def main(): menu = ''' 1.账户信息 2.还款 3.取款 4.转账 5.账单 ''' menu_dic = { ':account_info, ':repa ...

  8. 20165324_mybash

    20165324_mybash 实验要求 实验要求: 使用fork,exec,wait实现mybash 写出伪代码,产品代码和测试代码 发表知识理解,实现过程和问题解决的博客(包含代码托管链接) 背景 ...

  9. mysql数据库权限

    use mysql select * from user \G; UPDATE user set password=PASSWORD('root') where user='root' grant a ...

  10. java之类适配器

    类适配器 所谓类适配器,指的是适配器Adapter继承我们的被适配者Adaptee,并实现目标接口Target.由于Java中是单继承,所以这个适配器仅仅只能服务于所继承的被适配者Adaptee.代码 ...