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 ...
随机推荐
- Flink本地环境安装部署
本次主要介绍flink1.5.1版本的本地环境安装部署,该版本要求jdk版本1.8以上. 下载flink安装包:http://archive.apache.org/dist/flink/flink-1 ...
- Win10+VS2015折腾小记
20150807 昨天安装了多语言的专业版(当时语言可选,但是我也没选,今天虚机中文企业版时,视图选择语言,但是也只有中文简体,输入法有很多). 专业版安装在硬盘中,感觉不到半小时就完成了. 使用一个 ...
- 开源一款强大的文件服务组件(QJ_FileCenter)(系列三 访问接口与项目集成)
系列文章 1. 开源一款强大的文件服务组件(QJ_FileCenter)(系列一) 2. 开源一款强大的文件服务组件(QJ_FileCenter)(系列二 安装说明) 3. 开源一款强大的文件服务组件 ...
- .Net生成导出Excel
概述 在做.Net web开发的过程中经常需要将查出的数据导成Excel表返给用户,方便用户对数据的处理和汇总.这里我将导出Excel表格的代码做一个总结,这也是我项目中经常用到的,代码简单易懂,使用 ...
- k8s 入门系列之集群安装篇
关于kubernetes组件的详解介绍,请阅读上一篇文章<k8s入门系列之介绍篇> Kubernetes集群安装部署 •Kubernetes集群组件: - etcd 一个高可用的K/V键值 ...
- mysql--MySQL数据库的简单认识
一.MySQL介绍 1.mysql版本 双授权版本:社区版(完全免费,功能也够nb了)和商业版(更好,功能更多更强大一些,但是收费,VIP,有售后服务,也会参考和吸收社区版的一些nb的功能,安全性和稳 ...
- C# RDLC报表不出现预览窗体直接输出到打印机
#region 直接打印区域 /// <summary> /// 直接打印到打印机 /// </summary> /// <param name="report ...
- ROS(机器人URDF模型优化)
URDF模型 xacro优化后的URDF模型 1.精简模型代码(创建宏定义,文件包含) 2.提供可编程接口(常量,变量,数学计算,条件语句) 常量定义: name:base_length的值value ...
- 《快学Scala》第二章 控制结构和函数
- 为解决Samba windows 无法访问 尝试过的方法
1, 通过 vi /etc/sysconfig/selinux 把 SELINUX=enforcing 修改为SELINUX= disable 退出保存,并且重启.(设置了) 2, 把wind ...