【Lintcode】033.N-Queens II
题目:
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
For n=4, there are 2 distinct solutions.
题解:
Solution 1 ()
class Solution {
public:
void dfs(int &res, int n, vector<int>& pos, int row) {
if(row >= n) {
++res;
return;
}
for(int col=; col<n; ++col) {
if (!isValid(pos, row, col)) {
continue;
}
pos[row] = col;
dfs(res, n, pos, row + );
pos[row] = -;
}
}
bool isValid(vector<int>& pos, int row, int col) {
for (int i = ; i < row; ++i) {
if (pos[i] == col || abs(row - i) == abs(col - pos[i])) {
return false;
}
}
return true;
}
int totalNQueens(int n) {
int res = ;
vector<int> pos(n, -);
dfs(res, n, pos, );
return res;
}
};
【Lintcode】033.N-Queens II的更多相关文章
- 【Lintcode】033.N-Queens
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- 【Lintcode】153.Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【Python】 零碎知识积累 II
[Python] 零碎知识积累 II ■ 函数的参数默认值在函数定义时确定并保存在内存中,调用函数时不会在内存中新开辟一块空间然后用参数默认值重新赋值,而是单纯地引用这个参数原来的地址.这就带来了一个 ...
- 【BZOJ3569】DZY Loves Chinese II
[BZOJ3569]DZY Loves Chinese II 题面 bzoj 题目大意: 给你一张\(N(1\leq N\leq 10^5)\)个点\(M(1\leq M\leq 5\times 10 ...
- 【SPOJ】Longest Common Substring II
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
随机推荐
- URL Handle in Swift (二) — 响应链处理 URL
最后更新: Swift4时候的博客,以前在 CMD markdown 上编辑的,现在搬到这里 在上篇文章-URL Handle in Swift (一) -- URL 分解中,我们已经将URL进行了分 ...
- Spring IOC源码分析之-刷新前的准备工作
目录 ClassPathXmlApplicationContext的注册方式 加载父子容器 配置路径解析 容器刷新 刷新容器之刷新预处理 ClassPathXmlApplicationContext的 ...
- c#中Monitor的使用
首先lock和Minitor有什么区别呢? 其实lock在IL代码中会被翻译成Monitor.也就是Monitor.Enter(obj)和Monitor.Exit(obj). lock(obj) { ...
- hibernate出现 org.hibernate.PropertyNotFoundException: field [departmen] not found on cn.itcast.hibernate.domain.Employee1错误
hibernate出现 org.hibernate.PropertyNotFoundException: field [departmen] not found on cn.itcast.hibern ...
- servletRequest 常用操作
package request; import java.io.IOException;import javax.servlet.ServletException;import javax.servl ...
- jdbc 链接池的优化
package cn.itcast.jdbc.datasourse; import java.sql.Connection;import java.sql.DriverManager;import j ...
- 摩托罗拉SE955 One Discrete Length,Two Discrete Lengths,Length Within Range 相关解释
motorola scanner datasheet相关解释(下面通过Simple Serial Interface(SSI)进行设置,非扫描官方datasheet的设置条码): One Discre ...
- CountDownTimer
package com.daoge.widget; import java.text.DecimalFormat; import android.os.CountDownTimer; import a ...
- WPF DataGrid 获取当前行某列值
[0]是指当前行第1列的单元格位置 注意:DataRowView要求必须引用System.Data命名空间 方法一: DataRowView mySelectedElement = (DataRowV ...
- ASP.NET动态网站制作(13)-- JQ(5)
前言:jq的最后一节课,主要讲解应用, 内容: 1.会飞的li: HTML代码: <!DOCTYPE html> <html xmlns="http://www.w3.or ...