51. N皇后

n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

上图为 8 皇后问题的一种解法。

给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。

每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 ‘Q’ 和 ‘.’ 分别代表了皇后和空位。

示例:

输入: 4

输出: [

[".Q…", // 解法 1

“…Q”,

“Q…”,

“…Q.”],

["…Q.", // 解法 2

“Q…”,

“…Q”,

“.Q…”]

]

解释: 4 皇后问题存在两个不同的解法。

class Solution {
public static List<List<String>> output;
public List<List<String>> solveNQueens(int n) {
output = new ArrayList<>();
// 声明一个长度为n的数组用来代表 第n行棋子是在第result[n]列
int[] result = new int [n];
calnQueens(0, n, result);
return output;
} // n 皇后问题 row代表计算到了到第row行
private static void calnQueens(int row, int n, int[] result){
if (row == n){
// 到达第n行代表已经得到一个将解决方案 直接返回即可
// 根据result数组将结果加入到output列表中
getPrint(result);
return;
}
// 若不是第n行 则说明需要继续判断该行棋子应该在那一列
for (int column = 0; column < n; column++){
// 判断第row行 放置在column列的棋子是否满足要求
if (isOK(row, column, result)){
result[row] = column;
// 递归判断下一行的情况
calnQueens(row + 1, n, result);
}
// 不满足要求 回溯下一列 对应操作column++
}
} // row代表行数 column代表列数 result代表满足规则的棋子在第n行中的位置
private static boolean isOK(int row, int column, int[] result){
// 判断棋子的位置是否正确 不正确返回false
for (int i = 0; i < row; i++){
// 第一个条件排除的是相同列的问题
// 第二个条件排除的是对角线列的左下角
// 第三个条件排除的是对角线列的右下角
if (column == result[i] || column == result[i] - row + i || column == result[i] + row - i){
return false;
}
}
return true;
} private static void getPrint(int[] result){
List<String> one = new ArrayList<>();
for (int row = 0; row < result.length; row++){
// 一行一个StringBuilder
StringBuilder str = new StringBuilder();
for (int column = 0; column < result.length; column++){
if (column == result[row]){
str.append("Q");
}else{
str.append(".");
}
}
one.add(str.toString());
}
output.add(one);
}
}

Java实现 LeetCode 51 N皇后的更多相关文章

  1. leetcode 51. N皇后 及 52.N皇后 II

    51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...

  2. Java实现 LeetCode 52 N皇后 II

    52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...

  3. [leetcode]51. N-QueensN皇后

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  4. LeetCode 51. N-QueensN皇后 (C++)(八皇后问题)

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  5. leetcode 51 N皇后问题

    代码,由全排列转化而来,加上剪枝,整洁的代码: 共有4个变量,res(最终的结果),level,当前合理的解,n皇后的个数,visit,当前列是否放过皇后,由于本来就是在新的行方皇后,又通过visit ...

  6. Leetcode之回溯法专题-51. N皇后(N-Queens)

    Leetcode之回溯法专题-51. N皇后(N-Queens) n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给 ...

  7. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

随机推荐

  1. JDBC03 Statement接口

    Statement接口 用于执行静态SQL语句并返回它所生成结果的对象 三种Statem类 Statement:由createStatement创建,用于发送简单的SQL语句(不带参数的),会有SQL ...

  2. vue cli脚手架项目利用webpack给生产环境和发布环境配置不同的接口地址或者不同的变量值。

    废话不多说,直接进入正题,此文以配置不同的接口域名地址为例子 项目根目录下有一个config文件夹,基础项目的话里面至少包括三个文件, 1.dev.env.js 2.index.js 3.prod.e ...

  3. linux centos7 和 windows下 部署 .net core 2.0 web应用

    centos7 下部署asp.net core 2.0应用 安装CentOS7 配置网络[可选] 安装.Net core2.0 创建测试Asp.net Core应用程序 正式部署项目 安装VMware ...

  4. quartz 定时任务调度管理器

    本项目使用的是spring-quartz 以下配置可以开启多个已知定时任务 <?xml version="1.0" encoding="UTF-8"?&g ...

  5. 【Socket编程】【第一节】【Socket基本原理和套接字】

    参考http://c.biancheng.net/view/2351.html 一.scoket套接字(告诉你使用哪种数据传输方式) 这个世界上有很多种套接字(socket),比如 DARPA Int ...

  6. Python 常用编码规范

    一.简明概述 1.编码 如无特殊情况, 文件一律使用 UTF-8 编码 如无特殊情况, 文件头部必须加入#-*-coding:utf-8-*-标识 2.代码格式 2.1.缩进 统一使用 4 个空格进行 ...

  7. css实现双色饼图

    from:wx--前端早读课 首先回想用css画三角形的方法: <div class="triangle"></div> .triangle { displ ...

  8. Reflux之Store

    Reflux中的Store既是一个listener(既有对action的监听,又有对store的监听)同时又是一个publisher. 一.监听单个action const Reflux = requ ...

  9. 关于键盘事件-查询:有多个input框,任意一个支持enter键查询

    应用场景:同一个界面有多个input框支持任意一个Enter查询. 实现:在input框中添加onkeypress="函数名()". 函数里面编写对应键盘code值,在里面直接调用 ...

  10. nginx配置之错误和访问日志功能

    错误日志功能:logs/error.log nginx.conf中: #error_log logs/error.log; #error_log logs/error.log notice; #err ...