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 ...
随机推荐
- 转-ArcGIS Engine中的License设置
AE开发中的License有两种方法进行设置,一种是通过LicenseControl控件,另一种是通过IAoInitialize接口设置.整个应用程序中只能有一种方式存在,如果进行了两种License ...
- Node.js入门笔记(5):案例两则
案例分析:前端自动化 1. 实现一个自动创建前端项目文件的js 通过node.js自动创建前端项目目录,包括js目录,js目录css目录,index.html和对应的内容. 初步的代码如下: var ...
- Java表格的简单使用一
JTable组件使用的是模型/视图/控制器的设计方式,将可视化组件从其数据中分离出来,因此表格中的数据都封装在数据模型中.其中最重要的支持类就是TableModel类,这个类用来定义JTable类以及 ...
- can't debug windows service in win7 64bit
if encount below error: Solution: run the command “vsdiag_regwcf.exe -i” as admin in C:\Program File ...
- Linux/CentOS下开启MySQL远程连接,远程管理数据库
当服务器没有运行PHP.没装phpMyAdmin的时候,远程管理MySQL就显得有必要了. 第一步:开启MySQL用户的远程访问权限 mysql -u root -p mysql # 第1个mysql ...
- jenkins自动化构建iOS应用配置过程中遇到的问题
最近配置jenkins来自动构建iOS应用,期间遇上不少问题.在这里分享给大家,也给自己留个底,方便下次解决问题. 首先说明下基本情况,我们因为部署jenkins的机器不是Mac,所以不能安装Xcod ...
- 局域网中共享Lantern上网
Lantern作为一款非常好用的上网软件,深受大家喜爱,但是由于目前没有ios版,所以iPhone用户上网很麻烦,这里介绍一下如何共享Lantern使局域网内的设备都能正常上网. 1.电脑端设置:右键 ...
- java基础 作业(一)
题目: 跳水比赛,8个评委打分.运动员的成绩是8个成绩去掉一个最高分,去掉一个最低分,剩下的6个分数 的平均分就是最后 得分.使用以为数组实现打分功能 .请把打分最高的评委和最低的评委找出来. 解析: ...
- Proj.4库的编译及使用
Proj.4库的编译及使用 Proj.4是开源GIS最著名的地图投影库,GRASS GIS, MapServer, PostGIS, Thuban, OGDI, Mapnik, TopoCad, GD ...
- java26
1:网络编程(理解) (1)网络编程:用Java语言实现计算机间数据的信息传递和资源共享 (2)网络编程模型 (3)网络编程的三要素 A:IP地址 ...