N-Queens | & N-Queens II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
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.
There exist two distinct solutions to the 4-queens puzzle:
[
// Solution 1
[".Q..",
"...Q",
"Q...",
"..Q."
],
// Solution 2
["..Q.",
"Q...",
"...Q",
".Q.."
]
]
class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> allList = new ArrayList<>();
if (n <= ) return allList;
Integer[] row = new Integer[n];
List<List<Integer>> integerList = new ArrayList<>();
queen(, n, row, new ArrayList<>());
char[] arr = new char[n];
Arrays.fill(arr, '.');
for (List<Integer> list : integerList) {
List<String> temp = new ArrayList<String>();
for (int i = ; i < list.size(); i++) {
arr[list.get(i)] = 'Q';
temp.add(new String(arr));
arr[list.get(i)] = '.';
}
allList.add(new ArrayList<String>(temp));
}
return allList;
}
public void queen(int n, int count, Integer[] row, List<List<Integer>> list) {
if (n == count) {
list.add(new ArrayList<Integer>(Arrays.asList(row)));
return;
}
for (int i = ; i < count; i++) {
row[n] = i;
if (isSatisfied(n, row)) {
queen(n + , count, row, list);
}
}
}
public boolean isSatisfied(int n, Integer[] row) {
for (int i = ; i < n; i++) {
if (row[i] == row[n]) return false;
if (Math.abs(row[n] - row[i]) == n - i) return false;
}
return true;
}
}
N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
For n=4, there are 2 distinct solutions.
class Solution {
public int totalNQueens(int n) {
int[] row = new int[n];
int[] current = new int[];
queen(, n, row, current);
return current[];
}
public void queen(int n, int count, int[] row, int[] current) {
if (n == count) {
current[]++;
return;
}
for (int i = ; i < count; i++) {
row[n] = i;
if (isSatisfied(n, row)) {
queen(n + , count, row, current);
}
}
}
public boolean isSatisfied(int n, int[] row) {
for (int i = ; i < n; i++) {
if (row[i] == row[n]) return false;
if (Math.abs(row[n] - row[i]) == n - i) return false;
}
return true;
}
}
N-Queens | & N-Queens II的更多相关文章
- 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...
- “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...
- 52. N-Queens II
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode--052--N皇后II(java)
n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: 输入 ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- [Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [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 ...
- [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 ...
- 用试探回溯法解决N皇后问题
学校数据结构的课程实验之一. 数据结构:(其实只用了一个二维数组) 算法:深度优先搜索,试探回溯 需求分析: 设计一个在控制台窗口运行的“n皇后问题”解决方案生成器,要求实现以下功能: 由n*n个方块 ...
随机推荐
- form表单提交和ajax提交的区别
form表单是整个页面跳到服务器的地址然后提交数据: ajax是往这个地址post数据 <form style="padding:0px;margin:0px;" targe ...
- 【poj3159】 Candies
http://poj.org/problem?id=3159 (题目链接) 题意 有n个小朋友,班长要给每个小朋友发糖果.m种限制条件,小朋友A不允许小朋友B比自己多C个糖果.问第n个小朋友最多比第1 ...
- 【poj1236】 Network of Schools
http://poj.org/problem?id=1236 (题目链接) 题意 给定一个有向图,求:1.至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点:2.至少要加多少条边,才能使得从任 ...
- 使用Java的嵌套循环打印出平行四边形、等腰三角形、棱形、矩形的星星图案(Java工程师面试必备)
第一遍是看了视频,听老师讲解嵌套循环的使用,然后到星星图形这一步,当时都觉得听明白了,但是自己去做,就是写不出来 第二遍看了赵老师的教程,看了好熟悉的感觉,还是自己写不出来 第三遍找网上关于图形的嵌套 ...
- eclipse中新建python项目报错:Project interpreter not specified
eclipse-windows-preferences-python
- URLDecoder与URLEncoder
网页中的表单使用POST方法提交时,数据内容的类型是 application/x-www-form-urlencoded,这种类型会: 1.字符"a"-"z", ...
- JavaScript实现全排列
<html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body> ...
- 对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
一. AES对称加密: AES加密 分组 二. 分组密码的填充 分组密码的填充 e.g.: PKCS#5填充方式 三. 流密码: 四. 分组密码加密中的四种模式: 3.1 ECB模式 优点: 1. ...
- Linux 查看CPU,内存,硬盘 !转
Linux 查看CPU,内存,硬盘 本文转自:http://hi.baidu.com/mumachuntian/item/a401368dbe8a66cab07154e8 1 查看CPU 1.1 查看 ...
- 微信内测"微视频" 朋友圈可以发6-8秒短视频
我们在发朋友圈时现在只能发文字和图片,图片又分为从相册中选择和直接拍摄照片,以后朋友圈拍6-8秒短视频可能是一种新的内容形式,作为文字和图片外的补充.因为微信开始内测新功能“微视频”,其产品理念与微视 ...