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 ...
随机推荐
- No module named migrate.versioning
在学习mega-tutorial的数据库章节时创建数据库遇到了问题,在stackoverflow上找到了结果 pip install sqlalchemy==0.7.9 pip install sql ...
- RSync实现文件备份同步
[rsync实现网站的备份,文件的同步,不同系统的文件的同步,如果是windows的话,需要windows版本cwrsync] 一.什么是rsync rsync,remote synchronize顾 ...
- postgresql是如何处理死连接(转)
在数据库postgresql中,一个客户端到服务器连接实际上是一个tcp socket连接,tcp连接是虚连接,一方非正常退出(如断电),另一方会继续维持这个连接. 举个例子,一个客户端电脑正常连 ...
- 【Tomcat 6.0官方文档翻译】—— 简介
Tomcat作为使用最多的web容器,研究其原理过程,对掌握java web开发有很重要的影响. 因此下定决心,从官方文档入手,好好学学web相关的知识. 介绍 本篇是Apache Tomca ...
- Flex Builder快捷键
Flex几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/) 快速修正:Ctrl+1 单词补全:Alt+/ 打开外部Java文档:Shift+F2 显示搜索对话框:Ctr ...
- Github上的PHP资源汇总大全
依赖管理 ——用于依赖管理的包和框架 Composer/Packagist : 一个包和依赖管理器 Composer Installers: 一个多框架Composer库安装器 Pickle: 可以 ...
- 【Android学习】android:layout_weight的用法实例
对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...
- C和指针 第十二章 结构体 习题
12.3 重新编写12.7,使用头和尾指针分别以一个单独的指针传递给函数,而不是作为一个节点的一部分 #include <stdio.h> #include <stdlib.h> ...
- JS学习:第一周——NO.1预解释
1.何为预解释? 在当前作用域下,在JS代码执行之前,浏览器会对带var和带function的进行提前声明或定义: ①带var的:只声明不定义:告诉浏览器,有这么一个变量,但是并没有赋值 ②带func ...
- Proj.4坐标系统创建参数
Proj.4坐标系统创建参数 本文由乌合之众lym瞎编,欢迎转载blog.cnblogs.net/oloroso 本文原文地址(https://github.com/OSGeo/proj.4/wiki ...