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. 八大排序算法之六--交换排序—快速排序(Quick Sort)

    基本思想: 1)选择一个基准元素,通常选择第一个元素或者最后一个元素, 2)通过一趟排序讲待排序的记录分割成独立的两部分,其中一部分记录的元素值均比基准元素值小.另一部分记录的 元素值比基准值大. 3 ...

  2. 【转】Eclipse Console 加大显示的行数,禁止弹出

    转载地址:http://blog.csdn.net/leidengyan/article/details/5686691 Eclipse Console 加大显示的行数: 在 Preferences- ...

  3. API判断网站IP地址,国家区域

    直接访问http://api.wipmania.com/jsonp 还有经纬度

  4. ant-环境变量

    ant是绿色免安装的,通常习惯放在C盘根目录下,如:C:\apache-ant-1.9.4 那么环境就应该配置为: ANT_HOME=C:\apache-ant-1.9.4(注意:这里不需要加分号)P ...

  5. JSch - Java实现的SFTP(文件上传详解篇) [转载]

    文章来源:http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html JSch是Java Secure Channel的缩写.JSch是 ...

  6. 20151210001 DataGridView 选中与被选中

    // DataSet_IP list        private void DataSet_IP_list()        {            DataSet_IP = new System ...

  7. response.sendRedirect()重新定向的乱码问题

    这里response.sendRedirect("YPbianhaoModify.jsp?jinhuoshang="+jinhuoshang+"&jinhuori ...

  8. 安卓开发之json解析

    1.从网页获取json返回字符串 public class ReadNet extends AsyncTask<URL, Integer, String> { @Override      ...

  9. 第六课 SQLite

    总结:SQLite 1.SQLite的数据类型: NULL(空值).INTEGER(整型值).READL(浮点值).TEXT(字符串值).BLOB(二进制对象); 2.SQLite的应用 2.1 SQ ...

  10. Centos6.6上安装mysql5.6中的一些典型问题

    经过两天的摸索,终于成功在CentOS6.6系统上成功安装了mysql5.6,现整理如下. (1)安装时的问题: 最小化安装后,安装rpm包时经常会遇到 linux/centos Header V3 ...