Leetcode 51
//看了一次解析后,一次AC,用一个pos记录行列。
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> res;
vector<int> pos(n,-);
DFS(pos,,res);
return res;
} void DFS(vector<int>& pos,int row,vector<vector<string>>& res){
int n = pos.size();
if(row == n){
vector<string> out(n,string(n,'.')); //string也有这种填充写法
for(int i=;i < n;i++){
out[i][pos[i]] = 'Q';
}
res.push_back(out);
}
else{
for(int i=;i < n;i++){
if(isfine(pos,row,i)){
pos[row] = i;
DFS(pos,row+,res);
pos[row] = -;
}
}
}
} bool isfine(vector<int>& pos,int row,int col){
for(int i=;i < row;i++){
if(pos[i] == col || abs(row-i) == abs(col-pos[i]))
return false;
}
return true;
}
};
Leetcode 51的更多相关文章
- [LeetCode] 51. N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- LeetCode - 51. N-Queens
51. N-Queens Problem's Link ------------------------------------------------------------------------ ...
- leetcode@ [51/52] N-Queens
https://leetcode.com/problems/n-queens/ class Solution { public: void dfs(vector<vector<string ...
- leetcode 51. N-Queens 、52. N-Queens II
51. N-Queens 使用isValid判断当前的位置是否合法 每次遍历一行,使用queenCol记录之前行的存储位置,一方面是用于判断合法,另一方面可以根据存储结果输出最终的结果 class S ...
- LeetCode: 51. N-Queens(Medium)
1. 原题链接 https://leetcode.com/problems/n-queens/description/ 2. 题目要求 游戏规则:当两个皇后位于同一条线上时(同一列.同一行.同一45度 ...
- Java实现 LeetCode 51 N皇后
51. N皇后 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问题的解决 ...
- leetcode 51. N皇后 及 52.N皇后 II
51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...
- LeetCode(51)- Count and Say
题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...
- LeetCode 51 N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [leetcode]51. N-QueensN皇后
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
随机推荐
- Code Forces 652C Foe Pairs
C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- HDU 5652 India and China Origins(并查集)
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- Tomcat 系统架构
https://www.ibm.com/developerworks/cn/java/j-lo-tomcat1/index.html 2010 年 5 月 20 日发布 本文以 Tomcat 5 为基 ...
- Information:java: Errors occurred while compiling module 'spring'
IntellJ Idea遇到Errors occurred while compiling module的解决方法 - sully2008的专栏 - CSDN博客 https://blog.csdn. ...
- 【mlflow】打包:npm run build + python setup.py sdist
mlflow是一个开源机器学习平台 最近需要使用一个它的最新版本,但是这个最新版本没有git包,无法通过pip install安装,需要打包安装. 打包完之后在项目的dist文件夹中有打包后的压缩包, ...
- 双态运维:如何让CMDB配置维护更贴近人性
近来很多行业内的大佬关于CMDB连连发声,CMDB的关注度持续高涨,CMDB的前生就是长满雀斑的丑媳妇,扭扭捏捏不受待见这么多年,终于熬出头要见公婆了.哎,她的贤惠谁能懂? 言归正传,在拜读了多篇大牛 ...
- 获取List、Set、Map等字段的泛型参数
测试类加单元测试方法,运行结果在注释里面: package temp; import org.junit.Test; import java.lang.reflect.Field; import ja ...
- android showDialog用法
protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub switch(id){ case 10: re ...
- 3.3 Templates -- Conditionals(条件语句)
有时候你可能仅仅想展现模板的一部分,如果属性存在的话. 1. 我们可以使用{{if}}去有条件的渲染一块: {{#if person}} Welcome back, <b>{{person ...
- shoes的安装前后(一)
最近看到一个模型用到了shoes,准备自己试一试.搞了半天,也安装不成功.直接安装包,gem install shoes,失败, 从rubygems上下载最新版本的shoes 然后安装,成功了.随便写 ...