Leetcode 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.."]
] 典型的N皇后问题,对每行元素进行搜索即可
class Solution {
public:
int n;
vector<vector<string> > res;
vector<int> queenIndex;
bool judgePlaceQueen(int x,int y){
for(int i = ; i < x; ++ i){
if(abs(i-x) == abs(queenIndex[i]-queenIndex[x]) || queenIndex[i] == y) return false;
}
return true;
}
void dfs(int index){
if(index == n){
vector<string> board(n,string(n,'.'));
for(int i = ; i < n ; ++ i){
board[i][queenIndex[i]] = 'Q';
}
res.push_back(board);
return;
}
for(int i = ; i < n; ++ i){
queenIndex[index] = i;
if(judgePlaceQueen(index,i)) dfs(index+);
}
}
vector<vector<string> > solveNQueens(int n) {
this->n = n;
for(int i = ;i < n; ++ i) queenIndex.push_back();
dfs();
return res;
}
};
下面用排列组合的方法解决。
N个皇后放置在NxN的棋盘上,必定每行一个皇后,将每行中的皇后的位置用0~N-1的数字来表示,总共N行,
这样就是0~N-1数字的排列。这样的排列只满足了任意两个皇后不能处在同一行或者一列上,并不能保证它们在一斜线上。
在加入数字排列前,判断该数字是否和排列里所有的数字在斜线上:
如果两个数字在斜线上,那么两数之差的绝对值等于其下标差得绝对值。
class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
vector<vector<string> > res;
vector<int> queen(n,);
for(int i = ; i < n ; ++ i) queen[i] = i;
do{
bool flag = false;
for(int i = ; i< n && !flag; ++ i){
for(int j = ; j < i && !flag; ++ j){
if(abs(j-i) == abs(queen[j]-queen[i]) || queen[j] == queen[i]) flag = true;
}
}
if(!flag){
vector<string> board(n,string(n,'.'));
for(int i = ; i < n; ++ i) board[i][queen[i]]='Q';
res.push_back(board);
}
}while(next_permutation(queen.begin(),queen.end()));
return res;
}
};
Leetcode N-Queens的更多相关文章
- [Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【leetcode】1222. Queens That Can Attack the King
题目如下: On an 8x8 chessboard, there can be multiple Black Queens and one White King. Given an array of ...
- [LeetCode] N-Queens II N皇后问题之二
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [LeetCode] N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- [CareerCup] 9.9 Eight Queens 八皇后问题
9.9 Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board so that non ...
- Leetcode | N-Queens I & II
N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...
- [LeetCode]题解(python):051-N-Queens
题目来源 https://leetcode.com/problems/n-queens/ The n-queens puzzle is the problem of placing n queens ...
- [Leetcode][Python]52: N-Queens II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...
- [Leetcode][Python]51: N-Queens
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 51: N-Queenshttps://oj.leetcode.com/pro ...
随机推荐
- 第二轮冲刺-Runner站立会议05
今天:将baseadapter的原理弄清楚了 明天:解决适配问题 困难:程序会停止运行
- HTC学习笔记
添加一个属性的setter, getter 建立一个页面 <html> <head> <title>TODO supply a title</title> ...
- [译]使用branch
这篇文章将介绍Git分支. 首先, 看看如果创建分支, 这就像是request一个新的项目历史. 接着, 来看看git checkout是如果能被用来选择一个分支的. 最后, 学习用git merge ...
- Digital calculation
No1=1 No2=2 1. let result=No1+No2 let No1++ let No1+=3 2. result=$[No1+No2] 3. result=$((No1+No ...
- C和指针 第五章 警告总结
1.有符号的值得右移位操作是不可移植的 2.移位操作的位数是个负数,是未定义的 3.连续赋值的各个变量的长度 不一,导致变量值截断. #include <stdio.h> int main ...
- javascript基础02
javascript基础02 1.数据类型 数据类型的描述在上篇的扩展中有写到链接 由于ECMAScript数据类型具有动态性,因此的确没有再定义其他数据类型的必要.这句话很重要. 如果以后再数据类型 ...
- Yslow-23条规则
1. 减少HTTP请求次数 合并图片.CSS.JS,减少首次访问用户等待时间. 2. 使用CDN就近缓存==>智能路由==>负载均衡==>WSA全站动态加速 3. 避免空的src和h ...
- PYTHON isinstance语法
def obj_len(arg): #isinstance(),判断是否是某一类 if isinstance(arg,str) or (isinstance(arg,list)) or (isinst ...
- SQLServer 事务隔离级别与锁的申请和释放
脏读:当一个事务开始更新数据,但是这个事务并没有完全提交,这个时候第二个事务开始读取数据,把第一个事务所更改的数据读了出来, 第二个事务读取的数据时临时的,因为有可能第一个事务最终有可能做回滚操作 不 ...
- ecplise 常用快捷键
/* * alt+/ * * A:main * main+alt+/ * B:输出语句 * syso+alt+/ * C:提示作用 * * */ /* * 常用快捷键 * 1.格式化:ctrl+shi ...