LeetCode 10.4 N-Qeens
2016-04-26 15:01
169 查看
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
indicate a queen and an empty space respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
import java.util.Stack;
public class T1 { T1(int _n) { n = _n; f = new int[n]; } int n; int[] f; Stack<int[]> ans = new Stack<>();
public static void main(String[] args) { T1 t = new T1(8); t.DFS(0); t.print(); }
void DFS(int k) { if (k == n) { if (chack(n)) { int[] a = new int[n]; for (int i = 0; i < f.length; i++) { a[i] = f[i]; } ans.push(a); } } else { for (int i = 0; i < n; i++) { f[k] = i; if (chack(k)) DFS(k + 1); } } } boolean chack(int l) { for (int i = 0; i < l; i++) { for (int j = i + 1; j < l; j++) { if (Math.abs(f[j] - f[i]) == j - i || f[j] == f[i]) { return false; } } } return true; } void print() { int num = 1; while (!ans.isEmpty()) { System.out.println("============" + num + "======="); int[] arr = ans.pop();
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr[i] != j) System.out.print('.'); else System.out.print('*'); } System.out.println(); } num++; } }}
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.."] ]
import java.util.Stack;
public class T1 { T1(int _n) { n = _n; f = new int[n]; } int n; int[] f; Stack<int[]> ans = new Stack<>();
public static void main(String[] args) { T1 t = new T1(8); t.DFS(0); t.print(); }
void DFS(int k) { if (k == n) { if (chack(n)) { int[] a = new int[n]; for (int i = 0; i < f.length; i++) { a[i] = f[i]; } ans.push(a); } } else { for (int i = 0; i < n; i++) { f[k] = i; if (chack(k)) DFS(k + 1); } } } boolean chack(int l) { for (int i = 0; i < l; i++) { for (int j = i + 1; j < l; j++) { if (Math.abs(f[j] - f[i]) == j - i || f[j] == f[i]) { return false; } } } return true; } void print() { int num = 1; while (!ans.isEmpty()) { System.out.println("============" + num + "======="); int[] arr = ans.pop();
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr[i] != j) System.out.print('.'); else System.out.print('*'); } System.out.println(); } num++; } }}
相关文章推荐
- win10系统打开激战2没有反应卡住解决方法
- 给jdk写注释系列之jdk1.6容器(1):ArrayList源码解析
- iOS 获取本地相册的所有图片
- iOS中的通知机制
- Java实现代理模式(CGLIB)
- 王高利:NFS共享存储
- 第85讲:基于HDFS的SparkStreaming案例实战和内幕源码解密
- 团队作业(五)
- JavaScript及C# URI编码详解
- javascript获取时间
- CloudFoundry in 1 Box简介:Lattice篇
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- git分支模型
- Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
- JavaScript及C# URI编码详解
- JavaScript及C# URI编码详解