博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode — n-queens
阅读量:7032 次
发布时间:2019-06-28

本文共 3181 字,大约阅读时间需要 10 分钟。

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
solve (int n) { int[][] board = new int[n][n]; List
result = new ArrayList
(); revursion(board, 0, result); return result; } /** * n皇后问题,皇后所在位置的行、列、对角线都不能有其他皇后存在 * 使用递归解决 * * @param board * @param row * @param result */ public void revursion (int[][] board, int row, List
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
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()]))); }}

转载于:https://www.cnblogs.com/sunshine-2015/p/7518778.html

你可能感兴趣的文章
如何快速打造一款高清又极速的短视频APP?
查看>>
Cesium入门4 - 创建Cesium Viewer
查看>>
svn结合apache实现web也访问SVN
查看>>
我的友情链接
查看>>
MPLS 原理及配置思路
查看>>
实现 从“c:\\test.txt”这个文件中查找 "mobent"字符串出现的次数,并且记录出现的位置...
查看>>
HTML5 canvas 在线画笔绘图工具(一) 功能介绍
查看>>
我的友情链接
查看>>
lnmp环境下php7 安装redis扩展
查看>>
PPT实例教程第一篇:封面页改造
查看>>
Linux安全机制之文件加密解密
查看>>
快捷键
查看>>
linux 添加路由,让不同网络环境过来的访问按原线路回去
查看>>
oracle service 的创建、使用-基础分析
查看>>
我的友情链接
查看>>
lnmp环境修改最大上传文件大小限制
查看>>
百度地图API——MarkerTool单击事件的添加
查看>>
华为任正非《一江春水向东流》读后感
查看>>
用Perl脚本实现FTP的文件下载
查看>>
HASP HL Time
查看>>