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的更多相关文章

  1. lintcode 中等题:N Queens II N皇后问题 II

    题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...

  2. [Leetcode] n queens ii n皇后问题

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [Leetcode][Python]52: N-Queens II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...

  9. 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 ...

  10. [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...

随机推荐

  1. freeMarker中list的两列展示

    前台界面中我使用freeMarker的机会有很多,自然也就会接触下<List>标签,我想大家应该都不陌生.<#list attrList as attr>${a.name}&l ...

  2. java初学。加载图片

    public class GameFrame extends Frame{ private static final int WIDTH=900; private static final int H ...

  3. nn package

    1.nn模块是神经网络模块 2.父类module,子类Sequential, Parallel和Concat 3.Linear:做线性变换 4.criterion 这个模块包含了各式各样的训练时的损失 ...

  4. php修改和增加xml结点属性

    <?xml version="1.0" encoding="UTF-8" ?> <clientSet> <server url=& ...

  5. ios布局约束

    http://blog.csdn.net/pucker/article/details/41843511 除了使用Pin菜单之外,也可以直接在文档结构窗口中或者设计器中通过拖拽建立约束.在文档结构窗口 ...

  6. flume+hadoop

    摘自:http://www.kankanews.com/ICkengine/archives/130646.shtml

  7. Java动态代理 cglib

    代理模式:为某些对象提供代理以实现对这个对象的访问. 对一个对象进行访问控制的原因是为了只有在我们确实需要这个对象时才对它进行创建和初始化. 一般包括以下组件: 被代理者接口:提供被代理者的访问途径. ...

  8. 【转】Ajax中send方法参数的使用(get/post)

    Ajax中send方法参数的使用 一般情况下,使用Ajax提交的参数多是些简单的字符串,可以直接使用GET方法将要提交的参数写到open方法的url参数中,此时send方法的参数为null. 例如 : ...

  9. UVA 10534 三 Wavio Sequence

    Wavio Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  10. MVC部署IIS设置

    根目录→处理程序映射→添加通配符脚本映射 C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll