N-Queens I&&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 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行,那么说明目前棋盘的布局是N皇后解的一种
 1 class Solution {
 2 public:
 3     vector<vector<string> > solveNQueens(int n) {
 4         ans.clear();
 5         board.resize(n);    //初始化棋盘
 6         for(int i=0; i<n; ++i)
 7             board[i].resize(n, false);
 8         dfs(0, n);
 9         return ans;
10     }
11
12     void dfs(int k, int n) {
13         if( k==n ) {
14             vector<string> vstr = board2str();
15             ans.push_back(vstr);
16             return ;
17         }
18         for(int i=0; i<n; ++i)
19             if( canPut(k, i, n) ) {
20                 board[k][i] = true;
21                 dfs(k+1, n);
22                 board[k][i] = false;
23             }
24     }
25
26     bool canPut(int x, int y, int n) {
27         for(int i=0; i<n; ++i)  //横向
28             if( board[x][i] ) return false;
29         for(int i=0; i<n; ++i)  //纵向
30             if( board[i][y] ) return false;
31         int i = x;
32         int j = y;
33         //斜左向上
34         while( i>=0 && j>=0 ) {
35             if( board[i][j] ) return false;
36             --i,--j;
37         }
38         //斜右向下
39         i=x, j=y;
40         while( i<n && j<n ) {
41             if( board[i][j] ) return false;
42             ++i,++j;
43         }
44         //斜右向上
45         i=x, j=y;
46         while( i>=0 && j<n ) {
47             if( board[i][j] ) return false;
48             --i,++j;
49         }
50         //斜左向下
51         i=x, j=y;
52         while( i<n && j>=0 ) {
53             if( board[i][j] ) return false;
54             ++i,--j;
55         }
56         return true;
57     }
58
59     vector<string> board2str() {    //将棋盘转为string
60         vector<string> ans;
61         for(int i=0; i<board.size(); ++i) {
62             string str;
63             for(int j=0; j<board[i].size(); ++j)
64                 str.push_back( board[i][j] ? ‘Q‘ : ‘.‘ );
65             ans.push_back(str);
66         }
67         return ans;
68     }
69
70 private:
71     vector< vector<bool> > board;
72     vector< vector<string> > ans;
73 };


N-Queens II


Follow up for N-Queens problem.

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

改动I中所用的方法,直接获取解的个数。
时间: 2024-10-12 17:50:52

N-Queens I&&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 问题为例

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里面的数据,所以在工艺的稳定性以及设计的可靠性