https://leetcode.com/problems/n-queens/
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 '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.."] ]
解题思路:
著名的八皇后问题。开始忘了斜线也不能有棋子,只考虑行列了,所以只定义了一个visited的数组,结果当然是错误。
思路比较明显的是使用回溯,类似dfs遍历的方法。比较关键的是判断当前位置能否放棋子的这个方法。行,列都很简单,有些不直观的是斜线。其实斜线位置,就是和当前位置的row差和column差相等的地方。
有了这个方法,其他就和普通的回溯没有差异了。代码如下。
public class Solution { public List<String[]> solveNQueens(int n) { List<String[]> result = new ArrayList<String[]>(); if(n < 1) { return result; } String[] current = new String[n]; for(int i = 0; i < n; i++) { StringBuffer bf = new StringBuffer(); for(int j = 0; j < n; j++) { bf.append('.'); } current[i] = bf.toString(); } int[] columnInRow = new int[n]; for(int i = 0; i < n; i++) { columnInRow[i] = -1; } dfs(n, result, current, columnInRow, 0); return result; } public void dfs(int n, List<String[]> result, String[] current, int[] columnInRow, int row) { if(row == n) { String[] temp = Arrays.copyOf(current, current.length); result.add(temp); return; } for(int i = 0; i < n; i++) { if(checkValid(columnInRow, row, i)) { columnInRow[row] = i; String temp = current[row]; current[row] = current[row].substring(0, i) + "Q" + current[row].substring(i + 1); dfs(n, result, current, columnInRow, row + 1); current[row] = temp; columnInRow[row] = -1; } } } public boolean checkValid(int[] columnInRow, int row, int column) { int temp = row - 1, i = 1; while(temp >= 0) { if(columnInRow[temp] == column) { return false; } if(column - i >= 0 && columnInRow[temp] == column - i) { return false; } if(column + i < columnInRow.length && columnInRow[temp] == column + i) { return false; } i++; temp--; } temp = row + 1; i = 1; while(temp < columnInRow.length) { if(columnInRow[temp] == column) { return false; } if(column - i >= 0 && columnInRow[temp] == column - i) { return false; } if(column + i < columnInRow.length && columnInRow[temp] == column + i) { return false; } i++; temp++; } return true; } }
update 2015/05/26:
二刷,时间长了,居然忘记了。简便的方法是,在开始就构造出一个全部为点的棋盘。然后验证valid的数组也比较关键,只要用一个一维数组即可。纵坐标i为第几行,它的值为当前行的第几列为Q。
下面的解法简化了valid方法,读起来简单。
public class Solution { public List<String[]> solveNQueens(int n) { List<String[]> res = new ArrayList<String[]>(); if(n == 0) { return res; } // 首先构造一个全为.的board String[] cur = new String[n]; for(int i = 0; i < n; i++) { String temp = ""; for(int j = 0; j < n; j++) { temp += "."; } cur[i] = temp; } // 构造一个全部为-1的数组 int[] columnInRow = new int[n]; for(int i = 0; i < n; i++) { columnInRow[i] = -1; } dfs(n, res, cur, columnInRow, 0); return res; } public void dfs(int n, List<String[]> res, String[] cur, int[] columnInRow, int row) { if(row == n) { String[] temp = Arrays.copyOf(cur, cur.length); res.add(temp); return; } for(int i = 0; i < n; i++) { if(isValid(columnInRow, row, i)) { String temp = cur[row]; cur[row] = cur[row].substring(0, i) + "Q" + cur[row].substring(i + 1); columnInRow[row] = i; dfs(n, res, cur, columnInRow, row + 1); columnInRow[row] = -1; cur[row] = temp; } } } public boolean isValid(int[] columnInRow, int row, int column) { // 查看当前行是否已经有Q if(columnInRow[row] != -1) { return false; } for(int i = 0; i < columnInRow.length; i++) { // 查看当前列是否已经有Q if(columnInRow[i] == column) { return false; } // 查看斜线是否已经有Q if(i != row && columnInRow[i] != - 1 && Math.abs(columnInRow[i] - column) == Math.abs(i - row)) { return false; } } return true; } }