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 ...
随机推荐
- 02.02.02 第2章 制作power bi图表(Power BI商业智能分析)
---恢复内容开始--- 02.02.02第2章 制作power bi图表 02.02.02.01 power pivot数据导入 00:08:43 02.02.02.02建立数据透视表 00:11: ...
- h3c acl配置一列
acl number 3004 rule 0 permit ip source 10.2.1.4 0 rule 1 deny ip source 192.168.1.91 0 rule 2 deny ...
- python3 django1.11 安装xadmin 的方法,亲测可用
首先需要Pip安装如下的包ip install django-crispy-forms pip install django-import-export pip install django-reve ...
- Windows 注册表 16进制时间转换( Convert Reg_binary Time to a Datetime )
背景: Windows注册表中,存在大量16进制的时间,以 reg_binary存储在注册表中. 例如: 0D 6C A4 4B 37 C5 CE 01 这种值日常报表中需要转换为适合人阅读的格式,实 ...
- vue中集成pdfjs自定义分页
<template> <div id="div_read_area_scrool" class="no-scrollbar--x" :styl ...
- 20155205 郝博雅 Exp7 网络欺诈防范
20155205 郝博雅 Exp7 网络欺诈防范 一.实践内容 (1)简单应用SET工具建立冒名网站 (1分) (2)ettercap DNS spoof (1分) (3)结合应用两种技术,用DNS ...
- visual studio 2015 Opencv 3.4.0配置
因为想做AR方面,需要了解计算机视觉知识,决定从opencv开始入门,在网上买了本毛星云的<Opencv3编程入门>开始自学. 一.opencv 3.4.0下载安装 在官网http://o ...
- 河北大学python选修课00次作业
学习python认为挺好玩的一件事.看到很多关于python的东西在网上,看到有这个课,认为只是选修课,别人也可以选,自己想不能被别人落下,别人都会,我不会可不行. 而且认为python是一个很强大的 ...
- table设置上下左右边距不一样-html
新手上路,刚刚自学html,仅作为记录学习历程用,有需要的可以参考. 1.边距相同时 <table align="center" cellpadding="15re ...
- HashTable使用举例--C#
一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似keyvalue的键值对,其中 ...