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

上图为 8 皇后问题的一种解法。
给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。
每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
示例:
输入: 4
输出: [
[".Q..", // 解法 1
"...Q",
"Q...",
"..Q."],
["..Q.", // 解法 2
"Q...",
"...Q",
".Q.."]
]
解释: 4 皇后问题存在两个不同的解法。
import java.util.*;
public class Solution{
public List<List<String>> solveNQueens(int n){
List<List<String>> res=new ArrayList<List<String>>();
int[] queenList=new int[n];//第i个位置存放的数表示row行时,Q的列
placeQueen(queenList,0,n,res);//在第0行放Q
return res;
}
private void placeQueen(int[] queenList,int row,int n,List<List<String>> res){
if(row==n){
ArrayList<String> list=new ArrayList<String>();
for(int i=0;i<n;i++){
String str="";
for(int col=0;col<n;col++){
if(queenList[i]==col){
str+="Q";
}else{
str+=".";
}
}
list.add(str);
}
res.add(list);
}
for(int col=0;col<n;col++){
if(isValid(queenList,row,col)){
queenList[row]=col;
placeQueen(queenList,row+1,n,res);
}
}
}
private boolean isValid(int[] queenList,int row,int col){
for(int i=0;i<row;i++){
int pos=queenList[i];
if(pos==col)
return false;
if(pos+row-i==col)
return false;
if(pos-row+i==col)
return false;
}
return true;
}
}
Leetcode 51.N后问题的更多相关文章
- [LeetCode] 51. N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- LeetCode翻转矩阵后的得分-Python3<六>
上一篇:LeetCode子域名访问计数-Python3.7<五> 题目:https://leetcode-cn.com/problems/score-after-flipping-matr ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- LeetCode.985-查询后偶数的总和(Sum of Even Numbers After Queries)
这是悦乐书的第370次更新,第398篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第232题(顺位题号是985).有一个整数数组A和一个查询数组queries. 对于第i ...
- leetcode 51. N皇后 及 52.N皇后 II
51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...
- LeetCode - 51. N-Queens
51. N-Queens Problem's Link ------------------------------------------------------------------------ ...
- leetcode@ [51/52] N-Queens
https://leetcode.com/problems/n-queens/ class Solution { public: void dfs(vector<vector<string ...
- LeetCode 51 N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- leetcode 51. N-Queens 、52. N-Queens II
51. N-Queens 使用isValid判断当前的位置是否合法 每次遍历一行,使用queenCol记录之前行的存储位置,一方面是用于判断合法,另一方面可以根据存储结果输出最终的结果 class S ...
随机推荐
- 策略设计测试用例实践(2)--Pairwise(转)
一.关于”好的“测试用例 在设计测试用例的时候有多种设计方法和策略可以使用,使得测试用例设计得更丰富,尽可能覆盖到更多的程序路径和功能场景.常见的测试用例设计方法被提到最多的就是等价类划分.边界值分析 ...
- AMD的规范使用
1.解决命名冲突 2.解决繁琐的文件依赖 3. 可读性.可依赖性 参考这里 // foobar.js // 私有变量 var test = 123: // 公有方法 function foot ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)
题目描述 We consider a positive integer perfect, if and only if it is equal to the sum of its positive d ...
- 题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- ViewPager(4)用viewpager实现splash view
1,示例 2,代码 SplashMain.java import android.os.Bundle; import android.support.v4.app.Fragment; import a ...
- A Python example for HiveServer2
要做一个通过调用python来实现对hive server2 的连接.在网上搜索了很多资料,有些说的hive sever的,但是由于认证方式发生改变,行不通. 最后,找到了权威的说明(PS: 还是应该 ...
- System.AppDomain类详解(一)
AppDomain是CLR(Common Language Runtime:公共语言运行库),它可以加载Assembly.创建对象以及执行程序. AppDomain是CLR实现代码隔离的基本机制. 每 ...
- IDEA打可执行jar包
流程: 1. File ->Project Structure -> Artifacts -> + -> JAR -> From modules with depende ...
- vue2.0路由(跳转和传参)经典介绍
声明式 <router-link :to="...">编程式router.push(...) router.push('home') / ...
- python爬虫(房天下)
房天下 import requests res = requests.get('http://esf.sz.fang.com/') #res.text from bs4 import Beautifu ...