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. 杭电1009-FatMouse' Trade

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...

  3. SlickGrid example 1: 最简单的例子和用法

    SlickGrid例子和用法 开始学习使用SlickGrid,确实挺好用,挺方便的. 官网地址: https://github.com/mleibman/SlickGrid 不多说,先上效果图. 上代 ...

  4. c# 回调委托

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 2016 ACM/ICPC Asia Regional Qingdao Online HDU5889

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5889 解法:http://blog.csdn.net/u013532224/article/details ...

  6. python函数应用

    函数 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编 ...

  7. zookeeper系列之七—从远程调用认识zookeeper

    http://www.csdn.net/article/2014-01-02/2817944-zookeeper 在Hadoop的学习过程中,Zookeeper是让很多初学者困惑的技术,远程调用服务是 ...

  8. 【Web】写个HTML页面去调试HTTP接口方便些

    现在越来越多的系统基本SOA的思想,业务由许多小系统通过远程调用的方式串连起来,其中HTTP的接口在远程调用的方式中颇为常见.看过一些开发人员写完一些接口后,要么按照正常情况调用几次就交给调用方,要么 ...

  9. FragMent 初级学习

    FragMent 可以用在一个Activity中也可以用在多个Activity中 我们在activity中加载FragMent的时候,首先添加一个myFragMent的类 通过onCreateView ...

  10. DevExpress中使用DocumentManager,并确保不重复

    https://www.devexpress.com/Support/Center/Question/Details/Q411356 void ActivatForm(Type someType) { ...