51. N-Queens (JAVA)
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.
Example:
Input: 4
Output: [
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."], ["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
注意:任意斜线也不能有两个Q
class Solution {
public List<List<String>> solveNQueens(int n) {
List<String> ans = new ArrayList<String>();
//initialize ans
for(int i = 0; i < n; i++){
StringBuilder s = new StringBuilder();
for(int j = 0; j < n; j++){
s.append(".");
}
ans.add(s.toString());
} dfs(n,0,ans);
return ret;
} public void dfs(int n, int depth, List<String> ans){ if(depth == n) {
List<String> new_ans = new ArrayList<String>(ans);
ret.add(new_ans);
return;
}
StringBuilder strBuilder = new StringBuilder(ans.get(depth));
for(int i = 0; i < n; i++){
if(check(n, ans, depth, i)) continue; //already have Q in this column strBuilder.setCharAt(i, 'Q');
ans.set(depth, strBuilder.toString());
dfs(n, depth+1, ans);
strBuilder.setCharAt(i, '.'); //recover
ans.set(depth, strBuilder.toString());
}
} public Boolean check(int n, List<String> ans, int i, int j){
for(int k = 0; k < i; k++){ //iterate n line
//i-k = j-x => x = j+k-i; i-k = x-j => x = i+j-k
if( ans.get(k).charAt(j) == 'Q'
||(j+k-i >= 0 && ans.get(k).charAt(j+k-i) == 'Q')
|| (i+j-k < n && ans.get(k).charAt(i+j-k) == 'Q'))
return true;
} return false;
} private List<List<String>> ret = new ArrayList<>();
}
51. N-Queens (JAVA)的更多相关文章
- CXF错误:Unsupported major.minor version 51.0,java.lang.UnsupportedClassVersionErro
CXF错误:Unsupported major.minor version 51.0 java.lang.UnsupportedClassVersionError >>>>&g ...
- 第51章:Java操作MongoDB-[Mongo-Java-2.x]
①范例:连接数据库 package cn.mldn.demo; import com.mongodb.DB; import com.mongodb.MongoClient; public class ...
- 第51节:Java当中的集合框架Map
简书作者:达叔小生 Java当中的集合框架Map 01 Map提供了三个集合视图: 键集 值集 键-值 映射集 public String getWeek(int num){ if(num<0 ...
- java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51
http://blog.csdn.net/e_wsq/article/details/52100234 一日换了一下MyEclipse,换成2016CI,结果从SVN上下载了一个工程后出现以下错误: ...
- java.lang.UnsupportedClassVersionError: org/openqa/selenium/WebDriver : Unsupported major.minor version 51.0
周一上班,正常打开myeclipse,随便写了一个main方法执行.发现报错了... 问题提示如下: java.lang.UnsupportedClassVersionError: org/openq ...
- Java运行 Unsupported major.minor version 51.0 错误
今天写了简单的Java程序,运行的时候不知道为啥出现这个问题 happy@happy-HP-Compaq-dx7518-MT:~/Study/CrazyJava$ java FieldTest Exc ...
- 记一次jdk升级引起的 Unsupported major.minor version 51.0
之前jdk 一直是1.6,tomcat 是6.x 版本,, 现在引入的新的jar, 出现 Caused by: java.lang.UnsupportedClassVersionError: org/ ...
- myeclipse 无法启动 java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).
把myeclipse10 按照目录完整拷贝到了另外一台电脑, 另外的目录 原安装目录 D\:\soft\i\myeclipse10 新安装目录 E\:\soft\myeclipse10 双击启动失败, ...
- 【java.lang.UnsupportedClassVersionError】版本不一致出错
这种错误的全部报错信息: java.lang.UnsupportedClassVersionError: org/apache/lucene/store/Directory : Unsupported ...
- 为何JAVA虚函数(虚方法)会造成父类可以"访问"子类的假象?
首先,来看一个简单的JAVA类,Base. 1 public class Base { 2 String str = "Base string"; 3 protected vo ...
随机推荐
- koa2环境搭建
npm install -g koa-generator koa2 ssy-koa2 cd ssy-koa2 npm install
- Hadoop : MapReduce中的Shuffle和Sort分析
地址 MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Sch ...
- springboot2.0数据制作为excel表格
注意:由于公司需要大量导出数据成excel表格,因此在网上找了方法,亲测有效. 声明:该博客参考于https://blog.csdn.net/long530439142/article/details ...
- 阶段3 3.SpringMVC·_06.异常处理及拦截器_3 SpringMVC异常处理之异常处理代码编写
分三步 新建exception的包.然后添加SysException类 一般写异常都继承.Exception 定义Messgae属性,生成get和set 生成带参数的构造方法 选中异常的代码 Ctrl ...
- js __proto__ 和prototype的联系
1.Javascript中所有的对象都是Object的实例,并继承Object.prototype的属性和方法,也就是说,Object.prototype是所有对象的爸爸.(个人感觉搞清楚这一点很重要 ...
- spring boot系列(五)spring boot 配置spring data jpa (查询方法)
接着上面spring boot系列(四)spring boot 配置spring data jpa 保存修改方法继续做查询的测试: 1 创建UserInfo实体类,代码和https://www.cnb ...
- /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not found错误的解决 转载
升级cmake时,提示“Error when bootstrapping CMake:Problem while running initial CMake”,第二次运行./bootstrap时,直接 ...
- GIT命令总结,so easy
一:GIT命令实战(码云) https://oschina.gitee.io/learn-git-branching/ 提交 git commit 创建分支 git branch <name&g ...
- 【数据库】Redis/MongoDB/MySQL/Oracle随笔索引
数据库体系 [思维导图]数据库体系 密码: a8ni Redis JPA
- round函数——银行家舍入算法
在处理四舍五入时,相信大部分人会使用math.round函数(不同的语言应该都有).有没有考虑过,这个函数是不是自己所需要的? po主碰到的问题是用来计算平均分.有个顶真的学生反映,明明是86.5,怎 ...