LeetCode——N-Queens
Description:
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.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."], ["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
] 经典的n皇后问题,有一个经典的回溯法。具体请参考:N-Queens 在这个问题中需要用List来记录sum个解矩阵,并返回。 代码:
public class Solution {
public int[] x; //当前解
public int sum; //解的个数
public int n; //皇后个数
public List<List<String>> resList;
public List<List<String>> solveNQueens(int n) {
this.resList = new ArrayList<List<String>>();
for(int i=0; i<n; i++) {
}
this.x = new int[n + 1];
for(int i=0; i<=n; i++) {
this.x[i] = 0;
}
this.sum = 0;
this.n = n;
backTrack(1);
return resList;
}
public void backTrack(int t) {
if(t > n) {
List<String> pRes = new ArrayList<String>();
for(int i=1; i<=n; i++) {
StringBuilder row = new StringBuilder();
for(int j=1; j<=n; j++) {
if(this.x[i] == j) {
row.append("Q");
}
else {
row.append(".");
}
}
pRes.add(row.toString());
}
resList.add(pRes);
sum ++;
}
else {
for(int i=1; i<=n; i++) {
this.x[t] = i;
if(place(t)) {
backTrack(t + 1); //回溯
}
}
}
}
/**
* 判断当前位置是否合法
*/
public boolean place(int k) {
for(int j=1; j<k; j++) {
if(Math.abs(j-k) == Math.abs(this.x[j]-this.x[k]) ||
this.x[j] == this.x[k]) {
return false;
}
}
return true;
}
}
LeetCode——N-Queens的更多相关文章
- [Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【leetcode】1222. Queens That Can Attack the King
题目如下: On an 8x8 chessboard, there can be multiple Black Queens and one White King. Given an array of ...
- [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 ...
- [CareerCup] 9.9 Eight Queens 八皇后问题
9.9 Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board so that non ...
- Leetcode | N-Queens I & II
N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...
- [LeetCode]题解(python):051-N-Queens
题目来源 https://leetcode.com/problems/n-queens/ The n-queens puzzle is the problem of placing n queens ...
- [Leetcode][Python]52: N-Queens II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...
- [Leetcode][Python]51: N-Queens
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 51: N-Queenshttps://oj.leetcode.com/pro ...
随机推荐
- map和object互相转换
/** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToObject(Map<String ...
- NFS根文件系统
按照以前文档可以正确制作根文件系统,并且开发板可正确nfs挂测主机目录. 现只需修改bootargs,使内核启动时挂测文件系统即可.setenv bootargs mem=64M console=tt ...
- lua 的io操作,非常详细
Lua 标准库 - 输入输出处理(input and output facilities) I/O库提供两种不同的方式进行文件处理 1.io表调用方式:使用io表,io.open将返回指定文件的描述, ...
- 測试Service
<strong><span style="font-size:18px;">自己定义Service:</span></strong> ...
- vector、map删除当前记录
map<string, string> sMap; map<string, string>::iterator iter; for(iter = sMap.begin();it ...
- c++下面的一个单例
Public FileManager { private FileManager(); public static FileManager Instance=new FileManager(); } ...
- 关于Random中的随机数种子Seed
Random初始化的时候,可以以一个INT32作为参数,称为seed,MSDN上的解释是:“伪随机数是以相同的概率从一组有限的数字中选取的......随机数的生成是从种子值开始......” 所有标准 ...
- Unity3d之Shader开发介绍
Shader是为渲染管线中的特定处理阶段提供算法的一段代码.shader是伴随着可编程渲染管线出现的,它的出现使得游戏开发者可以对渲染过程加以控制,拥有更大的创作空间,因此Shader的出现可以看作是 ...
- Unity脚本中各函数成员的生命周期
在学习Unity时,掌握如何编写脚本是必须掌握的一项基本技能.但是关于Unity的游戏脚本中各函数的生命周期是怎样开始和结束的,它们的执行顺序是如何安排的?这一点我们要清楚的了解. 我们知道Unity ...
- 数据库ADO方式读取图片
void Caccess_test_1Dlg::OnBnClickedButton3()//将偏振图像存入数据库 { // TODO: 在此添加控件通知处理程序代码 if (!PathFileExis ...