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步放置皇后,由于皇后之间不能同行、同列、同斜线,所以每次放置皇后的时候,都要考虑是否与已有的皇后“冲突”,如果冲突,则改变位置,直至所有皇后放置完毕。

测试皇后冲突的函数:isValid();

本文思路比较取巧,使用a[n]记录N皇后位置。根据皇后放置规则可知,每一行有且只有一个皇后,所以从第一行到第N行,依次放置第n个皇后。a[n]记录第n个皇后所在列数。即第i个皇后放置在第i行第a[i]列。

参考:http://blog.csdn.net/feixiaoxing/article/details/6877965

具体实现如下(AC 36ms):

class Solution {
public:
vector<vector<string> > re; //测试在第row行,第row列放置皇后是否有效
int isValid(int *a, int n, int row, int col)
{
int tmpcol=0;
for(int tmprow=0;tmprow<row;tmprow++)
{
tmpcol = a[tmprow];
if(tmpcol == col)// 同列
return 0;
if((tmpcol-col) == (tmprow - row))// 在同一右斜线
return 0;
if((tmpcol-col) == (row - tmprow))// 在同一左斜线
return 0;
}
return 1;
} void PrintN(int *a, int n)
{
vector<string> tmps;
for(int i=0;i<n;i++)
{
string s(n,'.');
s[a[i]]='Q';
tmps.push_back(s);
}
re.push_back(tmps);
}
void n_queens(int *a,int n, int index)
{
for(int i=0;i<n;i++)
{
if(isValid(a,n,index,i))
{
a[index]=i;
if(index == n-1)
{
PrintN(a,n);
a[index]=0;
return;
}
n_queens(a,n,index+1);
a[index]=0;
}
}
} vector<vector<string> > solveNQueens(int n) { int *a = new int[n];
memset(a,0,sizeof(int)*n);
n_queens(a,n,0);
return re;
}
};

N皇后个数对应解的个数:(验证程序)

N皇后问题解的个数
n       solution(n)   
1       1   
2       0   
3       0   
4       2   
5       10   
6       4   
7       40   
8       92   
9       352   
10      724   
11      2680   
12      14200   
13      73712   
14      365596   
15      2279184   
16      14772512   
17      95815104   
18      666090624   
19      4968057848   
20      39029188884   
21      314666222712   
22      2691008701644   
23      24233937684440   
24      227514171973736   
25      2207893435808352

N-Queens leetcode的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  3. [Leetcode] n queens ii n皇后问题

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  4. 【leetcode】1222. Queens That Can Attack the King

    题目如下: On an 8x8 chessboard, there can be multiple Black Queens and one White King. Given an array of ...

  5. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  6. [LeetCode] N-Queens N皇后问题

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  7. [CareerCup] 9.9 Eight Queens 八皇后问题

    9.9 Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board so that non ...

  8. Leetcode | N-Queens I & II

    N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...

  9. [LeetCode]题解(python):051-N-Queens

    题目来源 https://leetcode.com/problems/n-queens/ The n-queens puzzle is the problem of placing n queens ...

  10. [Leetcode][Python]52: N-Queens II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...

随机推荐

  1. javascript 代码可读性

    可读性的大部分内容都是和代码缩进相关的,必须保证代码有良好的格式.可读性的另一方面就是注释,一般而言,有如下一些地方需要进行注释 1.1.1 函数和方法 每个函数或方法都应该包含一个注释,描述其目的和 ...

  2. 机器学习---python环境搭建

    一 安装python2.7 去https://www.python.org/downloads/ 下载,然后点击安装,记得记住你的安装路径,然后去设置环境变量,这些自行百度一下就好了. 由于2.7没有 ...

  3. gnuplot配置HOME目录

    http://blog.csdn.net/jspenliany/article/details/39828261 本人使用gnuplot绘图,使用console version的来进行处理的时候,经常 ...

  4. Yocto开发笔记之《驱动调试-华为3G模块》(QQ交流群:519230208)

    QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== 参考:ht ...

  5. MyISAM 和InnoDB 区别 转

    MyISAM 和InnoDB 讲解 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处理 ...

  6. Ubuntu 设置程序开机启动(以指定用户身份)

    一.方法 在/etc/rc.local写程序的启动命令(系统执行内核过程中会启动init进程,该进程把当前runlevel所对应的的所有service 都启动后,才会执行rc.local里的命令),程 ...

  7. centos7.2安装paramiko报error: command 'gcc' failed with exit status 1的解决办法

    安装依赖 yum install kernel-devel libxslt-devel libffi-devel python-devel mysql-devel zlib-devel openssl ...

  8. python 多线程学习

    多线程(multithreaded,MT),是指从软件或者硬件上实现多个线程并发执行的技术 什么是进程? 计算机程序只不过是磁盘中可执行的二进制(或其他类型)的数据.它们只有在被读取到内存中,被操作系 ...

  9. wordpress数据库表说明

    wp系统所用的表不多,那么每张表具体都存些什么?今天给大家介绍一下,希望对你有帮助. wp_commentmeta: 用于保存评论的元信息,在将评论放入回收站等操作时会将数据放入此表,Akismet等 ...

  10. Database Schema Reader

    数据架构与INSERT脚本生成 https://dbschemareader.codeplex.com/wikipage?title=Writing%20Data&referringTitle ...