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. myeclipse工程当中的.classpath 和.project文件什么作用?

    .project是项目文件,项目的结构都在其中定义,比如lib的位置,src的位置,classes的位置.classpath的位置定义了你这个项目在编译时所使用的$CLASSPATH .classpa ...

  2. 在CentOS上搭建apache和PHP服务器环境(转)

    1.您也可以使用一键自动部署环境的工具,请参见网友开发的这个工具 http://www.centos.bz/2013/08/ezhttp-tutorial/ 2. 安装: wget -c http:/ ...

  3. git SSH keys

    An SSH key allows you to establish a secure connection between your computer and GitLab. Before gene ...

  4. 使用DOS比较两个txt文件的差异

    将两个文件放入到同一个文件夹下 DOS下提供了FC命令 点击开始->运行->输入cmd,进入DOS下,进入指定目录,输入FC a.txt b.txt进行比较,下面会显示出之间的差异

  5. nginx TCP 代理& windows傻瓜式安装

    一.下载nginx Windows http://nginx.org/en/download.html 二.解压到目录 三.进入目录并start nginx.exe即可启动 cd d:/java/ng ...

  6. spring 缓存(spring自带Cache)(入门)源码解读

    spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.Ca ...

  7. 清北学堂模拟day6 兔子

    [问题描述] 在一片草原上有N个兔子窝,每个窝里住着一只兔子,有M条路径连接这些窝.更特殊地是,至多只有一个兔子窝有3条或更多的路径与它相连,其它的兔子窝只有1条或2条路径与其相连.换句话讲,这些兔子 ...

  8. HTML5 之Canvas 绘制时钟 Demo

    <!DOCTYPE html> <html> <head> <title>Canvas 之 时钟 Demo</title> <!--简 ...

  9. wordpress如何批量关闭旧日志留言功能

    于一些wordpress技术博客或者其他wordpress博客来说,一些旧日志的内容可能已经过时了,但是一些读者,还是对一些问题“纠缠不清”或者“喋喋不休”,怎么办,把留言关了就好了: UPDATE ...

  10. Mac Pro 软件收藏

    记录下Pro Mac中安装过的软件: 编号 软件名 功能说明 1 QQ   2 微信   3 搜狗输入法   4 Chrome 浏览器  Chrome 及其 插件“个性化设置”备份 5 360云盘   ...