N-Queens

来源:互联网 发布:疯狂的美工 编辑:程序博客网 时间:2024/05/12 22:59

问题描述

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皇后问题

方法:以后自己慢慢回味,想法就不写了

循环实现
public class Solution {    public List<String[]> solveNQueens(int n) {        List<String[]> re = new LinkedList<String[]>();        int[] queen = new int[n + 1];        int k = 1;        queen[k] = 1;        while(k > 0){            if(k <= n && queen[k] <= n){                if(check(k,queen)){                 //皇后能放在这里,探测下一列,并将行号置为初始值                    k++;                    if (k <= n)                        queen[k] = 1;                }                else queen[k]++;                //探测下一行            }            else{                if(k > n)                    addResult(re,queen);                k--;            //上一行回溯                queen[k]++;         //上一行的下一列开始继续            }        }        return re;    }    boolean check(int row, int[] queen){        for(int i = 1; i < row; i++)            if(queen[i] == queen[row] ||                    Math.abs(i - row) == Math.abs(queen[i] - queen[row]))                return false;        return true;    }    void addResult(List<String[]> re, int[] queen){        StringBuffer sb = new StringBuffer();        int len = queen.length;        String[] strs = new String[len - 1];        for(int i = 1; i < len; i++){            int q = queen[i] - 1;            for(int j = 0; j < len - 1; j++)                sb.append(j == q? 'Q': '.');            strs[i - 1] = sb.toString();            sb.setLength(0);        }        re.add(strs);    }}
递归
public class Solution {    int[] queen;    List<String[]> re;    public List<String[]> solveNQueens(int n) {        re = new LinkedList<String[]>();        queen = new int[n + 1];        placeQueen(1,n);        return re;    }    void placeQueen(int k, int n){        if(k > n)            addResult();        else{            for(int j = 1; j <= n; j++){                if(check(k, j)){                    queen[k] = j;                    placeQueen(k + 1,n);                }            }        }    }    boolean check(int row, int col){        for(int i = 1; i < row; i++)            if(queen[i] == col ||                    Math.abs(i - row) == Math.abs(queen[i] - col))                return false;        return true;    }    void addResult(){        StringBuffer sb = new StringBuffer();        int len = queen.length;        String[] strs = new String[len - 1];        for(int i = 1; i < len; i++){            int q = queen[i] - 1;            for(int j = 0; j < len - 1; j++)                sb.append(j == q? 'Q': '.');            strs[i - 1] = sb.toString();            sb.setLength(0);        }        re.add(strs);    }}
0 0
原创粉丝点击