leetcode:N-Queens 问题
一、N-QueensII
class Solution {
public:
int totalNQueens(int n) {
int total = ;
vector<int> v(n,);
dfs(,n,total,v);
return total;
}
private:
bool isvalide(vector<int>& v,int x){
for(int i=;i<x;i++){
if(v[i]==v[x] || abs(v[x]-v[i])==abs(x - i))
return false;
}
return true;
} void dfs(int curr,int n,int& total,vector<int>& v){
if(curr == n) {
total++;
return;
} for(int i=;i<n;i++){
v[curr] = i;
if(isvalide(v,curr)){
dfs(curr+,n,total,v);
}
}
}
};
二、N-Queens
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> result;
vector<int> v(n,-);
dfs(,n,v,result);
return result;
}
bool isValide(vector<int>& v,int curr){
for(int i=;i< curr;i++){
if(v[i] == v[curr] || abs(v[curr]-v[i])==abs(curr-i))
return false;
}
return true;
}
void dfs(int curr,int n,vector<int>& v,
vector<vector<string>>& result){
if(curr == n){
vector<string> temp;
for(int i=;i<n;i++){
string s(n,'.');
s[v[i]] = 'Q';
temp.push_back(s);
}
result.push_back(temp);
return;
}
for(int i=;i<n;i++){
v[curr] = i;
if(isValide(v,curr)){
dfs(curr+, n, v,result);
}
}
}
};
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 ...
随机推荐
- 中心极限定理&&正态分布 随想
0-前言 笔者本来周末约好朋友出去骑行,不料天公不作美!哎,闲来无事来到了实验室,本来打算看看<天天向上>,而这一期又实在不好看(偶像剧).只好来做做一些小实验,脑海里突然想到“正态分布“ ...
- 【C++】C++中的操作符重载
C++中的操作符重载使得对于类对象的操作更加方便和直观,但是对于各种操作符重载的规则以及语法形式,一直以来都是用到哪一个上stackoverflow上查找,在查找了四五次之后,觉得每次麻烦小总结一下. ...
- Replication--如何使用快照来初始化化请求订阅
这是一篇针对新人的知识普及文章,老人慎入! 在快照发布和事务发布中,SQL Server需要使用快照来将数据库某一时间点的数据传递给订阅,快照使用BCP的机制. 首先我们需要查看和设置快照的生成目录, ...
- jenkin+docker+git持续集成环境搭建
1.安装Jenkins(需要在Jenkins容器中安装maven,java环境不用安装,Jenkins初次启动时会自动安装) 参考:docker中安装Jenkins 2.配置git 3.安装docke ...
- C#基础笔记(第二十一天)
1.FIle类.Path类.Directory类复习操作文件的File 操作文件,静态类,对文件整体操作.拷贝.删除.剪切等.Directory 操作目录(文件夹),静态类.Path 对文件或目录的路 ...
- css细节复习笔记——浮动
CSS除了能够改变字体.背景和所有其他属性,还能够完成基本布局任务. div+css通过浮动和定位.盒子模型等技术应用,是最常用的布局方式. 定位的基本思想很简单,它允许定义元素框相对于其正常位置应该 ...
- linux 使用内存作为 /tmp 临时文件夹
方法一:cat /etc/fstabtmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 方法二:mount tmpfs /tmp -t tmpfs -o s ...
- CCF CSP 201803-1 跳一跳
题目链接:http://118.190.20.162/view.page?gpid=T73 问题描述 近来,跳一跳这款小游戏风靡全国,受到不少玩家的喜爱. 简化后的跳一跳规则如下:玩家每次从当前方块跳 ...
- soapui加载天气预报接口报错Characters larger than 4 bytes are not supported: byte 0xb1 implies a length of more than 4 byte的解决办法
soapui加载天气预报接口时报错如下: Error loading [http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl ...
- [Virtualization] VMware虚拟机三种网络模式详解(转)
原文:http://www.linuxidc.com/Linux/2016-09/135521.htm