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.."]
]

 分析: The classic recursive problem.
1. Use a int vector to store the current state,  A[i]=j refers that the ith row and jth column is placed a queen.
2. Valid state:  not in the same column, which is A[i]!=A[current], not in the same diagonal direction: abs(A[i]-A[current]) != r-i

3. Recursion: 
       Start:   placeQueen(0,n)
        if current ==n then print result
        else
            for each place less than n,
                 place queen
                if current state is valid, then place next queen   place Queen(cur+1,n)
           end for
        end if

class Solution {
public:
void record(vector<int> row)
{
vector<string> temp;
for(int i = ; i< n ; i++)
{
string str(n,'.');
str[row[i]] = 'Q';
temp.push_back(str);
}
res.push_back(temp) ;
}
bool isValid(vector<int> row, int curRow)
{
for(int i = ; i< curRow; i++)
if(row[i] == row[curRow] || abs(row[i] - row[curRow]) == curRow - i)
return false; return true;
}
void nqueue(vector<int> row,int curRow)
{
if(curRow == n)
{
record(row);
return ;
}
for(int i = ; i< n ;i++)
{
row[curRow] = i;
if(isValid(row,curRow))
nqueue(row,curRow+);
}
}
vector<vector<string> > solveNQueens(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res.clear();
if( n < ) return res;
this->n = n;
vector<int> row(n,-);
nqueue(row, );
return res;
}
private:
int n;
vector<vector<string> > res;
};

http://yucoding.blogspot.com/2013/01/leetcode-question-59-n-queens.html

LeetCode_N-Queens的更多相关文章

  1. Jeff Somers's N Queens Solutions 最快的n皇后算法

    /* Jeff Somers * * Copyright (c) 2002 * * jsomers@alumni.williams.edu * or * allagash98@yahoo.com * ...

  2. [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 ...

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

    题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...

  4. lintcode 中等题:N Queens N皇后问题

    题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...

  5. Codeforces Gym 100650D Queens, Knights and Pawns 暴力

    Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...

  6. Poj 3239 Solution to the n Queens Puzzle

    1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS ...

  7. Pat1128:N Queens Puzzle

    1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...

  8. PAT 1128 N Queens Puzzle

    1128 N Queens Puzzle (20 分)   The "eight queens puzzle" is the problem of placing eight ch ...

  9. kolla queens on centos7.5 -all in one

    目录 环境准备 开始配置 快照,快照,快照 pull镜像并部署 登录配置OpenStack 环境准备 我这里用workstation创建了一个虚拟机,安装centos7.5 mini系统,这台虚拟机上 ...

  10. openstack系列文章(1)devstack安装测试Queens

    1.在OpenStack 圈子中,有这么一句名言:”不要让朋友在生产环境中运行DevStack.但是初学者在没有掌握OpenStack CLI的情况下用devstack安装测试环境还是不错的.本系列文 ...

随机推荐

  1. Light OJ 1314 Names for Babies

    http://www.lightoj.com/volume_showproblem.php?problem=1314 题意:给定一个串和p,q,求长度在p到q之间的子串有几种 思路:后缀数组,对于每个 ...

  2. Qt构建工具QBS之零 —— QBS 概览

    本系列文章起因 自己非常喜欢 QT 这个框架, 使用 QT 这几年, IDE 一直是使用的 QT 自带的 Qt Creator, 这个 IDE 本身比较轻巧, 同事相关的语法提示之类的也算够用, 但是 ...

  3. 常用的Windows批处理

      切换执行路径 如果不换盘的话:cd xxx换盘:cd /d xxx   获取当前日期 编写Windows批处理时经常会需要使用到日期和时间作为文件名,所以是非常重要的. 如何获取日期呢?格式:  ...

  4. Linux & Python 导航目录

    < Python学习手册(第4版)>< Python Cookbook(第2版)>中文版.pdf< Python 高级编程>< Python 基础教程 第二版 ...

  5. 微软 Dynamics AX 学习步骤

    第一步:了解到AX的架构,AOT结构,了解AOT中表,窗体,类,job,菜单,菜单项的基础开发.知道代码可以写在那里,每个对象以及对象内部的具体设置.如果你不了解类,继承,这些,那么就需要找一下讲述类 ...

  6. 高性能以太网芯片W5500 数据手册 V1.0(一)

    W5500 W5500 是一款全硬件 TCP/IP 嵌入式以太网控制器,为嵌入式系统提供了更加简易的互联网连接方案.W5500 集成了 TCP/IP 协议栈,10/100M 以太网数据链路层(MAC) ...

  7. hdu 1874 畅通工程续(最短路)

    最短路问题! 最简单的最短路问题! 恩! #include<stdio.h> #define MAX 1000000 int map[500][500]; int n,m,start,en ...

  8. 关于bootstrap--表单(下拉<select>、输入框<input>、文本域<textare>复选框<checkbox>和单选按钮<radio>)

    html 里面的 role 本质上是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明.通常这种情况出现在一些自定义的组件上,这样可增强组件的可访问性.可用性和可交互性 ...

  9. JAVA并发实现四(守护线程和线程阻塞)

    守护线程     Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) 用户线程即运行在前台的线程,而守护线程是运行在后台的线程. 守护线程作用是为其他前台 ...

  10. python3-day3(函数-参数)

    1.参数 函数的核心是参数传值,其次是返回值,熟练这两这个技术即可灵活使用函数. 1>普通参数 def  show(name): print(name) show('tom') 2>默认参 ...