LeetCode: 51. N-Queens(Medium)
1. 原题链接
https://leetcode.com/problems/n-queens/description/
2. 题目要求
游戏规则:当两个皇后位于同一条线上时(同一列、同一行、同一45度斜线、同一135度斜线)时,便可以消灭其中一个皇后
给出一个n*n的棋盘,要求棋盘上的n个皇后都不能被其他皇后吃掉,给出棋盘上n个皇后所有的摆放情况。
‘Q’代表此处放的是皇后,‘.’代表此处为空
4皇后的输出结果形式:
[[.Q.., ...Q, Q..., ..Q.],
[..Q., Q..., ...Q, .Q..]]
3. 解题思路
1. 将n*n的棋盘用一个长度为8的String类型数组表示,每一个元素代表棋盘中的一行,由长度为8的字符串表示,例如:
String[] queens = {".......Q", "...Q....", "Q.......", "..Q.....", ".....Q..", ".Q......", "......Q.", "....Q..."};
2. 采用回溯的思想进行迭代,该位置满足则设为Q,否则回溯到上一步,重新选择列的位置进行迭代
4. 代码实现
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class NQueens51 {
public static void main(String[] args) {
NQueens51 nq = new NQueens51();
List<List<String>> res = nq.solveNQueens(8);
System.out.println(res.size()); for (List l : res) {
System.out.println(l.toString());
for(Object str:l){
System.out.println(str);
}
System.out.println("-------------------");
}
} public List<List<String>> solveNQueens(int n) {
List<List<String>> res = new ArrayList<List<String>>();
String[] queens = new String[n]; // 每一行用n个'.'填充
char[] initial = new char[n];
Arrays.fill(initial, '.'); // n*n的棋盘用n行'.'填充
Arrays.fill(queens, String.valueOf(Arrays.copyOf(initial, n))); // 45度斜线+135度斜线+n列
int[] flag = new int[5 * n - 2];
Arrays.fill(flag, 1); backtracking(res, queens, flag, 0, n);
return res;
} private void backtracking(List<List<String>> res, String[] queens, int[] flag, int row, int n) {
if (row == n) {
res.add(new ArrayList<String>(Arrays.asList(queens)));
return;
}
for (int col = 0; col != n; col++) { if (flag[col] == 1 && flag[n + col + row] == 1 && flag[4 * n - 2 + col - row] == 1) { // 判断列和两条斜线上是否有'Q'
flag[col] = 0; // 该列flag设为1,说明该列已经有Q了 flag[n + col + row] = 0; // 45度斜线上的flag设为0 flag[4 * n - 2 + col - row] = 0; // 135度斜线上的flag设为0
char[] chars = queens[row].toCharArray(); chars[col] = 'Q';
queens[row] = String.valueOf(chars); backtracking(res, queens, flag, row + 1, n); chars = queens[row].toCharArray();
chars[col] = '.';
queens[row] = String.valueOf(chars); flag[col] = 1;
flag[n + col + row] = 1;
flag[4 * n - 2 + col - row] = 1;
}
}
}
}
运行结果:
一共有92中排列方式
LeetCode: 51. N-Queens(Medium)的更多相关文章
- LeetCode: 61. Rotate List(Medium)
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- LeetCode: 55. Jump Game(Medium)
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...
- LeetCode: 54. Spiral Matrix(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...
- LeetCode:46. Permutations(Medium)
1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...
- LeetCode:18. 4Sum(Medium)
1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...
- LeetCode:15. 3Sum(Medium)
1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...
随机推荐
- js获取7天之前或之后的日期
function fun_date(aa){ var date1 = new Date(), time1=date1.getFullYear()+"-"+(date1.getMon ...
- DispatcherServlet类的分析
突然发现拿博客园来做笔记挺好的,不会弄丢.下面我把DispatcherServlet类的部分源代码复制到这里,然后阅读,把重要的地方翻译一下,该做笔记的地方做下笔记,蹩脚英语. =========== ...
- 几个python练习题
从python公众号里面看到了几道python的练习题,就拿来练练手,结果上手了发现自己还是特别水,不是很难的8道题,我只做出来5道,其中还3道题卡住了,边查边做的.原题链接在这里:http://py ...
- Could not publish to the server. null argument:
启动tomcat或clean tomcat报错:Could not publish to the server. null argument: Could not publish to the ser ...
- 【JQ】鼠标经过一组button,弹出各自的气泡图片
HTML <div id="bubble1" class="bubble"><img src="../image/p_bubble1 ...
- JDK工具系列之jps
一.简介 jps(JVM Process Status Tool)是虚拟机进程状态工具:可以列出正在运行的虚拟机进程,显示虚拟机正在执行的main()函数,及这些进程的ID(LVMID,Local V ...
- [19/04/01-星期一] IO技术_字节流分类总结(含字节数组(Array)流、字节数据(Data)流、字节对象(Object)流)
一.字节流分类概括 -->1.ByteArrayInputStream /ByteArrayOutputStream(数组字节输入输出) InputStream/OutputStr ...
- Kali-linux设置ProxyChains
ProxyChains是Linux和其他Unices下的代理工具.它可以使任何程序通过代理上网,允许TCP和DNS通过代理隧道,支持HTTP.SOCKS4和SOCKS5类型的代理服务器,并且可配置多个 ...
- Enum介绍
public enum Color { RED, YELLOW, BLUE; } 说明: 使用的是enum关键字而不是class 多个枚举变量之间用 逗号 隔开 枚举变量名大写,多个单词之间用 _ 隔 ...
- 【SQLSERVER学习笔记】细节记录
SQLSERVER 查询时,WHERE中使用<>时,不会把NULL值查出来. SQLSERVER子查询中不能使用 ORDER BY. SQLSERVER 使用DISTINCT时,必须把OR ...