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个方块 ...
随机推荐
- java.lang.NoClassDefFoundError: antlr/ANTLRException
在用Hibernate进行查询时,出现这样的错误:Exception in thread "main" java.lang.NoClassDefFoundError: antlr/ ...
- 5.9-2比较str1和str2截取后的子串
package zfc; public class ZfcShcq { public static void main(String[] args) { // TODO Auto-generated ...
- 【BZOJ 3732】 Network Kruskal重构树+倍增LCA
Kruskal重构树裸题, Sunshine互测的A题就是Kruskal重构树,我通过互测了解到了这个神奇的东西... 理解起来应该没什么难度吧,但是我的Peaks连WA,,, 省选估计要滚粗了TwT ...
- keep_on _coding——js_good_parts
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Mathematical operation
(1)Using let let result=2+1 let result=2-1 let result=2*1 let result=2/1(2) Using bracket echo $(($p ...
- 【HDU 4150】Powerful Incantation
题 题意 给你s1,s2两个字符串,求s1中有多少个s2 代码 #include<stdio.h> #include<string.h> int t,len1,len2,pos ...
- rpm常用选项
httpd-2.2.15-39.el6.centos.x86_64.rpmhttpd - 2.2.15- 39.el6.centos. x86_64 .rpm软件名称- ...
- BZOJ-2190 仪仗队 数论+欧拉函数(线性筛)
今天zky学长讲数论,上午水,舒爽的不行..后来下午直接while(true){懵逼:}死循全程懵逼....(可怕)Thinking Bear. 2190: [SDOI2008]仪仗队 Time Li ...
- 【bzoj1853】 Scoi2010—幸运数字
http://www.lydsy.com/JudgeOnline/problem.php?id=1853 (题目链接) 今天考试考了容斥,结果空知道结论却不会写→_→ 题意 求区间中不含6,8两个数字 ...
- c++ 类型安全
类型安全很大程度上可以等价于内存安全,类型安全的代码不会试图访问自己没被授权的内存区域.“类型安全”常被用来形容编程语言,其根据在于该门编程语言是否提供保障类型安全的机制:有的时候也用“类型安全”形容 ...