59 N Queens

研究了一夜,各种bug。

看到了大神的代码,简洁明了。

学习了。

class Solution {
public:
    void printQueen(vector<int> &A,int n,vector<vector<string>> &result){
    vector<string> r;
        for(int i=0;i<n;i++){
            string str(n,‘.‘);
            str[A[i]] = ‘Q‘;
            r.push_back(str);
        }
        result.push_back(r);
}
bool isValidQueens(vector<int>A,int r){
    for(int i=0;i<r;i++){
        if((A[i]==A[r])||(abs(A[i]-A[r]))==(r-i))
            return false;
    }
    return true;
}
void nqueens(vector<int> A,int cur, int n,vector<vector<string>> &result){
    if(cur == n){
        printQueen(A,n,result);
    }else{
        for(int i=0;i<n;i++){
            A[cur] = i;
            if(isValidQueens(A,cur))
                nqueens(A,cur+1,n,result);
        }
    }
}
vector<vector<string> > solveNQueens(int n) {
    vector<vector<string>> result;
    result.clear();
    vector<int> A(n,-1);
    nqueens(A,0,n,result);
    return result;
}
};  
时间: 2024-08-06 11:55:28

59 N Queens的相关文章

54. 八皇后问题[eight queens puzzle]

[本文链接] http://www.cnblogs.com/hellogiser/p/eight-queens-puzzle.html [题目] 在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即任意两个皇后不得处在同一行.同一列或者同一对角斜线上.下图中的每个黑色格子表示一个皇后,这就是一种符合条件的摆放方法.请求出总共有多少种摆法. [分析] 之前博文28.字符串的排列[StringPermutation]介绍过字符串的全排列,八皇后问题也可以转换为全排列问题. 由于八个皇后的任意两个不

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   Memory Limit: 131072K Total Submissions: 3459   Accepted: 1273   Special Judge Description The eight queens puzzle is the problem of putting eight

Jeff Somers&#39;s N Queens Solutions 最快的n皇后算法

1 /* Jeff Somers 2 * 3 * Copyright (c) 2002 4 * 5 * [email protected] 6 * or 7 * [email protected] 8 * 9 * April, 2002 10 * 11 * Program: nq 12 * 13 * Program to find number of solutions to the N queens problem. 14 * This program assumes a twos compl

SQL Server 取前一天的0点和23点59分59秒

DECLARE @startDate1 DATE;DECLARE @startDate DATETIME;SET @startDate1=GETDATE();SELECT @startDate=DATEADD(DAY,-1,@startDate1); DECLARE @endDate DATETIME ;SET @endDate=DATEADD(second,-1,CONVERT(DATETIME,@startDate1)); SELECT @startDate startDate,@endDa

高防服务器 59.36.97.140广东600G高防服务器,打死退款!

广州超高防:G口接入 独享100M 单机600G I5 8G 5000元/台 开80端口,IP段:59.36.97  112.91.174 东莞超防  :G口接入 独享100M 单机300G I5 8G 3800元/台 开80端口,IP段:183.2.193 112.91.22 东莞无限防:G口接入 独享50M  单机200G I5 8G 3000元/台 开80端口,IP段:14.152.83 112.90.168 公司承诺: 1.提供实时流量图 2.机器被打死,按天退款 3.可按照顾客需求,分

ACM-ICPC North America Qualifier 2014 Eight Queens

题意:问图是否满足八皇后. 解题思路:hash,dp,位运算 解题代码: 我的搓代码. 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月14日 星期六 12时00分44秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10

[LeetCode]59.Spiral Matrix II

[题目] 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 ] ] [分析] 模拟 [代码] /**-------------------------

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

题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案包含一个明确的n皇后放置布局,其中“Q”和“.”分别表示一个女王和一个空位置. 样例 对于4皇后问题存在两种解决的方案: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], [&qu

Python----递归------Eight Queens 八皇后问题

递归思想是算法编程中的重要思想. 作为初学者,对递归编程表示很蒙逼,每次遇到需要递归的问题,心里就有一万头草泥马飞过~~~~~~(此处略去一万头草泥马) 在B站看数据结构与算法的视频时,视频中给了两个非常典型的例子--<汉诺塔>和<八皇后问题>,就希望自己用Python实现一下这两个递归程序,其中汉诺塔问题比较简单,还是能够理解,这里就不讲了. <八皇后问题>:说要在一个棋盘上放置8个皇后,但是不能发生战争,皇后们都小心眼,都爱争风吃醋,如果有人和自己在一条线上(水平.