N-Queens II

https://leetcode.com/problems/n-queens-ii/

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

解题思路:

这题和N-Queens相比,有啥简单的方法?没看出,无非就是算出所有解后返回结果的size。当然可能也可以维护一个值,有结果就递增,最后返回他就可以了。

public class Solution {
    public int totalNQueens(int n) {
        List<String[]> result = new ArrayList<String[]>();
        if(n < 1) {
            return 0;
        }
        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.size();
    }

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

又优化了一下省去了构造string的过程,并直接返回这个size。

public class Solution {
    public int totalNQueens(int n) {
        if(n < 1) {
            return 0;
        }
        int[] columnInRow = new int[n];
        for(int i = 0; i < n; i++) {
            columnInRow[i] = -1;
        }
        int result = 0;
        result = dfs(n, columnInRow, 0, result);
        return result;
    }

    public int dfs(int n, int[] columnInRow, int row, int result) {
        if(row == n) {
            result++;
            return result;
        }
        for(int i = 0; i < n; i++) {
            if(checkValid(columnInRow, row, i)) {
                columnInRow[row] = i;
                result = dfs(n, columnInRow, row + 1, result);
                columnInRow[row] = -1;
            }
        }
        return result;
    }

    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;
    }
}
时间: 2024-10-22 11:03:56

N-Queens II的相关文章

lintcode 中等题:N Queens II N皇后问题 II

题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递归的时候才能保存结果,参考程序 java程序: class Solution { /** * Calculate the total number of distinct N-Queen solutions. * @param n: The number of queens. * @return:

58. N-Queens &amp;&amp; N-Queens II

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 configur

[LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the foll

LeetCode:N-Queens I II(n皇后问题)

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 configur

Leetcode | N-Queens I &amp; II

N-Queens I 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 config

N-Queens I&amp;&amp;II

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

惊叹计算机运行速度的提升---以n Queens 问题为例

1 介绍 实现了书<Data Structures and Program design in C++>(Robert L. Kruse and Alexander J. Ryba, 2000)中的188页的基于回溯策略的递归算法solve_from,该算法能够计算n Queens问题的解.选择不同的n作为棋盘大小,能够得出不同棋盘大小的Queens问题的解即执行时间. 该书出版时间为2000年,那么使用的计算机大概为1999年左右的.该书给出了执行的结果数据.我在我的电脑上採用相同的代码和算

leetCode 52.N-Queens II (n皇后问题II) 解题思路和方法

N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 思路:解决了上题,这题也就迎刃而解,或者说这题要不上题还要简单一些. 具体代码如下: public class Solution { int count = 0; public int totalNQueens(int n)

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

使用华邦的SPI FLASH作为EPCS时固化NIOS II软件报错及解决方案

Altera器件有EPCS系列配置器件,其实,这些配置器件就是我们平时通用的SPIFlash,据AlteraFAE描述:"EPCS器件也是选用某家公司的SPIFlash,只是中间经过Altera公司的严格测试,所以稳定性及耐用性都超过通用的SPIFlash".就本人看来,半导体的稳定性问题绝大部分都是由本身设计缺陷造成的,而成熟的制造工艺不会造成产品的不稳定:并且,现在Altera的器件在读入配置数据发生错误时,可以重新读取SPIFlash里面的数据,所以在工艺的稳定性以及设计的可靠性