zoukankan      html  css  js  c++  java
  • 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.."]
    ]

    n皇后问题,题意就是求n×n矩阵中,每行放一个棋子,使得棋子所在的列和两条斜线上没有其他棋子,枚举所有可能。

    dfs去遍历,考虑所有可能,row中记录每一行棋子的位置,col记录当前列是否有棋子,对角线的判断就是两点行差值和列差值是否相同。

    当dfs深度达到n时,就表示存在满足条件的解,把当前状态图存到结果中。

    temp(n, '.')先把字符串全部赋值成 ‘ . ' ,在吧存在棋子的位置改成’Q‘

    经典的8皇后,递归回溯可解。同时还学了StringBuilder里面一个setCharAt()很方便的方法 

    ref:http://www.2cto.com/kf/201311/256634.html

    public class Solution{
    
        public ArrayList<String[]> solveNQueens(int n) {  
            ArrayList<String[]> result = new ArrayList<String[]>();  
            int[] queenList = new int[n];  
            placeQueen(queenList, 0, n, result);  
            return result;  
        }  
           
        // 递归回溯8皇后,关键记录下到达了哪一行了  
        public void placeQueen(int[] queenList, int row, int n, ArrayList<String[]> result){  
            // Base Case, 已经完成任务了  
            if(row == n){  
                StringBuilder[] sol = new StringBuilder[n];  
                   
                // 对数组内每一个对象都要new出其对象, 并将其全设为'.' 
                for(int i=0; i<n; i++){  
                    sol[i] = new StringBuilder();  
                    for(int j=0; j<n; j++){  
                        sol[i].append(".");  
                    }  
                }  
                // 在相应的地方放置queen  
                for(int i=0; i<n; i++){  
                    sol[i].setCharAt(queenList[i], 'Q');  
                }  
                String[] ss = new String[n];  
                for (int i=0; i<n; i++) {  
                    ss[i] = sol[i].toString();  
                }  
                result.add(ss);  
                return;  
            }  
               
            // 开始这一行的查找  
            // 遍历第row行的所有列,测试哪一个位置是安全的,然后一行一行的放queen 
            for(int col=0; col<n; col++){  
                if(isSafe(queenList, row, col)){  
                    queenList[row] = col;  
                   //递归到下一行
                    placeQueen(queenList, row+1, n, result);  
                }  
            }  
        }  
           
        // 判断是否坐标(row,col)的位置是安全的(检查行,列,正反对角线)  
        // queenList里面存放行,列坐标pair,即queenList[row] = col  
        public boolean isSafe(int[] queenList, int row, int col){
          //遍历当前row之前的所有row
            for(int preRow=0; preRow<row; preRow++){  
                // 得到上一行queen 放在哪一个 col里
                int preCol = queenList[preRow];  
                if(preRow == row){      // 理论上不必检查,因为preRow是总是小于row的  
                    return false;  
                }  
                if(preCol == col){          // 检查是否在同一列  
                    return false;  
                }  
                if(row-preRow == col-preCol){       // 反对角线  
                    return false;  
                }  
                if(preRow+preCol == row+col){       // 正对角线  
                    return false;  
                }  
            }  
            return true;  
        }      
    
    }            
  • 相关阅读:
    复旦大学2016--2017学年第二学期(16级)高等代数II期末考试第八大题解答
    复旦大学2016--2017学年第二学期高等代数II期末考试情况分析
    16 级高代 II 思考题十的多种证明
    16 级高代 II 思考题九的七种解法
    Jordan 块的几何
    实对称阵可对角化的几种证明及其推广
    复旦大学高等代数在线课程2017--2018学年记录
    复旦高等代数II(16级)每周一题
    复旦大学2016--2017学年第一学期(16级)高等代数I期末考试第六大题解答
    复旦大学2016--2017学年第一学期(16级)高等代数I期末考试第七大题解答
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3535578.html
Copyright © 2011-2022 走看看