leetcode99:n-queens
题目描述

[".Q..", // 解法 1
"...Q",
"Q...",
"..Q."], ["..Q.", // 解法 2
"Q...",
"...Q",
".Q.."]
]
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens'
placement, where'Q'and'.'both indicate a queen and an empty space
respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[".Q..", // 解法 1
"...Q",
"Q...",
"..Q."], ["..Q.", // 解法 2
"Q...",
"...Q",
".Q.."]
class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
vector <vector<string>> res;
vector<string> cur(n,string(n,'.'));
dfs(res,cur,n,0);
return res;
}
void dfs(vector <vector<string>> &res,vector <string> &cur,int &n,int row){
if (row==n){
res.push_back(cur);
return ;
}
for (int j=0;j<n;j++){
if (isValid (cur,n,row,j)){
cur[row][j]='Q';
dfs(res,cur,n,row+1);
cur[row][j]='.';
}
}
}
bool isValid(vector<string> &cur,int &n ,int row,int col){
for (int i=0;i<row;i++){
if (cur[i][col]=='Q'){
return false;
}
}
for (int i=row-1,j=col-1;i>=0 && j>=0;i--,j--){
if (cur[i][j]=='Q'){
return false;
}
}
for (int i=row-1,j=col+1;i>=0 && j<n;i--,j++){
if (cur[i][j]=='Q'){
return false;
}
}
return true;
}
};
leetcode99:n-queens的更多相关文章
- Jeff Somers's N Queens Solutions 最快的n皇后算法
/* Jeff Somers * * Copyright (c) 2002 * * jsomers@alumni.williams.edu * or * allagash98@yahoo.com * ...
- [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 ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- lintcode 中等题:N Queens N皇后问题
题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...
- Codeforces Gym 100650D Queens, Knights and Pawns 暴力
Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...
- Poj 3239 Solution to the n Queens Puzzle
1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS ...
- Pat1128:N Queens Puzzle
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- PAT 1128 N Queens Puzzle
1128 N Queens Puzzle (20 分) The "eight queens puzzle" is the problem of placing eight ch ...
- kolla queens on centos7.5 -all in one
目录 环境准备 开始配置 快照,快照,快照 pull镜像并部署 登录配置OpenStack 环境准备 我这里用workstation创建了一个虚拟机,安装centos7.5 mini系统,这台虚拟机上 ...
- openstack系列文章(1)devstack安装测试Queens
1.在OpenStack 圈子中,有这么一句名言:”不要让朋友在生产环境中运行DevStack.但是初学者在没有掌握OpenStack CLI的情况下用devstack安装测试环境还是不错的.本系列文 ...
随机推荐
- 状压DP——【蜀传之单刀赴会】
某王 老师今天考了一套三国题,AK了...就挑一道最恶心的题来写一写吧. 题目描述: [题目背景] 公元215年,刘备取益州,孙权令诸葛瑾找刘备索要荆州.刘备不答应,孙权极为恼恨,便派吕蒙率军取长 ...
- .NET 云原生架构师训练营(模块一 架构师与云原生)--学习笔记
目录 什么是软件架构 软件架构的基本思路 单体向分布式演进.云原生.技术中台 1.1 什么是软件架构 1.1.1 什么是架构? Software architecture = {Elements, F ...
- kafka配置文件详解
kafka的配置分为 broker.producter.consumer三个不同的配置 一 .BROKER 的全局配置最为核心的三个配置 broker.id.log.dir.zookeeper.con ...
- CMD/ENTROYPOINT区别
CMD/ENTROYPOINT区别 相同点:都是指定一个容器:启动时要运行的命令 不同点(重点): CMD: dockerfile中可以有多个CMD指令,但是只有最后一个生效,CMD会被docker ...
- GC调优-XX:PrintGCDetails深度解析
查看程序运行GC的运行情况 资源充足的GC情况 新生代 老年代 元空间 因为现在资源充足没有发生GC *案例:将JVM初始化内存与最大内存(防止内存抖动,反复GC)调至10m,new一个50m的数组对 ...
- 浅谈Samsung Exynos4412处理器
转载于:http://www.cnblogs.com/android210/archive/2013/01/16/2862349.html Topic:浅谈Samsung Exynos4412处理器( ...
- protoc-c 阅读笔记
以前和山哥做过类似的,最近想起来,抽空又看了下 protoc-c. 山哥做的报文流向: rpc -> lydtree -> motree -> struct 涉及的细节很多 1) l ...
- DM9000时序设置
想了解一下DM9000的移植修改原理,所以分析了一下时序图和引脚连接 首先看一下DM9000的引脚和MINI2440的引脚连接 DM9000 MINI2440 功能描述 SD0 DA ...
- MeteoInfoLab脚本示例:Trajectory
示例读取HYSPLIT模式输出的气团轨迹数据文件,生成轨迹图层,并显示轨迹各节点的气压图.脚本程序: f = addfile_hytraj('D:/MyProgram/Distribution/jav ...
- git commit 代码提交规范
格式 type: description 1. type 类型 type 是 commit 的类别,只允许如下几种标识: fix: 修复bug add: 新功能 update: 更新 refactor ...