LeetCode OJ:N-Queens(N皇后问题)
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.."]
]
经典的N皇后问题,我首先是用brute force,但是这样做的话一直会TLE:
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<int> board(n);
vector<vector<string>> ret;
vector<string> tmpVec;
for(int i = ; i < n; ++i){
board[i] = i + ;//确保了上下左右不会有相邻的元素
}
vector<int> afterAdd(n);
vector<int> afterSub(n);
for(;;){
transform(board.begin(), board.end(), afterAdd.begin(),
[](int i)->int{static int index = ; i += index; index++; return i;});
transform(board.begin(), board.end(), afterSub.begin(),
[](int i)->int{static int index = ; i -= index; index++; return i;});
set<int>afterSubSet(afterSub.begin(), afterSub.end());
set<int>afterAddSet(afterAdd.begin(), afterAdd.end());
if(afterAddSet.size() == n && afterSubSet.size() == n){
for(int i = ; i < n; i++){
string tmp = "";
for(int j = ; j < n; ++j){
tmp.append(, j == board[i]- ? 'Q' : '.');
}
tmpVec.push_back(tmp);
tmp.clear();
}
ret.push_back(tmpVec);
tmpVec.clear();
}
if(!next_permutation(board.begin(), board.end())){
return ret;
break;
}
}
}
};
后来就只能使用dfs了,代码如下:
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
ret.clear();
memset(canUse, true, sizeof(canUse));
dfs(,n);
return ret;
}
bool check(int pos, int line)
{
for(int i = ; i < line; ++i){
if(line - i == abs(pos - a[i])) //这一步很重要
return false; //相差n行的两个皇后的位置不可以也正好相差n个,那样一定是不可以的 }
return true;
}
void dfs(int dep, int maxDep)
{
if(dep == maxDep){
vector<string> tmpVec;
for(int i = ; i < maxDep; ++i){
string tmp = "";
for(int j = ; j < maxDep; ++j){
tmp.append(, j == a[i] ? 'Q' : '.');
}
tmpVec.push_back(tmp);
tmp.clear();
}
ret.push_back(tmpVec);
tmpVec.clear();
}
for(int i = ; i < maxDep; ++i){
if(canUse[i] && check(i, dep)){
canUse[i] = false;
a[dep] = i;
dfs(dep + , maxDep);
canUse[i] = true;
}
}
}
private:
vector<vector<string>> ret;
int a[];
bool canUse[];
};
LeetCode OJ:N-Queens(N皇后问题)的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- Java中的JDBC是什么?
JDBC(Java Data Base Connectivity, Java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它有一组用Java语言编写的类和接 ...
- Java TreeMap详细介绍和使用示例
①对TreeMap有个整体认识 TreeMap是一个有序的key-value集合,它是通过红黑树实现的. TreeMap继承于AbstractMap,所以它是一个Map,即key-value集合. T ...
- playbook管理配置文件
前言:在生产环境中,经常需要更改多台机器配置文件,所以用playbook来实现nginx配置文件的管理 一.更新nginx配置文件并重新加载 1. 创建对应目录结构 mkdir -p /etc/ans ...
- Python学习笔记之Python的多重继承和MixIn
多重继承 继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. 回忆一下Animal类层次的设计,假设我们要实现以下4种动物: Dog - 狗狗: Bat - 蝙蝠: Parr ...
- Python3.x:代理ip刷点赞
Python3.x:代理ip刷点赞 声明:仅供为学习材料,不允许用作商业用途: 一,功能: 针对某网站对企业自动刷点赞: 网站:https://best.zhaopin.com/ 二,步骤: 1,获取 ...
- STS,MyEclipse中Maven配置
本文以STS的环境做讲解,MyEclipse环境和STS差别不大,配置过程相似. STS是解压版的,启动后,可以看到已经有了Maven插件,, 但是,STS也同时给你了一个Maven,但是通常不建议使 ...
- Ubuntu16.04安装搜狗拼音输入法(中文输入法)[转]
本文转载自:https://www.cnblogs.com/darklights/p/7722861.html 虽然网上有很多教程,但是我觉得我的很适合那些真正的小白... 1.下载文件 由于我要给多 ...
- LeetCode——Longest Word in Dictionary through Deleting
1. Question Given a string and a string dictionary, find the longest string in the dictionary that c ...
- SQL查询入门(下篇)
SQL查询入门(下篇) 文章转自:http://www.cnblogs.com/CareySon/archive/2011/05/18/2049727.html 引言 在前两篇文章中,对于单表查询 ...
- PyTorch源码解读之torchvision.transforms(转)
原文地址:https://blog.csdn.net/u014380165/article/details/79167753 版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...