N-Queens II
Description:
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
Code:
#define NUM 100
int x[NUM];
class Solution {
public:
bool isValid(int k)
{
for (int i = ; i < k; ++i)
{
if (x[i] == x[k] || abs(k-i)==abs(x[k]-x[i]) )
return false;
}
return true;
}
int totalNQueens(int n) { for (int i = ; i < NUM; ++i)
x[i] = ; int k = ,number = ;
while ( k >= )
{
x[k]+=;
while (x[k] <= n && !isValid(k))
x[k]+=;
if ( x[k] <= n && k == n)
{
number++;
}
else if ( x[k] <= n && k < n)
{
k = k+;
}
else
{
x[k] = ;
k = k-;
}
}
return number;
}
};
N-Queens II的更多相关文章
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- [Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- LeetCode:N-Queens I II(n皇后问题)
N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...
- 58. N-Queens && N-Queens II
N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...
- [CareerCup] 9.9 Eight Queens 八皇后问题
9.9 Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board so that non ...
- N-Queens | & N-Queens II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- Leetcode | N-Queens I & II
N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...
- [Leetcode][Python]52: N-Queens II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...
- N-Queens I II(n皇后问题)(转)
N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...
- [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...
随机推荐
- 每日一九度之 题目1030:毕业bg
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2046 解决:894 题目描述: 每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为“bg”.参加不同团体的b ...
- hihoCoder 搜索一·24点
题目1 : 搜索一·24点 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 周末,小Hi和小Ho都在家待着.在收拾完房间时,小Ho偶然发现了一副扑克,于是两人考虑用这副扑 ...
- JQuery知识快览之五—操作属性和结构
前文提到JQuery提供了丰富的内置方法来操作对象集,本文主要介绍JQuery中的那些内置操作属性和DOM结构的方法. prop还是attr? 从JQuery1.6版开始,新增了prop方法来获取和设 ...
- Linux 线程与进程,以及通信
http://blog.chinaunix.net/uid-25324849-id-3110075.html 部分转自:http://blog.chinaunix.net/uid-20620288-i ...
- LINUX 产生PPM 驱动例子
APP: //author:DriverMonkey //phone:13410905075 //mail:bookworepeng@Hotmail.com //qq:196568501 #inclu ...
- phpcms 05
所谓的异步加载,也就是两个线程同时执行一个任务,比如一个加载文字,一个加载图片,这样子可以先看到文字 footer,html包含尾部文件 {template "content",& ...
- Java提高篇---TreeMap
TreeMap的实现是红黑树算法的实现,所以要了解TreeMap就必须对红黑树有一定的了解,其实这篇博文的名字叫做:根据红黑树的算法来分析TreeMap的实现,但是为了与Java提高篇系列博文保持一致 ...
- EasyUI扩展方法
EasyUI扩展方法: 1.我想指定textarea的行,但editor:{type:'textarea', options: {rows:'4'}}这样写不行.请问大家怎么配置才是指定行的啊? 配置 ...
- Mysql-学习笔记(==》插入修改数据二)
USE db; -- 建立学生信息表CREATE TABLE student( sno INT UNSIGNED NOT NULL AUTO_INCREMENT, sname VARCHAR(20) ...
- ios kaifa
弹窗提示 { ////ios 7 弹窗 // UIAlertView *alert1=[[UIAlertView alloc] // initWithTitle:@"tishi" ...