【Lintcode】033.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.
There exist two distinct solutions to the 4-queens puzzle:
[
// Solution 1
[".Q..",
"...Q",
"Q...",
"..Q."
],
// Solution 2
["..Q.",
"Q...",
"...Q",
".Q.."
]
]
题解:
此题需要注意的是对角线因素,不仅不能对角线相邻,而且不能在一条对角线上,中间隔着一个也不行!刚开始没注意,花了一个多小时才明白过来。。。
Solution 1 ()
class Solution {
public:
void dfs(vector<vector<string>>& res, vector<string>& v, int n, vector<int>& pos, int row) {
if(row >= n) {
res.push_back(v);
return;
}
for(int col=; col<n; ++col) {
if (!isValid(pos, row, col)) {
continue;
}
v[row][col] = 'Q';
pos[row] = col;
dfs(res, v, n, pos, row + );
pos[row] = -;
v[row][col] = '.';
}
}
bool isValid(vector<int>& pos, int row, int col) {
for (int i = ; i < row; ++i) {
if (pos[i] == col || abs(row - i) == abs(col - pos[i])) {
return false;
}
}
return true;
}
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> res;
vector<string> v(n, string(n, '.'));
vector<int> pos(n, -);
dfs(res, v, n, pos, );
return res;
}
};
Solution 1.2 () from here
class Solution {
private:
vector<vector<string> > res;
public:
vector<vector<string> > solveNQueens(int n) {
vector<string>cur(n, string(n,'.'));
helper(cur, );
return res;
}
void helper(vector<string> &cur, int row)
{
if(row == cur.size())
{
res.push_back(cur);
return;
}
for(int col = ; col < cur.size(); col++)
if(isValid(cur, row, col))
{
cur[row][col] = 'Q';
helper(cur, row+);
cur[row][col] = '.';
}
}
//判断在cur[row][col]位置放一个皇后,是否是合法的状态
//已经保证了每行一个皇后,只需要判断列是否合法以及对角线是否合法。
bool isValid(vector<string> &cur, int row, int col)
{
//列
for(int i = ; i < row; i++)
if(cur[i][col] == 'Q')return false;
//右对角线(只需要判断对角线上半部分,因为后面的行还没有开始放置)
for(int i = row-, j=col-; i >= && j >= ; i--,j--)
if(cur[i][j] == 'Q')return false;
//左对角线(只需要判断对角线上半部分,因为后面的行还没有开始放置)
for(int i = row-, j=col+; i >= && j < cur.size(); i--,j++)
if(cur[i][j] == 'Q')return false;
return true;
}
};
Solution 1.3 ()
class Solution2 {
public:
std::vector<std::vector<std::string> > solveNQueens(int n) {
std::vector<std::vector<std::string> > res;
std::vector<std::string> nQueens(n, std::string(n, '.'));
solveNQueens(res, nQueens, , n);
return res;
}
private:
void solveNQueens(std::vector<std::vector<std::string> > &res, std::vector<std::string> &nQueens, int row, int &n) {
if (row == n) {
res.push_back(nQueens);
return;
}
for (int col = ; col != n; ++col)
if (isValid(nQueens, row, col, n)) {
nQueens[row][col] = 'Q';
solveNQueens(res, nQueens, row + , n);
nQueens[row][col] = '.';
}
}
bool isValid(std::vector<std::string> &nQueens, int row, int col, int &n) {
//check if the column had a queen before.
for (int i = ; i != row; ++i)
if (nQueens[i][col] == 'Q')
return false;
//check if the 45° diagonal had a queen before.
for (int i = row - , j = col - ; i >= && j >= ; --i, --j)
if (nQueens[i][j] == 'Q')
return false;
//check if the 135° diagonal had a queen before.
for (int i = row - , j = col + ; i >= && j < n; --i, ++j)
if (nQueens[i][j] == 'Q')
return false;
return true;
}
};
【Lintcode】033.N-Queens的更多相关文章
- 【Lintcode】033.N-Queens II
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
- 【Lintcode】074.First Bad Version
题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version ...
- 【LintCode】转换字符串到整数
问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...
- 【LintCode】判断一个字符串是否包含另一个字符串的所有字符
问题描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母. 样例 给出 A = "ABCD" B = "ACD",返 ...
- 【LintCode】链表求和
问题分析: 我们通过遍历两个链表拿到每个位的值,两个值加上前一位进位值(0或者1)模10就是该位的值,除以10就是向高位的进位值(0或者1). 由于两个链表可以不一样长,所以要及时判断,一旦为null ...
- 【LintCode】删除链表中的元素
问题分析: 声明当前指针和上一个指针即可. 问题求解: public class Solution { public ListNode removeElements(ListNode head, in ...
- 【LintCode】计算两个数的交集(二)
问题分析: 用两个指针分别遍历即可. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 ...
- 【LintCode】计算两个数的交集(一)
问题分析: 既然返回值没有重复,我们不妨将结果放进set中,然后对两个set进行比较. 问题求解: public class Solution { /** * @param nums1 an inte ...
随机推荐
- SpringBoot启动流程分析(四):IoC容器的初始化过程
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- Linux内核编译过程分析
http://pan.baidu.com/s/1mgtACVu 其中是我总结生成的一些文档,以便于理解当我们输入make uImage后,系统是怎么一步一步生成uImage的,我采用的是逆向分析的方法 ...
- mnesia的脏读和事物读的测试
在mnesia中,有脏读脏写等以及事物读写,它们的差异通过测试不难发现: 代码如下: -module(mnesia_read_test). -compile(export_all). -record( ...
- 小程序框架MpVue踩坑日记(一)
小程序也做了几个小功能模块了,总觉得需要总结一下,踩坑什么的还是得记录一下啊. 好吧,其实是为了方便回顾 首先,说到小程序框架,大家都知道wepy,不过,我是没用过 美团开发团队到mpvue到是个实在 ...
- maven scope runtime
https://blog.csdn.net/ningbohezhijunbl/article/details/25818069 There are 6 scopes available: compil ...
- mysq'l系列之10.mysql优化&权限控制
网站打开慢如何排查 1.打开网页, 用谷歌浏览器F12, 查看network: 哪个加载时间长就优化哪个 2.如果是数据库问题 2.1 查看大体情况 # top # uptime //load av ...
- SAP采购寄售业务操作步骤
[转自 http://blog.sina.com.cn/s/blog_6466e5f70100jghg.html] 这里所示的是比较完整的步骤,包含了:信息记录.采购合同.货源清单.采购申请.采购订单 ...
- B. Two Buttons
这是Codeforces Round #295 (Div. 2) 的B 题,题意为: 给出n, m, 有两种操作,n 减一 和 n 乘以 2,问最少要多少次操作才能把n 变成 m. Sample te ...
- ASCII 在线转换器
http://www.ab126.com/goju/1711.html 比如下发内容 <S020,*> 对应的16进制字节为 3C 2C 2A 3E https://wenku.baidu ...
- 统一ID服务
代码已经修改 调用方式 为restful请求 或者 feign请求 请参考 wiki: http://192.168.120.46:8090/display/peixun/akucun+Gui ...