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题目是这样的 ...
随机推荐
- 请求php返回json生成自定义对象
php代码 public function convert_array(){ $arr = array( '0'=>array('name'=>'zc','height'=>173) ...
- HttpClient的使用方法
使用httpClient发送请求.接收响应很简单.一般需要以下几个步骤. 第一:创建HttpClient对象: 第二:创建请求方法的实例,并指定请求URL.如果要发送GET请求,创建HttpGet对象 ...
- A simple problem 分类: 哈希 HDU 2015-08-06 08:06 1人阅读 评论(0) 收藏
A simple problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- JAVA基础知识之JVM-——通过反射查看类信息
Class实例 当类被加载之后,JVM中就会生成一个Class实例,通过这个实例就可以访问JVM中的这个类.有三种方式可以获取Class对象 使用Class的静态方法forName(完整包名) 调用类 ...
- Poj(1469),二分图最大匹配
题目链接:http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- Android开发之获取本地视频和获取自拍视频
1.获取本地所有视频 public void getLoadMedia() { Cursor cursor = UILApplication.instance.getApplicationContex ...
- 25-React事件处理及条件渲染
Handling Events React元素的事件处理非常类似于对DOM元素添加事件处理,但有一部分的语法不同: 1.React事件使用camelCase(驼峰命名法)来进行命名,而不是小写字母 2 ...
- crontab执行shell脚本
*/5 * * * * cd /data/**/ && ./*.sh * * * * * /bin/sh /home/*.sh
- SqlSever基础 select cast 将一个int类型数据转换为char
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- request请求对象实例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoRequest.as ...