LeetCode 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.

LeetCode N-Queens_第1张图片

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.."]
]

利用回朔法

public class Solution {
    public List<List<String>> solveNQueens(int n) {
        //棋盘,0表示干净,n表示放有皇后n,负数表示是有多少个皇后将这个格子据为己有
        int[][] chessboard = new int[n][n];
        List<List<String>> result = new ArrayList<List<String>>();
        place(1,chessboard,result);
        return result;
    }
    /* * 将第n个皇后放到棋盘上 */
    public static void place(int n,int[][] chessboard,List<List<String>> result){
        for(int j = 0; j < chessboard.length ; j++){
            //格子是否干净
            if(chessboard[n-1][j] == 0){
                //是否是最后一个皇后
                if(n == chessboard.length){
                    chessboard[n-1][j] = n;
                    result.add(printChessboard(chessboard));
                    chessboard[n-1][j] = 0;
                    //break;
                }else{
                    //将皇后n放到棋盘格子[n-1][j]上
                    chessboard[n-1][j] = n;
                    occupy(n-1,j,chessboard);
                    place(n+1,chessboard,result);
                    chessboard[n-1][j] = 0;
                    clear(n-1,j,chessboard);
                }
            }
        }

    }
    //占地盘
    public static void occupy(int row , int col,int[][] chessboard){
        int n = chessboard.length;
        int i = 0;
        int j = 0;
        //将同一行据为己有
        for(j = 0; j < n ; j ++){
            if(j != col){
                chessboard[row][j]--;
            }
        }
        //将同一列据为己有
        for(i = 0; i < n ; i ++){
            if(i != row){
                chessboard[i][col]--;
            }
        }
        //将左上斜线据为己有
        i = row - 1;
        j = col - 1;
        while(i>=0 && j>= 0){
            chessboard[i][j]--;
            i--;
            j--;
        }
        i = row + 1;
        j = col + 1;
        //将右下斜线据为己有
        while(i < n && j < n){
            chessboard[i][j]--;
            i++;
            j++;
        }
        //将右上斜线
        i = row - 1;
        j = col + 1;
        while(i>=0 && j < n){
            chessboard[i][j]--;
            i--;
            j++;
        }
        //将左下斜线
        i = row + 1;
        j = col - 1;
        while(i < n && j >= 0){
            chessboard[i][j]--;
            i++;
            j--;
        }
    }
    //清理地盘
    public static void clear(int row , int col,int[][] chessboard){
        int n = chessboard.length;
        int i = 0;
        int j = 0;
        //清理同一行
        for(j = 0; j < n ; j ++){
            if(j != col){
                chessboard[row][j]++;
            }
        }
        //清理同一列
        for(i = 0; i < n ; i ++){
            if(i != row){
                chessboard[i][col]++;
            }
        }
        //清理左上斜线
        i = row - 1;
        j = col - 1;
        while(i>=0 && j>= 0){
            chessboard[i][j]++;
            i--;
            j--;
        }
        i = row + 1;
        j = col + 1;
        //清理右下斜线
        while(i < n && j < n){
            chessboard[i][j]++;
            i++;
            j++;
        }
        //将右上斜线
        i = row - 1;
        j = col + 1;
        while(i>=0 && j < n){
            chessboard[i][j]++;
            i--;
            j++;
        }
        //将左下斜线
        i = row + 1;
        j = col - 1;
        while(i < n && j >= 0){
            chessboard[i][j]++;
            i++;
            j--;
        }
    }
    public static List<String> printChessboard(int[][] chessboard){
        List<String> list = new ArrayList<String>();
        for(int i = 0; i < chessboard.length; i++){
            StringBuffer buffer = new StringBuffer();
            for(int j = 0; j < chessboard[0].length; j++){
                if(chessboard[i][j] > 0){
                    buffer.append('Q');
                }else{
                    buffer.append('.');
                }
            }
            list.add(buffer.toString());
        }
        return list;
    }
}

你可能感兴趣的:(java,n皇后问题)