LeetCode - N-Queens I && II
2015-04-11 01:25
471 查看
N-Queens I
https://leetcode.com/problems/n-queens/
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:
这道题跟solve sudoku差不多,都是每个index挨个检查是否valid,如果valid就进入下一轮递归,否则就继续寻找。
注意这里,用一个LinkedList<Integer>来存已经生成的position,list中第i个元素j表示第i行的queen在第j列。这样简化了很多代码,不然直接操作string[]简直是灾难啊。
还有就是在检查对角线时,检查的是 Math.abs(row1-row2)==Math.abs(col1-col2),如果检查Math.abs(row1-col) == Math.abs(row2-col2)的话,只能检查一个方向的对象线,即从左上到右下的,不能检查从右上到左下的对角线。
从左上到右下的对角线上row-col值是相同的
从右上到左下的对角线row+col的值相同
两种都满足 Math.abs(row1-row2)==Math.abs(col1-col2)
N-Queen II
https://leetcode.com/problems/n-queens-ii/
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
这道题直接用上一道题的函数就可以了,只是当遇到一个solution的时候,不需要生成board,只需要count++就可以了。另外,由于JAVA都是值传递,所以如果
传给helper函数的是int count的话,那么helper中修改的count值不会生效,所以这里传一个int[] count数组,然后修改count[0],另一个方法是在类中设置一个全局变量。
https://leetcode.com/problems/n-queens/
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.."] ]
这道题跟solve sudoku差不多,都是每个index挨个检查是否valid,如果valid就进入下一轮递归,否则就继续寻找。
注意这里,用一个LinkedList<Integer>来存已经生成的position,list中第i个元素j表示第i行的queen在第j列。这样简化了很多代码,不然直接操作string[]简直是灾难啊。
还有就是在检查对角线时,检查的是 Math.abs(row1-row2)==Math.abs(col1-col2),如果检查Math.abs(row1-col) == Math.abs(row2-col2)的话,只能检查一个方向的对象线,即从左上到右下的,不能检查从右上到左下的对角线。
从左上到右下的对角线上row-col值是相同的
从右上到左下的对角线row+col的值相同
两种都满足 Math.abs(row1-row2)==Math.abs(col1-col2)
public class Solution { public List<String[]> solveNQueens(int n) { List<String[]> rst = new LinkedList<String[]>(); List<Integer> position = new LinkedList<Integer>(); helper(rst, position, n); return rst; } public void helper(List<String[]> rst, List<Integer> position, int n){ if(position.size()==n){ genBoard(position, rst); return; } for(int i=0; i<n; i++){ if(isValid(position, i)){ position.add(i); helper(rst, position, n); position.remove(position.size()-1); } } } public boolean isValid(List<Integer> position, int col){ int row = position.size(); for(int i=0; i<position.size(); i++){ if(col == position.get(i)) return false; if(Math.abs(row-i)==Math.abs(col-position.get(i))) return false; } return true; } public void genBoard(List<Integer> position, List<String[]> rst){ String[] board = new String[position.size()]; for(int i=0; i<position.size(); i++){ int queen = position.get(i); StringBuilder sb = new StringBuilder(); for(int j=0; j<position.size(); j++){ if(j!=queen) sb.append('.'); else sb.append('Q'); } board[i] = sb.toString(); } rst.add(board); } }
N-Queen II
https://leetcode.com/problems/n-queens-ii/
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
这道题直接用上一道题的函数就可以了,只是当遇到一个solution的时候,不需要生成board,只需要count++就可以了。另外,由于JAVA都是值传递,所以如果
传给helper函数的是int count的话,那么helper中修改的count值不会生效,所以这里传一个int[] count数组,然后修改count[0],另一个方法是在类中设置一个全局变量。
public class Solution { public int totalNQueens(int n) { List<String[]> rst = new LinkedList<String[]>(); List<Integer> position = new LinkedList<Integer>(); int[] count = new int[1]; helper(rst, position, n, count); return count[0]; } public void helper(List<String[]> rst, List<Integer> position, int n, int[] count){ if(position.size()==n){ count[0]++; return; } for(int i=0; i<n; i++){ if(isValid(position, i)){ position.add(i); helper(rst, position, n, count); position.remove(position.size()-1); } } } public boolean isValid(List<Integer> position, int col){ int row = position.size(); for(int i=0; i<position.size(); i++){ if(col == position.get(i)) return false; if(Math.abs(row-i)==Math.abs(col-position.get(i))) return false; } return true; } }
相关文章推荐
- leetcode N-Queens I & II N皇后问题
- leetcode 52. N-Queens II & leetcode 51 N-Queens (N皇后问题)
- [LeetCode 51&52] N-Queens I & II (N皇后问题)
- leetcode -- N-Queens I&II,经典回溯,再看
- Leetcode | N-Queens I & II
- leetcode--n_queens&&n_queens_II
- 【leetcode】N-Queens I & II
- 【LeetCode】N-Queens II && 【九度】题目1254:N皇后问题
- leetcode做题总结,回溯法(N-Queens, N-QueensII,Combination SumI&II,wordbreak II, SubsetsI&II)
- leetcode Best Time to Buy and Sell Stock I&&II&&III
- N-Queens II N皇后问题(有几种放法)@LeetCode
- LeetCode Single Number I & II 都符合两个问题额外要求的 通用解法 与 思考过程
- [leetcode 78 & 90, Medium] Subsets I and II
- LeetCode 之 Pascal's Triangle II
- Leetcode: Unique Path I & II, Minimum Path Sum, Triangle
- [Leetcode 141 & 142, medium] Linked List Cycle (I and II)
- [LeetCode 112 113] - 路径和I & II (Path Sum I & II)
- [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
- leetcode之 Palindrome Partitioning I&II
- Leetcode Permutation I & II