【leetcode】N-Queens
N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."], ["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
vector<int> q(n,-);
vector<vector<string> > result;
getQueens(,n,result,q);
return result;
}
void PrintQueens(vector<int> &q,vector<vector<string> > &result)
{
int n=q.size();
vector<string> tmp;
string str;
for(int i=;i<n;i++)
{
str="";
for(int j=;j<n;j++)
{
if(q[i]==j)str+='Q';
else str+='.';
}
tmp.push_back(str);
}
result.push_back(tmp);
}
void getQueens(int level,int &n,vector<vector<string> > &result,vector<int> &q)
{
if(level==n)
{
PrintQueens(q,result);
return;
}
bool flag=false;
for(int i=;i<n;i++)
{
q[level]=i;
if(isValid(q,level))
{
getQueens(level+,n,result,q);
}
q[level]=-;
}
}
bool isValid(vector<int> &q,int &level)
{
for(int i=;i<level;i++)
{
if(q[i]==q[level]||abs(q[level]-q[i])==abs(level-i)) return false;
}
return true;
}
};
【leetcode】N-Queens的更多相关文章
- 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【leetcode】1222. Queens That Can Attack the King
题目如下: On an 8x8 chessboard, there can be multiple Black Queens and one White King. Given an array of ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- App Extension Today
App Extensions 是iOS8新开放的扩展机制,之后不断增加功能.App Extension Programming Guide: Today 不喜欢废话,直接上干货! 一:重要概 ...
- 自定义验证jquery.validate
js中填写 $(function() { validateSysConfigForm(); jQuery.validator.addMethod("validateNum", fu ...
- margin-bottom在IE6和IE7下失效的解决办法
IE6/7下margin-bottom无效一般出现在容器里某元素设置后在父容器内无效,这个时候只需要在父容器中加入以下两句css,基本上所有的浏览器都兼容了: overflow:hidden;zoom ...
- 【8-17】c++学习笔记01
控制台程序不自动退出方法: system("pause"); getchar() 使用执行 ctrl+F5,开始调试 F5会出现闪退 动态内存分配 //construct c st ...
- C#反射机制(转)
一:反射的定义 审查元数据并收集关于它的类型信息的能力.元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等. Sys ...
- linux命令--dig
dig,和nslookup作用有些类似,都是DNS查询工具,但是却比nslookup强大 dig,其实是一个缩写,即Domain Information Groper. [我想用google-DNS来 ...
- spring缓存Ehcache(入门2)源码解读
Ehcache缓存: 解读: Ehcache缓存是在继承spring缓存核心类CacheManager的基础上实现的. 常用类: EhCacheCacheManager:继承自CacheManager ...
- Excel 使用宏批量修改单元格内指定文字为红字
-> step 1:新建宏,进入编辑,使用如下代码: Sub Ss()Dim c As RangeFor Each c In ActiveSheet.UsedRange i = 1 While ...
- 绿书模拟day10 单词前缀
[题目描述]一组单词是安全的,当且仅当不存在一个单词是另一个单词的前缀,这样才能保证数据不容易被误解,现在你手上有一个单词集合s,你需要计算有多少个自己是安全的.注意空集永远是安全的.[输入格式]第一 ...
- Todd's Matlab讲义第5讲:二分法和找根
二分法和if ... else ... end 语句 先回顾一下二分法.要求方程\(f(x)=0\)的根.假设\(c = f(a) < 0\)和\(d = f(b) > 0\),如果\(f ...