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 ...
随机推荐
- Provider 模式
Provider 模式:为一个API进行定义和实现的分离. 常见场景:DBPrider切换,第3方集成API切换 以发邮件为例: Email Provider Config: public abstr ...
- akka 练手 原来第一次是原封不动的返回传出去的参数
今天,有介绍akka的文章,就下了个源码的demo练手! 在TimeServer 这个实例中主要就2个文件 server端 static void Main(string[] args) { usin ...
- windows本地代码上传github
1.下载Git工具 https://www.git-scm.com/download/win 2.进到项目目录 cd /dir/dir/dir/file 3.初始化 git init 4.添加远程仓库 ...
- 在JS中简单实现Formatter函数
JS原生并没有提供方便使用的Formatter函数,用字符拼接的方式看起来混乱难读,而且使用起来很不方便.个人感觉C#里提供的语法比较好用,如: String.Format("Welcome ...
- list<T>集合中的Remove()、RemoveAt()、RemoveRange()、RemoveAll()的用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 纸壳CMS的插件加载机制
纸壳CMS是一个开源的可视化设计CMS,通过拖拽,在线编辑的方式来创建网站. GitHub https://github.com/SeriaWei/ZKEACMS.Core 欢迎Star,Fork,发 ...
- python中的 小数据池 is 和 ==
1. 小数据池 一种数据缓存机制,也被称为驻留机制 小数据池针对的是:整数 , 字符 , 布尔值 .其他的数据类型不存在驻留机制 在python中对 -5 到256之间的整数会被驻留在内存中, 将一定 ...
- Web Api 端点设计 与 Oauth
最近一直看这方面的东西,总结如下: 在后续会进行实例demo演示,本篇进行理论详解. 下篇相关博客: <Web Api 内部数据思考 和 利用http缓存优化 Api> <API接口 ...
- 【OCP-052】052最新考试题库分析整理-第7题
7.Which is true about external tables? A) The ORACLE_DATAPUMP access driver can be used to write dat ...
- var在PHP和JS中的使用
一,var在PHP中的使用 var在PHP中使用很少,只在类中声明成员变量时候,可以使用var,其相当于public,而且以后逐渐用public替代var,所以在PHP中尽量不使用var声明变量. 二 ...