皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

上图为 8 皇后问题的一种解法。

给定一个整数 n,返回所有不同的 皇后问题的解决方案。

每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。

示例:

输入: 4
输出: [
[".Q..", // 解法 1
"...Q",
"Q...",
"..Q."], ["..Q.", // 解法 2
"Q...",
"...Q",
".Q.."]
]
解释: 4 皇后问题存在两个不同的解法。 思路:
按上述解法1queens数组为[1,3,0,2] 因为没以行就一个queens所以不用考虑行了,只纪录该行哪一列放了Q
先建立queens数组(建立好后用addSolution填好Q和point),用回溯的方法,把所有满足题意的方式都穷举出来.
TIME:O(N^N)?
SPACE:O(N)
 class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> res = new ArrayList<>();
if(n <= 0)return res; helper(res,new int[n],0);
return res;
}
public void helper(List<List<String>> res,int[] queens,int pos){
if(pos == queens.length){
addSolution(res,queens);
return;
} for(int i = 0;i < queens.length;i++){
queens[pos] = i;
if(isValid(queens,pos)){
helper(res,queens,pos+1);
}
}
}
public boolean isValid(int[] queens,int pos){
for(int i = 0;i < pos;i++){
if(queens[i] == queens[pos]){//在同一列
return false;
}else if(Math.abs(queens[pos] - queens[i]) == Math.abs(i - pos)){//在同一对角线上
return false;
}
}
return true;
}
public void addSolution(List<List<String>> res ,int[] queens){
List<String> list = new ArrayList<>();
for(int i = 0;i < queens.length;i++){
StringBuilder sb = new StringBuilder();
for(int j = 0;j < queens.length;j++){
if(queens[i] == j){
sb.append('Q');
}else{
sb.append('.');
}
}
list.add(sb.toString()); }
res.add(list);
}
}

2019-05-08 20:51:23

python版本,秒杀java

 class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
def DFS(queens,xy_dif,xy_sum):
p = len(queens)
if p == n:
result.append(queens)
return None
for q in range(n):
if q not in queens and p-q not in xy_dif and p+q not in xy_sum:
DFS(queens+[q],xy_dif+[p-q],xy_sum+[p+q])
result = []
DFS([],[],[])
return [["."*i + "Q" + "."*(n-i-1) for i in sol]for sol in result]

2020-01-13 16:38:14

LeetCode--051--N皇后(java)-star的更多相关文章

  1. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  2. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. Java for LeetCode 051 N-Queens

    Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a d ...

  6. Java实现 LeetCode 52 N皇后 II

    52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...

  7. Java实现 LeetCode 51 N皇后

    51. N皇后 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问题的解决 ...

  8. [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 ...

  9. 八皇后java算法

    import java.util.Date; public class EightQueen { public static void main(String[] args) {  long star ...

  10. 极限编程,最强N皇后JAVA解题代码,4秒出15皇后,33秒出16皇后

    私人博客原文链接来自:http://www.hexcode.cn/article/show/eight-queen 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,循环控制及其优化 8皇后以 ...

随机推荐

  1. qbzt day3 上午

    内容提要 堆 lca(最近公共祖先) st表 hash 并查集 树状数组 线段树 数据结构 1.堆 Priority_queue 他滋兹:插入删除查询最大值(最小值) 分为大根堆小根堆 2.LCA 首 ...

  2. laravel使用artisan报错SQLSTATE[42S02]: Base table or view not found: 1146

    说明你在应用初始化阶段使用到了数据库层面的东西,然而当时数据库不存在这个表/字段,所以会报错 需要在初始化比如 config 目录配置中,使用了数据库,在使用前需要添加一层判断,如果不存在 你需要用到 ...

  3. ecshop后台增加模块菜单详细教程

    我们有时候针对ecshop如此开发,想在后台加一些菜单,最模板以前提供过教程,但是并非很系统,今天最模板抛砖引玉图文教程告诉大家:如何在ecshop后台增加模块菜单! 首先需要修改四个文件:inc_p ...

  4. 【ABAP系列】SAP ABAP 优化LOOP循环的一点点建议

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 优化LOOP循 ...

  5. 疯狂Java学习

    面向对象(下) 6.3:类成员:又讲了一遍static修饰的类成员:   Singleten(单例)类: 通过封装的方式,实现了一个类只能创建一次,应该是为了更好编写代码,创造的一个概念. 6.4:f ...

  6. from 表单回车自动提交

    自动提交的情况 1 表单只有单个输入框 2 type=‘submit  这里注意button默认type为submit 解决方法 1 添加一个隐藏的输入框 2 form添加属性 onsubmit=&q ...

  7. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  8. Python 中的垃圾回收机制

    GC作为现代编程语言的自动内存管理机制,专注于两件事:1. 找到内存中无用的垃圾资源 2. 清除这些垃圾并把内存让出来给其他对象使用.GC彻底把程序员从资源管理的重担中解放出来,让他们有更多的时间放在 ...

  9. H-Updating a Dictionary (模拟)

    In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...

  10. 在react中用装饰器来装饰connect

    假设我们在react中有如下header组件: import React, { PureComponent } from 'react'; import { connect } from 'react ...