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个方块 ...
随机推荐
- text-align:justify_内容居中对齐
一直发现text-align : justify这个对齐方式不好使,都不知道为什么么么哒: 因为两端对齐的这个行的结束要一个有空字符串或者别的不可见的字符,用户代理会把这个行的最后几个字符往右边拉,实 ...
- 常用的Java 架包(jar)的用途
前言:如果需要在项目中引入jar包,可以采用maven,配置方式在 http://mvnrepository.com 查询 slf4j-api 简介:slf4j并不是一种具体的日志系统,而是一个用户 ...
- 图解Android - System Service 概论 和 Android GUI 系统
通过 图解Android - Binder 和 Service 一文中,我们已经分析了Binder 和 Service的工作原理.接下来,我们来简要分析Android 系统里面都有哪些重要的Servi ...
- yii2URL美化
yii2的url 域名/index.php?r=site%2Findex 实际为 域名/index.php?r=site/index 可以美化下 可以在main.php中配置 'components' ...
- python 学习笔记2(list/directory/文件对象/模块/参数传递)
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象. 11. list list是一个类.每个列表都属于该类. >>>nl = [1,2,5,3, ...
- opencv笔记2:图像ROI
time:2015年 10月 03日 星期六 12:03:45 CST # opencv笔记2:图像ROI ROI ROI意思是Region Of Interests,感兴趣区域,是一个图中的一个子区 ...
- Linux Kernel sys_call_table、Kernel Symbols Export Table Generation Principle、Difference Between System Calls Entrance In 32bit、64bit Linux
目录 . sys_call_table:系统调用表 . 内核符号导出表:Kernel-Symbol-Table . Linux 32bit.64bit环境下系统调用入口的异同 . Linux 32bi ...
- PL/0编译器(java version) - Interpreter.java
1: package compiler; 2: 3: import java.io.BufferedReader; 4: import java.io.BufferedWriter; 5: imp ...
- 微信公众平台开发接口PHP SDK完整版
<?php /* 方倍工作室 http://www.fangbei.org/ CopyRight 2015 All Rights Reserved */ define("TOKEN&q ...
- Virtualbox虚拟机安装Ubuntu图文版
这篇文章给大家介绍一下如何在Windows系统下的Virtual Box虚拟机软件中安装Ubuntu系统. 适用环境:Windows系统作为物理机,在此平台上搭建一个Virtual Box虚拟平台,在 ...