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安装测试环境还是不错的.本系列文 ...
随机推荐
- 【题解】[USACO12MAR]Cows in a Skyscraper G
题目链接 题目大意:给定一个集合\(S\),给一个限制条件\(P\),要求划分集合,使得每一个子集\(A\in S\),\(A\)满足限制条件\(P\),且划分总数最小. 注意到数据范围\(n< ...
- 【MySQL Errors】Table 'xxx' is marked as crashed and should be repaired 的解决方案
现象描述 访问 Zabbix Web,出现如下错误提示: • Error in query [SELECT * FROM history_uint h WHERE h.itemid='25067' O ...
- 《New Horizon College English》2--长篇阅读技能指南
<New Horizon College English>2--长篇阅读技能指南 <长篇阅读>目的是提升学生的英语阅读技能和限时获取信息的能力.<长篇阅读>共四级, ...
- 35岁老半路程序员的Python从0开始之路
9年的ERP程式开发与维护,继而转向一年的售前,再到三年半的跨行业务,近4的兜兜转转又转回来做程式了,不过与之前不同的,是这次是新的程序语言Python, 同时此次是为了教学生而学习! 从今天开始,正 ...
- JProfiler 教程 使用说明
JProfiler (本文原创转载请注明) 简介 JProfiler是一个重量级的JVM监控工具,提供对JVM精确监控,其中堆遍历.CPU剖析.线程剖析看成定位当前系统瓶颈的得力工具.可以统计压 ...
- 2017年 实验四 B2C模拟实验
实验四 B2C模拟实验 [实验目的] 掌握网上购物的基本流程和B2C平台的运营 [实验条件] ⑴.个人计算机一台 ⑵.计算机通过局域网形式接入互联网. (3).奥派电子商 ...
- day18 Pyhton学习 匿名函数
匿名函数 别称: lambda表达式 函数,没有名字 def wahaha(n):#wahaha return n**2 print(wahaha.__name__) qqxing = lambda ...
- 请求https接口时报错:Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificat,安装certifi
如何解决SSL 根证书验错误: 一种解决方法是:verify=False 在session.request 里面: session.post(post_url,data=post_data,heade ...
- 扫描仪扫描文件处理-ABBYY对扫描版PDF文字加黑加粗、去除背景漂白
1. 设置ABBYY自动歪斜矫正: 2. 设置导出PDF参数: 3. PDF文字加黑加粗.去除背景漂白步骤:3.1 ABBYY - 打开扫描版PDF文档3.2 ABBYY - 编辑图像3.3 等级 - ...
- 调试与优化:一次数据中心看板 T+1 改 T+0 优化过程
背景 团队目前在做一个用户数据看板(下面简称看板),基本覆盖用户的所有行为数据,并生成分析数据,用户行为数据来源于多个数据源(餐饮.生活日用.充值消费.交通出行.通讯物流.交通出行.医疗保健.住房物业 ...