LeetCode OJ-- N-Queens **
https://oj.leetcode.com/problems/n-queens/
n皇后问题,1皇后有1个解,4皇后2个解,8皇后也有解……
每个皇后不能在同一行上,同一列上,以及同一条45度线上。
可以把皇后所在位置用一维数组表示,比如 2 0 3 1,表示第0个皇后在第0行的第二个位置上……
所以验证条件就是,不能有两个数相等,不能 位置差值 == 值的差值
N皇后问题,使用递归法。
对于一个皇后,它所属于列的所有位置,遍历,找出合理的位置,之后递归下一个皇后
class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
vector<vector<string> > ans;
if(n<=)
return ans; vector<int> places(n,-); placeQueen(, n, ans, places); return ans;
} // col and lines
bool valid(int i, int j, vector<int> &places)
{
for(int p = ; p<i; p++)
{
if(places[p] == j || abs(i - p) == abs(places[p] - j))
return false;
}
return true;
} void placeQueen(int i, int n, vector<vector<string> > &ans, vector<int> &places)
{
// one solution
if(i == n)
{
vector<string> ansPiece;
for(int p = ; p<n; p++)
{
string str;
str.resize(n);
for(int q = ; q<places[p]; q++)
str[q] = '.';
str[places[p]] = 'Q';
for(int q = places[p] + ; q < n; q++)
str[q] = '.';
ansPiece.push_back(str);
}
ans.push_back(ansPiece);
} for(int col = ; col < n; col++)
{
if(valid(i,col,places))
{
places[i] = col;
placeQueen(i+,n,ans,places); // next queue, next line
}
}
}
};
LeetCode OJ-- N-Queens **的更多相关文章
- 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 ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
随机推荐
- pip3 的安装 同时安装lxml和pygame
ubuntu18.04中 首先查看自己电脑的python版本,一般都会有2, 和3 python -V python3 -V 查看pip版本 pip -V pip3 -V 现在我们就可以开始安装我们的 ...
- Redis实现之字典
字典 字典,又称为符号表(symbol table).关联数组(associative array)或映射(map),是一种用于保存键值对(key-value pair)的抽象数据结构.在字典中,一个 ...
- Java并发之(1):volatile关键字(TIJ21-21.3.3 21.3.4)
Java并发Java服务器端编程的一项必备技能. ** 1 简介 volatile是java中的一个保留关键字,它在英语中的含义是易变的,不稳定的.volatile像final.static等其 ...
- 【Word Search】cpp
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- leetcode 【 Reverse Nodes in k-Group 】 python 实现
原题: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- IOS笔记047-代理传值和block传值
在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据 ...
- Oracle 分析函数--Row_Number()
row_number() over ([partition by col1] order by col2) ) as 别名 表示根据col1分组,在分组内部根据 col2排序 而这个“别名”的值就表示 ...
- 基于web自动化测试框架的设计与开发(讲解演示PPT)
- Python-S9-Day128——算法基础Algorithm
01 算法基本概念与递归: 02 二分查找与LOW B三人组 03 快速排序 04 归并排序 01 算法基本概念与递归: 1.1 算法概念 1.2 复习:递归 1.3 时间复杂度 1.4 空间复杂度 ...
- python-day5-装饰器第二弹之多层装饰器
多层装饰器 #首先我们先实现一个简单的登陆与权限验证功能,注意看执行结果 USER_INFO = {} def check_login(func): def inner(*args,**kwargs) ...