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

上图为 8 皇后问题的一种解法。
给定一个整数 n,返回所有不同的 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的更多相关文章
- 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 + ...
- 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 ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- 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 ...
- Java for LeetCode 051 N-Queens
Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a d ...
- Java实现 LeetCode 52 N皇后 II
52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...
- Java实现 LeetCode 51 N皇后
51. N皇后 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问题的解决 ...
- [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 ...
- 八皇后java算法
import java.util.Date; public class EightQueen { public static void main(String[] args) { long star ...
- 极限编程,最强N皇后JAVA解题代码,4秒出15皇后,33秒出16皇后
私人博客原文链接来自:http://www.hexcode.cn/article/show/eight-queen 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,循环控制及其优化 8皇后以 ...
随机推荐
- 文件格式-CVS:CVS
ylbtech-文件格式-CVS:CVS 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_07-网络编程_第2节 TCP协议_4_TCP通信的服务器端代码实现
表示服务器的类是ServerSocket 启动服务器端 再启动客户端 客户端代码修改获取服务端会写的数据 先启动服务器端,再启动客户端 客户端打印: 服务器端读取:
- win10编写8086汇编程序(dosbox)
有部分同学反馈.在使用edit命令来编写汇编程序时遇到问题,由于模拟器没有edit程序,所以要换一种方式编写源程序.下面是完整的演示. 视频链接:http://www.bilibili.com/vid ...
- Common Linux Commands 日常工作常用Linux命令
How to know CPU info cat /proc/cpuinfo arch How to know memory info: cat /proc/meminfo ...
- AWS Cloud Practioner 官方课程笔记 - Part 1
课程笔记: 1. 3种访问AWS服务的方式: GUI, CLI, SDK 前两种是用户用来访问的,SDK可以让程序调用去访问服务. 2. core services 以及通用的use cases Am ...
- IDEA常用快揵键
IDEA常用快揵键 工作中常用IDEA快捷键 参见博客:https://www.cnblogs.com/zhangpengshou/p/5366413.html Double shift --- ...
- MyBatis Generator 生成的example 使用 and or 简单混合查询
MyBatis Generator 生成的example 使用 and or 简单混合查询 参考博客:https://www.cnblogs.com/kangping/p/6001519.html 简 ...
- ntp局域网时间同步操作
需求:局域网里面有两台电脑需要同步时间 一台windows,一台Linux.把windows当作服务器 windows10自带ntp服务器,可以按如下步骤进行设置 1. 打开注册表编辑器,在运行里面输 ...
- JavaSE编码试题强化练习2
1.编写递归算法程序:一列数的规则如下: 0.1.1.2.3.5.8.13.21.34...... 求数列的第40位数是多少. public class TestRecursion { public ...
- [Python3] 018 if:我终于从分支中走出来了
目录 0. 谁是主角 1. 从三大结构说起 (1) 顺序 (2) 分支 1) 分支的基本语法 2) 双向分支 3) 多路分支 (3) 循环 0. 谁是主角 分支是主角 我前面几篇随笔提到 if 不下2 ...