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. 设计模式-观察者模式(上)<转>

    本文参考Head First设计模式一书,感觉书中的例子实在很好,很贴切.对模式的知识点进行总结,并对书的源码做了一定注释.   观察者模式要点有二:主题和观察者. 最贴切的案例是:杂志订阅,杂志是主 ...

  2. 关于listView的item失去焦点不能点击 Item中包含Button 导致抢占焦点

    今天发现一个问题.listView的item点击以后进入到下一个页面,下个页面有个返回按钮,直接返回回去以后点击事件不能触发,滑动或者重新打开这个listView,就可以达到原来的效果.后来发现是因为 ...

  3. ExtJs Ext.data.Model 学习笔记

    Using a Proxy Ext.define('User', { extend: 'Ext.data.Model', fields: ['id', 'name', 'email'], proxy: ...

  4. Spark Streaming中的操作函数讲解

    Spark Streaming中的操作函数讲解 根据根据Spark官方文档中的描述,在Spark Streaming应用中,一个DStream对象可以调用多种操作,主要分为以下几类 Transform ...

  5. 一、think in java 第一章

    一.面向对象程序设计方式: 1.万物皆为对象. 将对象视为奇特的变量,它可以存储数据,也可以要求它在自身上执行操作. 2.程序是对象的集合,它们通过发送消息来告知彼此所要做的. 要请求一个对象,就必须 ...

  6. 【这特么是个坑。。。】iOS 10.3下解决Charles抓包ssl证书信任问题

    针对近期iOS 10.3以上的系统charles抓https信任问题 前言 最近iPhone系统更新到ios 10.3后,在公司里用Charles抓包竟然出现了一些问题,https的请求都会失败,提示 ...

  7. Ubuntu创建新用户并增加管理员权限 删除某个用户

    sudo adduser xxx 这样的命令会在home目录下添加一个帐号sudo useradd xxx 仅仅是添加用户, 不会在home目录添加帐号 删除:终端方法:以下用newuser代替想要删 ...

  8. BaaS后端即服务 - 概念篇

    摘要: 什么是BaaS? BaaS(Backend as a Service)是一种新型的云服务,旨在为移动和Web应用提供后端云服务,包括云端数据/文件存储.账户管理.消息推送.社交媒体整合等.Ba ...

  9. Hibernate注解关系映射

    Hibernate Annotation关系映射的几种类型映射用法及使用方法(说明:以前实例的实体是user和role,主键分别是userid和roleid)   1)一对一外键关联映射(单向) @O ...

  10. ceRNA 调控机制

    ceRNA 不同于mRNA, lncRNA, ncRNA 等概念,其指的既不是某一种类型的RNA(比如mRNA, lncRNA), 也不是某一类的RNA(如ncRNA); ceRNA 其实指的是不同种 ...