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 ...
随机推荐
- 2.C#WebAPI设置路由和参数1
1.当我们创建WebApi的时候我们的项目下的Contorls文件夹下的ValuesController文件下会出现这么几个方法: // GET http://程序ip:程序端口/api/values ...
- stack和stack frame
首先,我们先来了解下栈帧和栈的基本知识: 栈帧也常被称为“活动记录”(activation record),是编译器用来实现过程/函数调用的一种数据结构. 从逻辑上讲,栈帧就是一个函数执行的环境,包含 ...
- C# 使用dynamic类型来访问JObject对象
dynamic是C#里面的动态类型,可在未知类型的情况访问对应的属性,非常灵活和方便. 使用Json.Net可以把一个Json字符串转换成一个JObject对象,如果有已知强类型,如果有已知对应的强类 ...
- HBase – 存储文件HFile结构解析
本文由 网易云发布. 作者:范欣欣 本篇文章仅限内部分享,如需转载,请联系网易获取授权. HFile是HBase存储数据的文件组织形式,参考BigTable的SSTable和Hadoop的TFile ...
- 苹果appID的获取方法
1.登陆 iTunes Connect iTunes Connect 2.选择我的app 3.选择相应的应用 4.查看结果
- [bzoj4009] [HNOI2015]接水果 整体二分+扫描线+dfs序+树状数组
Description 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果. 由于她已经DT FC 了The big black, 她觉得这个游戏太简单了,于是发明了一个更 加 ...
- luoguP4647 [IOI2007] sails 船帆
https://www.luogu.org/problemnew/show/P4647 首先发现答案与顺序无关,令 $ x_i $ 表示高度为 $ i $ 的那一行帆的个数,第 $ i $ 行对答案的 ...
- 浅谈HashMap原理,记录entrySet中的一些疑问
HashMap的底层的一些变量: transient Node<K,V>[] table; //存储数据的Node数组 transient Set<java.util.Map.Ent ...
- python基础知识梳理----5dict 字典的应用
内容简介: 1:字典简介 2:字典的增删该查 3:字典嵌套 1: 字典(dict)是python中唯一的一个映射类型.他是以{ }括起来的键值对组成. 在dict中key是唯一的. 在保存的时候, 根 ...
- 4、Caffe其它常用层及参数
借鉴自:http://www.cnblogs.com/denny402/p/5072746.html 本文讲解一些其它的常用层,包括:softmax_loss层,Inner Product层,accu ...