N-Queens | & N-Queens II
2016-07-06 07:06
399 查看
The n-queens puzzle is the problem of placing n queens on an
Given an integer
Each solution contains a distinct board configuration of the n-queens' placement, where
Example
There exist two distinct solutions to the 4-queens puzzle:
Now, instead outputting board configurations, return the total number of distinct solutions.
Example
For n=4, there are 2 distinct solutions.
n×nchessboard 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
There exist two distinct solutions to the 4-queens puzzle:
[ // Solution 1 [".Q..", "...Q", "Q...", "..Q." ], // Solution 2 ["..Q.", "Q...", "...Q", ".Q.." ] ]
class Solution { ArrayList<ArrayList<String>> solveNQueens(int n) { ArrayList<ArrayList<String>> allList = new ArrayList<ArrayList<String>>(); if (n <= 0) return allList; Integer[] row = new Integer ; ArrayList<ArrayList<Integer>> integerList = new ArrayList<ArrayList<Integer>>(); queen(0, n, row, integerList); char[] arr = new char ; Arrays.fill(arr, '.'); for (ArrayList<Integer> l : integerList) { ArrayList<String> temp = new ArrayList<String>(); for (int i = 0; i < l.size(); i++) { arr[l.get(i)] = 'Q'; temp.add(new String(arr)); arr[l.get(i)] = '.'; } allList.add(new ArrayList<String>(temp)); } return allList; } public void queen(int n, int count, Integer[] row, ArrayList<ArrayList<Integer>> list) { if (n == count) { list.add(new ArrayList<Integer>(Arrays.asList(row))); return; } for (int i = 0; i < count; i++) { row = i; if (isSatisfied(n, row)) { queen(n + 1, count, row, list); } } } public boolean isSatisfied(int n, Integer[] row) { for (int i = 0; i < n; i++) { if (row[i] == row ) return false; if (Math.abs(row - row[i]) == n - i) return false; } return true; } };
N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.
Example
For n=4, there are 2 distinct solutions.
class Solution { /** * Calculate the total number of distinct N-Queen solutions. * @param n: The number of queens. * @return: The total number of distinct solutions. */ public int totalNQueens(int n) { //write your code here int[] row = new int ; int[] current = new int[1]; queen(0, n, row, current); return current[0]; } public void queen(int n, int count, int[] row, int[] current) { if (n == count) { current[0]++; return; } for (int i = 0; i < count; i++) { row = i; if (isSatisfied(n, row)) { queen(n + 1, count, row, current); } } } public boolean isSatisfied(int n, int[] row) { for (int i = 0; i < n; i++) { if (row[i] == row ) return false; if (Math.abs(row - row[i]) == n - i) return false; } return true; } }
相关文章推荐
- MySQL: Tree-Hierarchical query
- 陶哲轩实分析-第18章 Lebesgue测度
- Range Sum Query 2D
- Next Larger Value in BST
- LeetCode--No.357--Count Numbers with Unique Digits
- The Concept of Indistinguishability under Chosen Plaintext Attack (IND-CPA)
- UITableView滚动时NSTimer不执行
- 【Leetcode】303 Range Sum Query - Immutable - 【动态规划】
- Codeforces Round #360 (Div. 2) -- E. The Values You Can Make (DP)
- Vue.js 基础示例
- Quiz 6b_question#7
- UI设计有哪些规范?
- HashMap/HashTable/String/StringBuffer/StringBuilde
- Class类文件结构之ConstantValue属性
- StringBuffer 和 StringBuilder的区别
- UITableView
- 全文检索Lucene(二)--特定项搜索与queryParser搜索
- Vue.js
- hdu 5286 wyh2000 and sequence 分块
- 【MySQL】MySQL的Sequence