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.."]
]

这题和Sudoku Solver是一个套路,回溯法尝试所有可能性,将可行解的保存起来。可以对比着看。

由于这里路径尝试本质上是有序的,即1~9逐个尝试,因此无需额外设置状态位记录已经尝试过的方向。

我们先用vector<int>来存放可行解,下标代表行号,元素代表列号。

因此上图中Solution 1用vector<int>表示就是[1,3,0,2]

解题流程就是在下一行中尝试列数(0~n-1),如果可行则递归下去,如果不可行则弹出继续尝试下一列。

最后将vector<vector<int> > 转为vector<vector<string> >即可。

class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
return convert(solve(n), n);
}
vector<vector<int> > solve(int n)
{
vector<vector<int> > ret;
vector<int> cur;
Helper(ret, cur, , n);
return ret;
}
void Helper(vector<vector<int> >& ret, vector<int> cur, int pos, int n)
{
if(pos == n)
ret.push_back(cur);
else
{
for(int i = ; i < n; i ++)
{
cur.push_back(i);
if(check(cur))
Helper(ret, cur, pos+, n);
cur.pop_back();
}
}
}
bool check(vector<int> cur)
{
int size = cur.size();
int loc = cur[size-];
for(int i = ; i < size-; i ++)
{
if(cur[i] == loc)
return false;
else if(abs(cur[i]-loc) == abs(i-size+))
return false;
}
return true;
}
vector<vector<string> > convert(vector<vector<int> > ret, int n)
{
vector<vector<string> > retStr;
for(int i = ; i < ret.size(); i ++)
{
vector<string> curStr;
for(int j = ; j < n; j ++)
{
string loc(n, '.');
loc[ret[i][j]] = 'Q';
curStr.push_back(loc);
}
retStr.push_back(curStr);
}
return retStr;
}
};

【LeetCode】51. N-Queens的更多相关文章

  1. 【LeetCode】51. N-Queens 解题报告(C++)

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

  2. 【LEETCODE】51、数组分类,简单级别,题目:581,830,1010,665

    package y2019.Algorithm.array; /** * @ClassName FindUnsortedSubarray * @Description TODO 581. Shorte ...

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

随机推荐

  1. Python的开源人脸识别库:离线识别率高达99.38%

    Python的开源人脸识别库:离线识别率高达99.38%   github源码:https://github.com/ageitgey/face_recognition#face-recognitio ...

  2. 四边形优化dp

    理解: http://blog.renren.com/share/263498909/1064362501 http://www.cnblogs.com/ronaflx/archive/2011/03 ...

  3. JavaScript快速检测浏览器对CSS3特性的支持情况

    项目中使用动画效果在IE9下不支持,所以写了个判断浏览器是否支持动画的函数,进而扩展到下面判断浏览器支持任一特定CSS3属性的函数. function supportAnimation(){ var ...

  4. C# 特性(Attribute)(一)

    特性(Attributes)是一种崭新的声明性信息.我们不仅可以通过特性来定义设计层面的信息(例如help file, URL for documentation)以及运行时(run-time)信息( ...

  5. EF实体类的枚举属性映射设计方法

    public class FoundationInfo { [Column("id")] public int ID { get; set; } public InvestType ...

  6. Triangular numbers

    http://codeforces.com/problemset/problem/47/A Triangular numbers time limit per test 2 seconds memor ...

  7. 微博推荐算法学习(Weibo Recommend Algolrithm)

    原文:http://hijiangtao.github.io/2014/10/06/WeiboRecommendAlgorithm/ 基础及关联算法 作用:为微博推荐挖掘必要的基础资源.解决推荐时的通 ...

  8. CentOS6.3 安装配置 ant

    OS:CentOS6.3 ant版本:apache-ant-1.9.2-bin 第1步:下载ant apache-ant-1.9.2-bin.tar.gz 第2步:解压 tar -zxvf apach ...

  9. Python-__builtin__与__builtins__的区别与关系(超详细,经典)(转)

    Python-__builtin__与__builtins__的区别与关系(超详细,经典) (2013-07-23 15:27:32) 转载▼   分类: Python 在学习Python时,很多人会 ...

  10. java面试第四天

    修饰符static: 把对象相关的变成类相关的,它可以修饰属性.方法.代码块和内部类 static修饰属性(类变量): 那么这个属性就可以用" 类名.属性名 "来访问,也就是使这个 ...