码迷,mamicode.com
首页 > 其他好文 > 详细

N-Queens

时间:2015-04-11 22:13:56      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
}

 

 

N-Queens

标签:

原文地址:http://www.cnblogs.com/NickyYe/p/4418395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!