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的更多相关文章

  1. [Leetcode] n queens ii n皇后问题

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  2. 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  3. 【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 ...

  4. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. [LeetCode]题解(python):051-N-Queens

    题目来源 https://leetcode.com/problems/n-queens/ The n-queens puzzle is the problem of placing n queens ...

  9. [Leetcode][Python]52: N-Queens II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...

  10. [Leetcode][Python]51: N-Queens

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 51: N-Queenshttps://oj.leetcode.com/pro ...

随机推荐

  1. linux rename 和?????

    [root@ob2 mytmp]# ls01.txt  02.txt  03.txt  aa2.txt  aa.sh  aa.txt  tp_2017-09-143516.tar.gz  tt [ro ...

  2. thymeleaf+bootstrap,onclick传参实现模态框中遇到的错误

    一个困扰了N久的问题... 网上大多帖子是这么写的 onclick调javascript函数时,不能直接使用onclick=“editUser(${prod.id})”,这样会报错,需要修改成如下的格 ...

  3. sparkr基本操作1

    由于装的sparkr是1.4版本的,老版本的很多函数已经不再适用了. 在2台服务器的组成的集群中测试了一版数据,熟悉下这个api的基本操作.​ libpath <- .libPaths() li ...

  4. 基本上每个应用程序领域的程序员都有使用 C++

    C++ 的使用基本上每个应用程序领域的程序员都有使用 C++. C++ 通常用于编写设备驱动程序和其他要求实时性的直接操作硬件的软件. C++ 广泛用于教学和研究. 任何一个使用苹果电脑或 Windo ...

  5. linux -- 进程管理和作业控制

    一. 作业控制 1. 直接将命令放到后台"执行": &  [root @test /root ]# command & 范例: [root @test /root] ...

  6. php多语言截取字符串函数

    <?php header("Content-Type:text/html;charset=utf-8"); function msubstr($str, $start = 0 ...

  7. RAC:Oracle11gR2:启动gsd服务

    /************/ 正在测试是否必须执行 gsdctl enable gsdctl start /************/ srvclt enable nodeapps -v srvctl ...

  8. 公式编辑器编辑倒L符号的方法

    数学公式全都是由数字字母和一些符号组成的,一些常用的字母符号我们使用起来也很熟练,但是在数学中也有一些符号是比较少用的,比如倒着的L,这个符号在一些函数中出现过,表示某一类的函数.在word公式编辑器 ...

  9. MathType中公式不对齐怎么办

    MathType是一款专门用来编辑数学公式的数学公式编辑器,利用它可以在文档中快速编辑公式,与文字完美结合,可以编辑出各种各样的数学符号与公式,省下你不少的时间. 一.对齐上标与下标 MathType ...

  10. VS 清除编译产生的临时文件、文件夹

    VS编译过程中会产生一些临时文件,通过以下脚本可清除 @echo off for /r %%i in (*.sdf,*.ncb,*.suo,*.exp,*.user,*.aps,*.idb,*.dep ...