一天一道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();
            }
        }
    }
};

最新文章

  1. SQL Server中的高可用性(1)----高可用性概览
  2. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
  3. 虚拟机Linux----Ubuntu1204----退格键方向键无法使用
  4. Java for LeetCode 049 Anagrams
  5. 9.0 alpha 版安装出现 could not execute command lessc 的问题
  6. Unix环境链接静态库
  7. DTN学习的一些有用链接
  8. struts 2 --SEVERE: Could not find action or result
  9. 积累的VC编程小技巧之文件操作
  10. Java设计模式——装饰模式
  11. mysql添加用户,授权,刷新权限
  12. day47-python爬虫学习二
  13. Servlet映射
  14. Perl包和模块(内容来自beginning perl)
  15. AI Accord.NET入门
  16. react学习笔记1一基础知识
  17. DTLS协议中client/server的认证过程和密钥协商过程
  18. Linux通配符与基础正则表达式、扩展正则表达式
  19. Java-控制台传递参数
  20. Route Between Two Nodes in Graph

热门文章

  1. Check the string CodeForces - 960A
  2. Docker 移除镜像
  3. OpenCV+VS2013 属性表配置
  4. 【我的书】《Unity Shader入门精要》出版上市
  5. 【安卓开发】Layout Inflation不能这么用
  6. 剑指Offer——小米+小红书笔试题+知识点总结
  7. SSL协议相关证书文件
  8. 1.Cocos2dx 3.2中vector,ValueMap,Touch触摸时间的使用.iconv字符编解码
  9. Python 自动刷博客浏览量
  10. The Chain Of Responsibility (1)