LeetCode — 51. N皇后

解法一:

class Solution {
    public static List<List<String>> output;
    public List<List<String>> solveNQueens(int n) {
        output = new ArrayList<>();
        // 声明一个长度为n的数组用来代表 第n行棋子是在第result[n]列
        int[] result = new int [n];
        place(0, n, result);
        return output;
    }
    
    // n 皇后问题 row代表计算到了到第row行
    private static void place(int row, int n, int[] result){
        if (row == n){
            // 到达第n行代表已经得到一个将解决方案 直接返回即可
            // 根据result数组将结果加入到output列表中
            show(result);
            return;
        }
        // 若不是第n行 则说明需要继续判断该行棋子应该在那一列
        for (int column = 0; column < n; column++){
            // 判断第row行 放置在column列的棋子是否满足要求
            if (isValid(row, column, result)){
                result[row] = column;
                // 递归判断下一行的情况
                place(row + 1, n, result);
            }
            // 不满足要求 回溯下一列 对应操作column++
        }
    }
    
    // row代表行数 column代表列数 result代表满足规则的棋子在第n行中的位置
    private static boolean isValid(int row, int col, int[] result){
        // 判断棋子的位置是否正确 不正确返回false
        for (int i = 0; i < row; i++){        
        	// 第col列已经有皇后
        	if (result[i] == col) return false;
        	// 第i行的皇后跟第row行第col列格子处在同一斜线上
        	if (row - i == Math.abs(col - result[i])) return false;
        }
    	return true;
    }
    
    private static void show(int[] result){
        List<String> one = new ArrayList<>();
        for (int row = 0; row < result.length; row++){
            // 一行一个StringBuilder
            StringBuilder str = new StringBuilder();
            for (int column = 0; column < result.length; column++){
                if (column == result[row]){
                    str.append("Q");
                }else{
                    str.append(".");
                }
            }
            one.add(str.toString()); 
        }
        output.add(one);
    }
}

解法二:

class Solution {
    // 数组索引是行号,数组元素是列号
    private int[] queens;
	// 标记着某一列是否有皇后
	boolean[] cols;
	// 标记着某一斜线上是否有皇后(左上角 -> 右下角)
	boolean[] leftTop;
	// 标记着某一斜线上是否有皇后(右上角 -> 左下角)
	boolean[] rightTop;
	// 输出数组
	public List<List<String>> output;
    public List<List<String>> solveNQueens(int n) {
    	output = new ArrayList<>();
        queens = new int[n];
        cols = new boolean[n];
        leftTop = new boolean[(n << 1) - 1];
        rightTop = new boolean[(n << 1) - 1];
   		place(0);
   		return output;        
    }
    // 从第row行开始摆放皇后
	void place(int row) {
    	if (row == cols.length) {
        	show();
        	return;
    	}
    	for (int col = 0; col < cols.length; col++) {
        	if (cols[col]) continue;
        	int ltIndex = row - col + cols.length - 1;
        	if (leftTop[ltIndex]) continue;
        	int rtIndex = row + col;
        	if (rightTop[rtIndex]) continue;
        	cols[col] = leftTop[ltIndex] = rightTop[rtIndex] = true;
            queens[row] = col;
        	place(row + 1);
        	cols[col] = leftTop[ltIndex] = rightTop[rtIndex] = false;
     	}
	}
    private void show(){
        List<String> one = new ArrayList<>();
        for (int row = 0; row < queens.length; row++){
            // 一行一个StringBuilder
            StringBuilder str = new StringBuilder();
            for (int column = 0; column < queens.length; column++){
                if (column == queens[row]){
                    str.append("Q");
                }else{
                    str.append(".");
                }
            }
            one.add(str.toString()); 
        }
        output.add(one);
    }
}
发布了163 篇原创文章 · 获赞 18 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/songzhuo1991/article/details/104094286
今日推荐