lintcode-34-N皇后问题 II
34-N皇后问题 II
根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局。
样例
比如n=4,存在2种解决方案
标签
递归
思路
参考http://www.cnblogs.com/libaoquan/p/7073252.html
code
class Solution {
public:
/**
* Calculate the total number of distinct N-Queen solutions.
* @param n: The number of queens.
* @return: The total number of distinct solutions.
*/
int totalNQueens(int n) {
// write your code here
if(n == 1) {
return 1;
}
else if(n < 4) {
return 0;
}
int result = 0;
int i=0, row=0, col=0, j=0, k=0;
int *pCheckerboard = new int[n];
for(i=0; i<n; i++) {
pCheckerboard[i] = -1;
}
while(row < n) {
while(col < n) {
if(canPlace(row, col, n, pCheckerboard)) {
pCheckerboard[row] = col;
col = 0;
break;
}
else {
col++;
}
}
if(pCheckerboard[row] == -1) {
if(row == 0) {
break;
}
else {
row--;
col = pCheckerboard[row] + 1;
pCheckerboard[row] = -1;
continue;
}
}
if(row == n-1) {
result++;
col = pCheckerboard[row] + 1;
pCheckerboard[row] = -1;
continue;
}
row++;
}
delete[] pCheckerboard;
return result;
}
int canPlace(int row, int col, int n, int *pCheckerboard) {
int i;
for(i=0; i<n && i!=row; i++) {
if(pCheckerboard[i] == col) {
return 0;
}
if(abs(row-i) == abs(col-pCheckerboard[i])) {
return 0;
}
}
return 1;
}
};
lintcode-34-N皇后问题 II的更多相关文章
- 34 N皇后问题Ⅱ
原题网址:https://www.lintcode.com/zh-cn/old/problem/n-queens-ii/ 34. N皇后问题 II 描述 笔记 数据 评测 讨论区 根据n皇后问题, ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- [LintCode] Coins in a Line II 一条线上的硬币之二
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- lintcode :Coins in Line II 硬币排成线 II
题目 硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 是 ...
- lintcode:买卖股票的最佳时机 II
买卖股票的最佳时机 II 假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再 ...
- lintcode : 二叉树的层次遍历II
题目 二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, ...
- lintcode:最大子数组II
题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 样例 给出数组[1, 3, -1, 2, -1, 2], ...
- lintcode :Segmemt Tree Build II
题目 Segmemt Tree Build II The structure of Segment Tree is a binary tree which each node has two attr ...
随机推荐
- Web移动端商城 移动端商城手机网站html整套模板,web移动商城仿app手机模板下载
--Web移动端商城移动端商城手机网站html整套模板,web移动商城仿app手机模板下载.原生的js和jquery-1.6.2.min.js,页面才有html5自适应.包括首页(轮播,导航).兼职( ...
- S3C2440上LCD驱动(FrameBuffer)实例开发讲解(一)
一.开发环境 主 机:VMWare--Fedora 9 开发板:Mini2440--64MB Nand, Kernel:2.6.30.4 编译器:arm-linux-gcc-4.3.2 二.背景知识 ...
- python 解积分方程
引用:https://www.aliyun.com/jiaocheng/527786.html sympy求解极限.积分.微分.二元一次方程:http://www.gzhshoulu.wang/art ...
- Git安装配置(Windows)
下载Git并安装 下载地址:https://git-scm.com/ 安装一般默认即可 配置用户信息 配置之前最好已经有了Github的账号,如果没有可以先去注册一个 安装后打开Git Bash gi ...
- R语言爬虫:CSS方法与XPath方法对比(代码实现)
CSS选择器和XPath方法都是用来定位DOM树的标签,只不过两者的定位表示形式上存在一些差别: CSS 方法提取节点 library("rvest") single_table_ ...
- LeetCode初级算法的Python实现--动态规划
动态规划的本质是递归:所以做题之前一定要会递归:递归式就是状态转移方程:这里将会介绍使用动态规划做题的思维方式. 统一的做题步骤: 1.用递归的方式写出代码:(此方法写的代码在leetcode中一定会 ...
- stylus , another css processor
1. install from npm sudo npm install stylus 2. create a styl file named step1.styl border-radius() { ...
- 北京Uber优步司机奖励政策(1月17日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 安装MySQLdb模块遭遇"fatal error: my_config.h: No such file or directory"的处理
Issue I encountered an error when I run the python script which need to import the module of & ...
- 使用.net 更新word目录
方案一.采用OpenXml(服务器不依赖Office组件) 在word生成的最后加上代码: using (WordprocessingDocument docx = WordprocessingDoc ...