一天一道LeetCode系列

(一)题目

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

]

(二)解题

不玩国际象棋还真不知道这题的规则。百度了好久才明白。

主要有以下三个:

+ 同一行上只能有一个皇后

+ 同一列上只能有一个皇后

+ 两个皇后之间不能处在同一条对角线上

具体解法看代码:

/*
首先利用一个数row和数组a[i]确保每一行每一列只有一个皇后
然后利用一个vector存储已摆放皇后的位置坐标,每摆一个皇后就与已摆放的皇后进行比较,如果在一条对角线上就不能摆
*/
class Solution {
public:
    vector<vector<string>> ret;
    vector<pair<int, int>> queens;//存放已摆放的皇后的坐标值
    vector<vector<string>> solveNQueens(int n) {
        int *a = new int[n];//确保每一列只有一个皇后
        memset(a,0,n*sizeof(int));
        vector<string> res;
        backtrc(res, a, 0, n);
        return ret;
    }
    bool isValid(vector<pair<int,int>> queens , int row,int col)//
    {
        if (queens.empty()) return true;
        for (int i = 0; i < queens.size();i++)
        {
            if (abs(row- queens[i].first) == abs(col-queens[i].second))
            {
                return false;
            }
        }
        return true;
    }
    void backtrc(vector<string> res, int a[], int row, int n)//row确保每一行只有一个皇后
    {
        if (row == n)//如果摆放完n行,则退出
        {
            ret.push_back(res);
            return;
        }
        for (int i = 0; i < n; i++)
        {
            if (a[i] == 0&&isValid(queens, row, i))//保证了同一行,同一列,同一对角线只有一个Q
            {
                a[i] = 1;
                string tmp(n, '.');
                tmp[i] = 'Q';
                res.push_back(tmp);
                queens.push_back(pair<int, int>(row, i));
                backtrc(res, a, row + 1, n);
                //回溯
                a[i] = 0;
                queens.pop_back();
                res.pop_back();
            }
        }
    }
};

【一天一道LeetCode】#51. N-Queens的更多相关文章

  1. 【一天一道LeetCode】#52. N-Queens II

    一天一道LeetCode系列 (一)题目 Follow up for N-Queens problem. Now, instead outputting board configurations, r ...

  2. 【一天一道LeetCode】索引目录 ---C++实现

    [一天一道LeetCode]汇总目录 这篇博客主要收藏了博主所做题目的索引目录,帮助各位读者更加快捷的跳转到对应题目 目录按照难易程度:easy,medium,hard来划分,读者可以按照难易程度进行 ...

  3. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  4. 【一天一道Leetcode】#190.Reverse Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  5. 【一天一道Leetcode】#203.Remove Linked List Elements

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  6. 【一天一道LeetCode】#290. Word Pattern

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 【一天一道LeetCode】#219. Contains Duplicate II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 【一天一道LeetCode】#257. Binary Tree Paths

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 【一天一道LeetCode】#205. Isomorphic Strings

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

随机推荐

  1. jspacker压缩及解压缩研究(js eval)

    起因: 在研究爬虫的时候发现很多网站都出现了同一种方式的js混淆,并且名字都是pde.js,怀疑是使用了同一款混淆工具,所以研究一下. 这款工具叫JS Packer,并不是专门的混淆工具,而是一款js ...

  2. SQL SERVER Management Studio

    1.​ 实验目的 ​ 熟悉SQL SERVER Management Studio的部分操作 ​ 数据SQL SERVER简化版和完整版数据库设计 2.​ 实验内容 2.1.​ 熟悉简化版SQL ...

  3. Java中使用CountDownLatch进行多线程同步

    CountDownLatch介绍 在前面的Java学习笔记中,总结了Java中进行多线程同步的几个方法: 1.synchronized关键字进行同步. 2.Lock锁接口及其实现类ReentrantL ...

  4. 《Non-Negative Matrix Factorization for Polyphonic Music Transcription》译文

    NMF(非负矩阵分解),由于其分解出的矩阵是非负的,在一些实际问题中具有非常好的解释,因此用途很广.在此,我给大家介绍一下NMF在多声部音乐中的应用.要翻译的论文是利用NMF转录多声部音乐的开山之作, ...

  5. Swift对象实例方法名混淆的解决

    在Xcode7.x中,比如有以下一个类: class Foo{ func test(v:Int,before:Int)->Int{ return v + 1 } } 我可以直接这么做: let ...

  6. 24 AIDL案例

    服务端 MainActivity.java package com.qf.day24_aidl_wordserver; import android.app.Activity; import andr ...

  7. Java异常处理-----Throwable类

    Throwable类 1.toString() 输出该异常的类名. 2.getMessage() 输出异常的信息,需要通过构造方法传入异常信息(例如病态信息). 3.printStackTrace() ...

  8. 可能是CAP理论的最好解释

    一篇非常精彩的解释CAP理论的文章,翻译水平有限,不准确之处请参考原文,还请见谅. Chapter 1: "Remembrance Inc" Your new venture : ...

  9. 【Netty源码解析】NioEventLoop

    上一篇博客[Netty源码学习]EventLoopGroup中我们介绍了EventLoopGroup,实际说来EventLoopGroup是EventLoop的一个集合,EventLoop是一个单线程 ...

  10. FFmpeg源代码简单分析:av_write_frame()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...