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模板和回调机制学习总结
最近看spring的JDBCTemplete的模板方式调用时,对模板和回调产生了浓厚兴趣,查询了一些资料,做一些总结. 回调函数: 所谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个 ...
- Sublime-jQueryDocs
Package Control Messages======================== jQueryDocs---------- This package shows a selected ...
- ansible 的组件inventory
P44 Ansible 的默认的inventory的是一个静态的ini格式的文件/etc/ansible/hosts. 我们还可以通过ansible_hosts环境变脸指定或者运行ansible和an ...
- DLUTOJ #1394 Magic Questions
传送门 Time Limit: 3 Sec Memory Limit: 128 MB Description Alice likes playing games. So she will take ...
- CSU 1115 最短的名字
传送门 Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description 在一个奇怪的 ...
- POJ1037A decorative fence(动态规划+排序计数+好题)
http://poj.org/problem?id=1037 题意:输入木棒的个数n,其中每个木棒长度等于对应的编号,把木棒按照波浪形排序,然后输出第c个; 分析:总数为i跟木棒中第k短的木棒 就等于 ...
- IDEA 从SVN检出项目相关配置
1.新建好一个工程,然后通过SVN检出项目 2.检出后一般tomcat的环境是配置好的,点击上方Project Structure按钮,弹出窗体,查看Project项,一般没问题,如果要配置就配置Pr ...
- javascript中parentNode,childNodes,children的应用详解
本篇文章是对javascript中parentNode,childNodes,children的应用进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 "parentNode&qu ...
- linux下Nginx服务器安装教程
序:Nginx服务器安装总结而已,不是教程. 安装的过程中出现了一些问题,原因我的云主机是纯净版,所以很多依赖包都没有.其中安装过程中就发现perl库缺少和openssl库缺少,因此我手动安装的这两款 ...
- Sqlserver日期函数应用
1.获取当前时间 SELECT GETDATE() AS '当前日期' , DATENAME(year, GETDATE()) AS '年' , DATENAME(m ...