N-Queens leetcode java

题目:

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.."] ]题解:这道题很经典,网上有很多讲解实例。在国际象棋中,皇后最强大,可以横竖斜的走(感谢AI课让我稍微对国际象棋了解了一下)。而八皇后问题就是让8个皇后中的每一个的横竖斜都没有其他皇后,这样其实感觉是一种和棋的状态。。不知道说的对不对。。。毕竟真实棋盘中不能放8个皇后。。额。。这道题是N皇后,我说着说着就说到了8皇后去了。。其实是一样的。。。

我这道题的解法也是参考了网上的一些人的讲法。经典的DFS递归回溯解法,大体思路就是对每一行,按每一列挨个去试,试到了就保存结果没试到就回溯。难点大概就是用1个一维数组存皇后所在的坐标值。对于一个棋盘来说,每个点都有横纵坐标,用横纵坐标可以表示一个点。而这道题巧就巧在,每一行只能有一个皇后,也就是说,对于一行只能有一个纵坐标值,所以用1维数组能提前帮助解决皇后不能在同一行的问题。那么用一维数组表示的话,方法是:一维数组的下标表示横坐标(哪一行),而数组的值表示纵坐标(哪一列)。

例如:对于一个4皇后问题,声明一个长度为4的数组(因为行数为4)。     A[] = [1,0,2,3]表达含义是:     当前4个皇后所在坐标点为:[[0,1],[1,0],[2,2],[3,3]](被我标蓝的正好是数组的下标,标粉的正好是数组的值)     相当于:A[0] = 1, A[1] = 0, A[2] = 2, A[3] = 3 

这样以来,皇后所在的坐标值就能用一维数组表示了。而正是这个一维数组,在回溯找结果的时候不需要进行remove重置操作了,因为回溯的话正好就回到上一行了,就可以再重新找下一个合法列坐标了。

因为是按照每一行去搜索的,当行坐标值等于行数时,说明棋盘上所有行都放好皇后了,这时就把有皇后的位置标为Q,没有的地方标为0。按照上面讲的那个一维数组,我们就可以遍历整个棋盘,当坐标为(row,columnVal[row])的时候,就说明这是皇后的位置,标Q就行了。

代码如下:

1     public ArrayList<String[]> solveNQueens(int n) {
 2         ArrayList<String[]> res = new ArrayList<String[]>();
 3         if(n<=0)
 4             return res;
 5             
 6         int [] columnVal = new int[n];
 7         
 8         DFS_helper(n,res,0,columnVal);
 9         return res;
10     }
11     
12     public void DFS_helper(int nQueens, ArrayList<String[]> res, int row, int[] columnVal){
13         if(row == nQueens){
14             String[] unit = new String[nQueens];
15             for(int i = 0; i < nQueens; i++){
16                 StringBuilder s = new StringBuilder();
17                 for(int j = 0; j < nQueens; j++){
18                     if(j == columnVal[i])
19                         s.append("Q");
20                     else
21                         s.append(".");
22                 }
23                 
24                 unit[i] = s.toString();
25             }
26             
27             res.add(unit);
28         }else{
29             for(int i = 0; i < nQueens; i++){
30                 columnVal[row] = i;//(row,columnVal[row)==>(row,i)
31                 
32                 if(isValid(row,columnVal))
33                     DFS_helper(nQueens, res, row+1, columnVal);
34             }
35         }
36     }
37     
38     public boolean isValid(int row, int [] columnVal){
39         for(int i = 0; i < row; i++){
40             if(columnVal[row] == columnVal[i]
41                ||Math.abs(columnVal[row]-columnVal[i]) == row-i)
42                return false;
43         }
44         return true;
45     }

Reference:http://zh.wikipedia.org/wiki/%E5%85%AB%E7%9A%87%E5%90%8E%E9%97%AE%E9%A2%98http://blog.csdn.net/linhuanmars/article/details/20667175http://blog.csdn.net/u011095253/article/details/9158473
时间: 2024-10-10 20:52:13

N-Queens leetcode java的相关文章

N-Queens II leetcode java

题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 题解: 这道题跟NQueens的解法完全一样(具体解法参照N QueensN Queens leetcode java),只不过要求的返回值不同了..所以要记录的result稍微改一下就好了... 因为涉及到递归,result传进去引用类型(

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Pascal&#39;s Triangle leetcode java(杨辉三角)

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题解:既然讲到了Pascal‘s Triangle,即杨辉三角.那么就先去Wikipedia上面复习一下杨辉三角吧:”杨辉三角形,又称賈憲三角形.帕斯卡三角形.海亚姆三角形,是二项式係數在的

Triangle leetcode java

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

Word Ladder leetcode java

题目: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:

Set Matrix Zeroes leetcode java

题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple improvement