n皇后问题leetcode-51. N-Queens
n皇后问题是应用回溯法的经典问题。任一行、列、对角线不能有两皇后并存,因此在判断是否合法时,可以将某一行是否有皇后、某一列是否有皇后分别用数组存起来。注意到,对于往左下右上的对角线,每个点的行号(i)和列号(j)的和相等且与别的对角线不同,因此可用数组将此对角线是否有皇后,即i+j是否为1记录下来,n*n的棋盘有2*n-1条左下到右上的对角线;对于从左上到右下的对角线也类似,每个点的行号(i)和列号(j)的差相等且与别的对角线不同,但是差值可能会出现负值,所以将差值加上n-1,便于用数组记录。
解决判断是否合法的问题后,就要考虑怎么用回溯法求解了。我的思路是在探测第i行后,如果找到一个可以放置皇后的位置j后,则会递归探测下一行,结束后则会继续探测i行j+1列,故可以找到所有的n皇后的解。
class Solution {
public:
vector<vector<string>> ans;
vector<string> res;
vector<vector<string>> solveNQueens(int n) {
res = vector<string>(n,string(n,'.'));
nQueens(,n);
return ans;
}
void nQueens(int i,int n)
{//在n*n的棋盘上,第i-1行之前满足条件
if(i>=n)
{
ans.push_back(res);
return;
}
for(int j=;j<n;j++)
{
res[i][j] = 'Q';
if(isValid(res,n))
nQueens(i+,n);
res[i][j] = '.';
}
}
bool isValid(vector<string>& sol, int n)
{//行和列用数组保存,左下的对角线和一样,右下的对角线差一样,且独一无二,也用数组表示
vector<int> row(n, );
vector<int> col(n, );
vector<int> ldia( * n - , );
vector<int> rdia( * n - , );
for (int i = ; i < sol.size(); i++)
{
string str = sol[i];
for (int j = ; j < n; j++)
{
if (sol[i][j] == 'Q')
{
if (row[i] || col[j] || ldia[i + j] || rdia[i-j+n-])
return false;
row[i] = ; col[j] = ;
ldia[i + j] = ; rdia[i-j+n-] = ;
}
}
}
return true;
}
};
n皇后问题leetcode-51. N-Queens的更多相关文章
- [LeetCode] 51. N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- [leetcode]51. N-QueensN皇后
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- LeetCode 51. N-QueensN皇后 (C++)(八皇后问题)
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- Java实现 LeetCode 51 N皇后
51. N皇后 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问题的解决 ...
- leetcode 51. N皇后 及 52.N皇后 II
51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...
- leetcode 51 N皇后问题
代码,由全排列转化而来,加上剪枝,整洁的代码: 共有4个变量,res(最终的结果),level,当前合理的解,n皇后的个数,visit,当前列是否放过皇后,由于本来就是在新的行方皇后,又通过visit ...
- LeetCode: 51. N-Queens(Medium)
1. 原题链接 https://leetcode.com/problems/n-queens/description/ 2. 题目要求 游戏规则:当两个皇后位于同一条线上时(同一列.同一行.同一45度 ...
- 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- LeetCode - 51. N-Queens
51. N-Queens Problem's Link ------------------------------------------------------------------------ ...
- leetcode@ [51/52] N-Queens
https://leetcode.com/problems/n-queens/ class Solution { public: void dfs(vector<vector<string ...
随机推荐
- js -去掉首尾的空格.
function trimFE (str) { return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }
- Jenkins integration for AngularJS code coverage
Install Jenkins plugins 'Cobertura' and 'HTML Publisher' 1. add Post-build Actions "Publish HTM ...
- PhpStorm 注册码
JetBrains PhpStorm key PhpStorm注册码 User Name : EMBRACE License Key : License Key : ===== LICENSE B ...
- 如何在swift中实现oc中的分类
在oc中为了增强已有类的功能,我们经常使用分类.使用分类,我们可以在不破坏原有类的结构的前提下,对原有类进行模块化的扩展. 但是在swift中没有分类这种写法了.相对应的是swift中只有扩展(Ext ...
- 类库探源——System.Exception
一.MSDN描述 Exception 类: 表示在应用程序执行期间发生的错误 命名空间 : System 程序集: mscorlib.dll 继承关系: 常用属性(含字段)和方法: 1. 属性Me ...
- ZOJ 1074 To the Max(DP 最大子矩阵和)
To the Max Time Limit: 2 Seconds Memory Limit: 65536 KB Problem Given a two-dimensional array o ...
- JqGrid实现自定义查询
$("#jqGridId").setGridParam({url:"数据查询地址"}).trigger("reloadGrid");
- winform C#屏幕右下角弹出消息框并自动消失
①弹出信息框后慢慢下降消失 在主窗体中新增按钮重命名为btnShowAndDisappearMessages,在click事件中写如下代码: private void btnShowAndDisapp ...
- destoon代码从头到尾捋一遍
destoon® B2B网站管理系统(以下简称destoon)由西安嘉客信息科技有限责任公司独立研发并推出,对其拥有完全知识产权,中国国家版权局计算机软件著作权登记号:2009SR037570. 系统 ...
- 实现windows和linux的NFS交互
说明:本文是Omni-NFS-X Windows与Linux间通讯的另一种方式和在windows中配置使用NFS客户端的杂交篇 概述 windows/winnt4.0/win2000与Linux/Fr ...