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.."]
]
 
 
采用q[i]表示第i行的Queen放置的位置
不合法的条件:q[i]==q[level]||abs(q[level]-q[i])==abs(level-i)
 
 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的更多相关文章

  1. 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  2. 【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 ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  10. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. wampserver 2.5安装pear win8.1

    集成环境的悲伤啊~ 本来看到pear想试试 结果发现根本没有go-pear.bat 自己的环境 都是 系统win 8.1 php 5.5.12 mysql  5.6.17 apache 2.4.9   ...

  2. oracle 特殊符号

    http://hi.baidu.com/wind_stay/blog/item/85113a6f6553a5d680cb4a0e.html oracle通配符,运算符的使用 用于where比较条件的有 ...

  3. 30 分钟开发一个简单的 watchOS 2 app <oneVcat>

    Apple Watch 和 watchOS 第一代产品只允许用户在 iPhone 设备上进行计算,然后将结果传输到手表上进行显示.在这个框架下,手表充当的功能在很大程度上只是手机的另一块小一些的显示器 ...

  4. [MongoDB]对数组操作

    摘要 在实际开发中遇到更新某个document中的数组的值,这里做一下记录. 这里使用的驱动为 using MongoDB.Bson;using MongoDB.Driver; 相关文章 [Mongo ...

  5. php中的正则函数主要有三个-正则匹配,正则替换

    php中变量的声明? 由于php声明变量的时候, 不支持使用 var关键字, 又不能直接写一个变量名字, 孤零零的放在那里, 所以, 在php中声明变量的方式, 同时也是给变量初始化的形式, 即: & ...

  6. wamp包--如何导出sql

    在windows下安装wamp,如果不想用phpmyadmin工具和其他工具,如何导出自己想要的sql呢. 比如:我想导出blogyaf库,可以从以下步骤进行操作. 1,进入到wamp的mysql安装 ...

  7. .Net Core 之 图形验证码

    本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能. 通过测试的系统: Windows 8.1 64bit Ubuntu Server 16.04 LTS 64 ...

  8. hadoop安装实战(mac实操)

    集群环境配置参考(http://blog.csdn.net/zcf1002797280/article/details/49500027) 参考:http://www.cnblogs.com/liul ...

  9. POJ 3252 Round Numbers

     组合数学...(每做一题都是这么艰难) Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7607 A ...

  10. 小明系列问题――小明序列(LIS)

    小明系列问题――小明序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...