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. 【C】——指针与const限定符

    const限定符和指针结合起来常见的情况有以下几种. const int *a; int const *a; 这两种写法是一样的,a是一个指向const int型的指针,a所指向的内存单元不可改写,所 ...

  2. iOS边练边学--UITableViewCell的常见属性设置

    // 取消选中的样式(常用) 让当前 cell 按下无反应 cell.selectionStyle = UITableViewCellSelectionStyleNone; // 设置选中的背景色,U ...

  3. print spooler 服务无法启动 打印机全部消失

    这往往是失败的打印任务导致的. 打开 C:\Windows\System32\spool\PRINTERS ,这个目录下的文件全是打印缓存,失败的任务也保存在这里.把这里的文件全部删除,然后再重新启动 ...

  4. r指定位置插入一列数值

    y<-1:4 data1 <-data.frame(x1=c(1,3,5,7), x2=c(2,4,6,8),x3=c(11,12,13,14),x4=c(15,16,17,18)) da ...

  5. 终于想明白一些事,关于NAS

    一直以来想搞好一部NAS存储小孩的视频和照片,一直纠结用什么硬件,硬件解决后虽然不甚满意,不过无论怎么样都算投入巨资(超过7千……)组装完毕,然后就一直纠结用什么NAS系统,终于下定决心使用了OMV, ...

  6. 关于Cocos2d-x的动作和动画

    1.动作,在cocos2d-x中有非常多种的动作,各种移动,旋转,缩放,淡入淡出....等等非常多,但是这些动作只是作用于节点,最常作用于的就是精灵节点.而且我们可以把很多个动作放进一个Sequenc ...

  7. 关于Cocos2d-x中类与类之间调用彼此方法的机制

    1.一般情况下都是把需要实例化的对象类的头文件包含到一个GameController.h中,再在GameController.cpp文件里面去使用各种对象类的方法来实现游戏的逻辑. 2.还有的时候会把 ...

  8. 【转】 如何利用C#代码来进行操作AD

    要用代码访问 Active Directory域服务,需引用System.DirectoryServices命名空间,该命名空间包含两个组件类,DirectoryEntry和 DirectorySea ...

  9. e657. 用直线和曲线绘制图形

    GeneralPath shape = new GeneralPath(); shape.moveTo(x, y); shape.lineTo(x, y); shape.quadTo(controlP ...

  10. 用C语言实现解析简单配置文件的小工具

    本文介绍作者写的一个小工具,简单的代码中包含了C语言对字符串的处理技巧,对文本文件的简单解析,二进制文件的数据复制的方法,以及格式化输出文本文件的示例. 工具的输入是如下内容的配置文件: ;资源管理器 ...