leetcode — n-queens
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/n-queens/
*
* Created by lverpeng on 2017/7/18.
*
* 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.."]
* ]
*
*/
public class NQueens {
public List<String> solve (int n) {
int[][] board = new int[n][n];
List<String> result = new ArrayList<String>();
revursion(board, 0, result);
return result;
}
/**
* n皇后问题,皇后所在位置的行、列、对角线都不能有其他皇后存在
* 使用递归解决
*
* @param board
* @param row
* @param result
*/
public void revursion (int[][] board, int row, List<String> result) {
if (row == board.length) {
// 找到解
StringBuilder stringBuilder = new StringBuilder();
if (result.size() > 0) {
stringBuilder.append("\n");
}
stringBuilder.append("[");
for (int i = 0; i < board.length; i++) {
stringBuilder.append("\"");
for (int j = 0; j < board.length; j++) {
if (board[i][j] == 1) {
stringBuilder.append("Q");
} else {
stringBuilder.append(".");
}
}
stringBuilder.append("\",\n");
}
stringBuilder = stringBuilder.delete(stringBuilder.length() - 3, stringBuilder.length());
stringBuilder.append("]");
result.add(stringBuilder.toString());
}
for (int i = 0; i < board.length ; i++) {
if (isValiad (board, row, i)) {
board[row][i] = 1;
revursion(board, row + 1, result);
board[row][i] = 0;
}
}
}
private boolean isValiad (int[][] board, int row, int col) {
for (int i = 0; i < row; i++) {
if (board[i][col] == 1 || (col - row + i >= 0 && board[i][col - row + i] == 1) || (col + row - i < board.length && board[i][col + row - i] == 1)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
NQueens nQueens = new NQueens();
List<String> list = nQueens.solve(4);
System.out.println("=======" + list.size() + "=======");;
System.out.println(Arrays.toString(list.toArray(new String[list.size()])));
list = nQueens.solve(8);
System.out.println("=======" + list.size() + "=======");
System.out.println(Arrays.toString(list.toArray(new String[list.size()])));
}
}
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 ...
随机推荐
- python中3个连续的单引号是什么意思?''' ... ''' 这样的引号是什么意思?
- java开发师笔试面试每日12题(3)
1.JDK和JRE的区别是什么? Java运行时环境(JRE)是将要执行Java程序的Java虚拟机.它同时也包含了执行applet需要的浏览器插件.Java开发工具包(JDK)是完整的Java软件开 ...
- ext__给grid Panel设置绑定事件
使用面板来展示详情信息 1.创建一个面板 (双击添加) 2.给该面板设置itemid的值为:detailPanel 3.给面板设置模板 4.添加下面的内容 id:{id}</br> nam ...
- qscoj 喵哈哈村的打印机游戏 区间dp
点这里去看题 区间dp ,dp[l][r][d]代表从l到r的区间底色为d,具体看代码 第一次见到区间dp...两个小时对着敲了五遍终于自己敲懂了一遍ac #include<bits/stdc+ ...
- ubuntu系统用docker搭建wordpress
目标:在docker中搭建wordpress 安装顺序: 首先要有一个云服务器---购买或者自己搭建(本人是自己在主机上装了虚拟机,搭建了一个ubuntu14.04,安装链接:https://www. ...
- Asp.net Security框架(2)
Asp.net 的Security框架除了提供Cookies,OAuth,ActiveDirectory等多个用户认证实现,基本上已经满足业务项目的开发需要了. 当需要实现OAuth2.0服务器端实现 ...
- Python学习笔记-函数基础
函数基础 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 为什么使用函数:减少重复代码.使程序变的可扩展使程序变得易维护 1.定义一个函数 #定 ...
- feign调用spring clound eureka 注册中心服务
@RestController public class TestService { private TestApi computeClient; private static final Strin ...
- Dubbo 源码分析 - 集群容错之 Cluster
1.简介 为了避免单点故障,现在的应用至少会部署在两台服务器上.对于一些负载比较高的服务,会部署更多台服务器.这样,同一环境下的服务提供者数量会大于1.对于服务消费者来说,同一环境下出现了多个服务提供 ...
- wordpress背景添加跟随鼠标动态线条特效
今天看到别人的博客,在鼠标移动背景时会出现线条动态特效 感觉挺有意思的,还有另一种,在背景点击时会跳出字符特意去找了方法,以为需要添加代码的,结果只要安装个插件就可以了,所以说wordpress就是方 ...