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. Visual Studio 2013键盘码农常用快捷键

    声明及广告 所有功能针对C#开发配置而写,面向人群是喜欢键盘操作而非鼠标操作的开发者.部分功能可能由插件提供,我会尽可能标注出相应的插件名称.为行文方便,所有快捷键以大写形式表示.太常用的快捷键,如C ...

  2. UVA 10012 How Big Is It?(暴力枚举)

      How Big Is It?  Ian's going to California, and he has to pack his things, including his collection ...

  3. [leetcode]Sort List @ Python

    原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度 ...

  4. jQuery的一个关键函数

    jQuery.extend = jQuery.fn.extend = function() { // copy reference to target object var target = argu ...

  5. PHP json_encode转换空数组为对象

    问题描述: php返回json格式的数据,当返回数据的为数组,且key为字符串时,json化后将返回jsonObject,但是如果是空数组,有可能返回的就是jsonArray,数据结构不一致导致端解析 ...

  6. Ngxtop-Nginx日志实时分析利器

    ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop.有了ngxtop,你可以实时了解到当前nginx的访问状况,再也不需要tail ...

  7. C# 使用Newtonsoft.Json序列化自定义类型

    Json.Net是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Linq To JSON可以快速的读写Json,通过JsonSerializ ...

  8. Windows上使用“LogView”打开大文件

    最近因为工作需要要打开一个300+MB的文件,试过了N个编辑器不是打不开就是软件直接挂掉或是占用内存太大,已经严重影响了我的工作 还好在网络上找到老外写的一个免费软件,官网上说是可以打开4G+的文件( ...

  9. linux下设置 git ssh 代理

    /root/.ssh (以下为 root权限的操作) 1. 生成key. $ ssh-keygen 一路回车,直到生成 id_rsa, id_rsa.pub 1.1 chmod 400 id_rsa. ...

  10. C语言及程序设计[套餐]课程主页

    课程链接:http://edu.csdn.net/combo/detail/30,提供全部的视频和课件下载. 三部分的课程主页.提供了为每一课时配套的自測.演示样例下载,以及程序阅读.程序填空.实践项 ...