【一天一道LeetCode】#51. N-Queens
一天一道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的更多相关文章
- 【一天一道LeetCode】#52. N-Queens II
一天一道LeetCode系列 (一)题目 Follow up for N-Queens problem. Now, instead outputting board configurations, r ...
- 【一天一道LeetCode】索引目录 ---C++实现
[一天一道LeetCode]汇总目录 这篇博客主要收藏了博主所做题目的索引目录,帮助各位读者更加快捷的跳转到对应题目 目录按照难易程度:easy,medium,hard来划分,读者可以按照难易程度进行 ...
- 【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- 【一天一道Leetcode】#190.Reverse Bits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- 【一天一道Leetcode】#203.Remove Linked List Elements
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- 【一天一道LeetCode】#290. Word Pattern
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#205. Isomorphic Strings
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
随机推荐
- Android 学习笔记二 自定义按钮形状 颜色 点击渐变
问题:自定义按钮的颜色 形状弧度 渐变效果 1.新建自定义属性button_login.xml (借鉴某大神) <?xml version="1.0" encoding=& ...
- 根据class显示或隐藏多个div
引用一下jquery,然后function放head中 function test(){ $(".1").css("display","none&qu ...
- Docker容器时间与宿主机时间不一致
编写Dockerfile,在其中加入: RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo ' ...
- 自定义View实现五子棋游戏
成功的路上一点也不拥挤,因为坚持的人太少了. ---简书上看到的一句话 未来请假三天顺带加上十一回家结婚,不得不说真是太坑了,去年婚假还有10天,今年一下子缩水到了3天,只能赶着十一办事了. 最近还在 ...
- 【SSH系列】深入浅出spring IOC中三种依赖注入方式
spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入什么?控 ...
- 获取客户信息SQL
/*取客户信息SQL*/ --客户信息 SELECT hou.name 业务实体, hca.account_number 客户编号, hp.party_name 客户名称, arp_addr_pkg. ...
- ASCII 大文字生成器
display text in large ASCII art fonts 显示大ASCII艺术字体 这种东西在源码声明或者软件初始化控制台打印时候很有用. 例如打开: http://www.oran ...
- 协议系列之UDP协议
上节说的TCP协议虽然提供了可靠的传输,但是也有一个缺点,发送速度慢.那么有没有一种协议能快速发送的呢?这节要讨论的就是UDP协议,它提供了更加快的发送速度,但也牺牲了可靠性,它是一种无连接的传输协议 ...
- XMPP(二)-基于asmack+openfire的安卓客户端(仿QQ)的介绍以及个人心得
关于XMPP第一篇-openfire的搭建写完后,就一直在赶本篇所要介绍的这个基于asmack+openfire的安卓客户端,费了不少精力,因为有不少同学在还在焦急的等待着(自恋了呵呵),所以紧赶慢赶 ...
- 剑指offer面试题5 从头到尾打印链表(java)
注:(1)这里体现了java数据结构与C语言的不同之处 (2)栈的操作直接利用stack进行 package com.xsf.SordForOffer; import java.util.Stack; ...