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 ...
随机推荐
- 自我介绍及注册github和上传文件
自我介绍: 周侃 年龄20 喜好:玩游戏,赚钱,交际 理想:想要改变中国手游界颓靡的时代,让它进入新次元. 注册github,以及上传文件: 今天给大家来讲解下如何注册githup 当我们打开gith ...
- [转载]灵动思绪EF(Entity FrameWork)
很久之前就想写这篇文章了,但是由于种种原因,没有将自己学习的EF知识整理成一片文章.今天我就用CodeFirst和ModelFirst两种方式的简单案例将自己学习的EF知识做个总结. 在讲解EF之前, ...
- Spring学习(七)——开发Web Service的几种方式
本文作者在学习使用Java开发Web Service(不包括Restful)时,由于不知道Java有这么多框架支持开发Web Service一度陷入迷惘,不知道这些框架各有 什么不同,各有什么优缺点. ...
- SQL datediff()函数 时间差
定义和用法 DATEDIFF() 函数返回两个日期之间的天数. 语法 DATEDIFF(datepart,startdate,enddate) startdate 和 enddate 参数是合法的日期 ...
- ES6——内置对象的扩展
字符串的扩展 //模版字符串 let flag=true; let hml=`<ul> <li> <span></span> <span>& ...
- util.Date转化成sql.date格式
https://www.cnblogs.com/zhaotiancheng/p/6413067.html
- java学习笔记—第三方数据库连接池包1(29)
第一步:导入dbcp包 第二步:通过核心类连接数据 BasicDataSource它是javax.sql.DataSrouce的子类. 一个工具类:BasicDataSourceFactory. 手工 ...
- CentOS7.x安装时的分区方案
-------------------------------------------------分区方案描述--------------------------------------------- ...
- day 11课后作业
# -*- coding: utf-8 -*-# @Time : 2019/1/3 20:03# @Author : Endless-cloud# @Site : # @File : day 11 课 ...
- python 之 爬普房网
from bs4 import BeautifulSoupimport reimport requestsimport pandas## pa pufangwangclass down(object) ...