题目描述

N皇后问题是把N个皇后放在一个N×N棋盘上,使皇后之间不会互相攻击。

给出一个整数n,返回n皇后问题的所有摆放方案
例如:
4皇后问题有两种摆放方案
[".Q..",  // 解法 1
  "...Q",
  "Q...",
  "..Q."],  ["..Q.",  // 解法 2
  "Q...",
  "...Q",
  ".Q.."]
]


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..",  // 解法 1
  "...Q",
  "Q...",
  "..Q."],  ["..Q.",  // 解法 2
  "Q...",
  "...Q",
  ".Q.."]
class Solution {
public:
    vector<vector<string> > solveNQueens(int n) {
        vector <vector<string>> res;
        vector<string> cur(n,string(n,'.'));
        dfs(res,cur,n,0);
        return res;
    }
    
    void dfs(vector <vector<string>> &res,vector <string> &cur,int &n,int row){
        if (row==n){
            res.push_back(cur);
            return ;
        }
        
        for (int j=0;j<n;j++){
            if (isValid (cur,n,row,j)){
                cur[row][j]='Q';
                dfs(res,cur,n,row+1);
                cur[row][j]='.';
            }
        }
    }
    
    bool isValid(vector<string> &cur,int &n ,int row,int col){
        for (int i=0;i<row;i++){
            if (cur[i][col]=='Q'){
                return false;
            }
        }
        
        for (int i=row-1,j=col-1;i>=0 && j>=0;i--,j--){
            if (cur[i][j]=='Q'){
                return false;
            }
        }
        
        for (int i=row-1,j=col+1;i>=0 && j<n;i--,j++){
            if (cur[i][j]=='Q'){
                return false;
            }
                
        }
    
    return true;
    }
};

leetcode99: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. webRTc实现视频直播

    <!DOCTYPE html> <html> <head> <script type='text/javascript' src='https://cdn.s ...

  2. 白话解析:一致性哈希算法 consistent hashing【转】

    学习一致性哈希算法原理的时候看到博主朱双印的一片文章,看完就懂,大佬! 白话解析:一致性哈希算法 consistent hashing

  3. maven下载依赖包下载失败

    在家办公,遇到项目的maven包下载不了,刚开始以为是vpn的问题,折腾半天反复确认之后没有发现什么问题. 同时试过阿里巴巴的maven仓库,删除过以来,重新导过包发现都不行. 后来在idea的设置里 ...

  4. Windows7 提示“无法访问 xxxx,您没有权限访问,请与网络管理员联系请求访问权限”的解决办法

    Windows7 客户端访问提示"无法访问 xxxx,您没有权限访问,请与网络管理员联系请求访问权限"的解决办法

  5. 16.Android-activity生命周期与启动模式

    1.activity共有4个状态 如下图所示: 运行状态 如果一个活动位于屏幕的前台(可见的),那么它就是活动的或正在运行的. 暂停状态 如果一个活动失去了焦点,但仍然可见(也就是说,一个新的非全尺寸 ...

  6. 判断移动还是PC 以及微信环境

    //判断pc还是移动端 function IsPC() {   var userAgentInfo = navigator.userAgent;   var Agents = ["Andro ...

  7. 从源码角度来分析线程池-ThreadPoolExecutor实现原理

    作为一名Java开发工程师,想必性能问题是不可避免的.通常,在遇到性能瓶颈时第一时间肯定会想到利用缓存来解决问题,然而缓存虽好用,但也并非万能,某些场景依然无法覆盖.比如:需要实时.多次调用第三方AP ...

  8. github 上传与删除项目

    在github上创建好仓库后,在本地创建一个仓库 1.先要初始化本地仓库 git config --global user.name 'you name' git config --global us ...

  9. GeoServer发布shapfile字段名和值乱码问题解决

    摘要: 网上说了一大堆方法又是转格式咯又是改源代码了,修改很简单: 修改Styles下的你的style: Xml代码 修改Stores下你的图层的属性,设置 DBF charset为GBK 以上设置G ...

  10. Helium文档13-WebUI自动化-helium快速切换到selenium状态并调用其方法

    前言 前面说过helium是对Selenium 进行了封装,那么我们如何使用selenium的方法呢,通过下面的介绍,我们能够清楚在helium中能够使用selenium的任何方法 入参介绍 def ...