Queens
来源:互联网 发布:熹妃传刷元宝软件 编辑:程序博客网 时间:2024/05/29 21:33
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span><wbr style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);">The</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);"> <wbr></wbr></span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; line-height: 1.5; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: rgb(255, 255, 255);">n</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);">-queenspuzzle is the problem of placing</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);"> <wbr></wbr></span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; line-height: 1.5; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: rgb(255, 255, 255);">n</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);"> <wbr></wbr></span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);">queenson an</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);"> <wbr></wbr></span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; line-height: 1.5; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: rgb(255, 255, 255);">n</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);">�</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; line-height: 1.5; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: rgb(255, 255, 255);">n</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);"> <wbr></wbr></span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);">chessboardsuch that no two queens attack each other.</span></wbr>
Given an integer
Each solution contains a distinct board configuration ofthe 'Q'
'.'
For example,
There exist two distinct solutions to the 4-queens puzzle:
[ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."]]
位运算神解:http://www.matrix67.com/blog/archives/266
2014.8.11 update:
public List<String[]> solveNQueens(int n) { List<Integer> row = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { row.add(i); } List<List<Integer>> result = new ArrayList<List<Integer>>(); helper(result, row, new ArrayList<Integer>(), n); List<String[]> res = new ArrayList<String[]>(); int rowNum; for (List<Integer> col : result) { rowNum = 0; // 循环完了要重新初始! String[] rowStr = new String[n]; for (Integer i : col) { // process one possibility String tmp = ""; for (int j = 0; j < n; j++) { // construct one row string "..Q." if (i.intValue() == j) { tmp = tmp + "Q"; } else { tmp = tmp + "."; } } rowStr[rowNum++] = tmp; } res.add(rowStr); } return res; } void helper(List<List<Integer>> result, List<Integer> row, List<Integer> col, int n) { if (col.size() == n) { result.add(col); } for (int i = 0; i < n; i++) { List<Integer> newCol = new ArrayList<Integer>(col); newCol.add(i); if (isValid(row, newCol)) { helper(result, row, newCol, n); } } } boolean isValid(List<Integer> row, List<Integer> col) { // process col Integer lastElement = col.get(col.size()-1); for (int i = 0; i < col.size() - 1; i++) { if (lastElement.equals(col.get(i))) { return false; } if (Math.abs(i - (col.size()-1)) == Math.abs(col.get(i) - lastElement)) { // 判断对角线时要row[i] - row[last], col[i] - col[last] 低级错误是row-col return false; } } return true; }}
2013:
public class Solution {
}
0 0
- Queens
- eight queens
- eight queens
- N-Queens
- N-Queens
- N-Queens
- N-Queens
- N-Queens
- N-Queens
- N-Queens
- N-Queens
- N-Queens
- N-Queens
- N-Queens
- N-queens
- N-Queens
- Leetcode_n-queens
- N-Queens
- 罗马数字转化
- Letter combinations of phone number
- sudoku
- word search
- Restore IP address
- Queens
- surrounded region
- Palindrome Partition
- Decode Ways
- Ibizcybertech阿联酋IMS的软件服务,解决方案,今天再次拉低大型机的成本
- 关于DFS和递归的一点总结
- 【cocos2d-x官方文档】骨骼动画详解
- hd 1465 不容易系列之一
- 股票买卖