1. Follow up for N-Queens problem.
  2.  
  3. Now, instead outputting board configurations, return the total number of distinct solutions.

  

  1. class Solution {
  2. public:
  3. bool isValid(int* row, int curRow)
  4. {
  5. for(int i = ; i< curRow; i++)
  6. if(row[i] == row[curRow] || abs(row[i] - row[curRow]) == curRow - i)
  7. return false;
  8. return true;
  9. }
  10. void nqueue(int* row,int curRow)
  11. {
  12. if(curRow == n)
  13. {
  14. res++;
  15. return ;
  16. }
  17. for(int i = ; i< n ;i++)
  18. {
  19. row[curRow] = i;
  20. if(isValid(row,curRow))
  21. nqueue(row,curRow+);
  22. }
  23. }
  24. int totalNQueens(int n) {
  25. // Start typing your C/C++ solution below
  26. // DO NOT write int main() function
  27. res = ;
  28. if( n < ) return res;
  29. this->n = n;
  30. int *row = new int[n];
  31. nqueue(row, );
  32. return res;
  33. }
  34. private:
  35. int n;
  36. int res;
  37. };

LeetCode_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. 面试题 41 和为s的两个数字VS 和为S的连续整数序列

    (1)和为S的两个数字 bool findNumberWithSum(int data[], int length, int sum, int &numb1, int &numb2){ ...

  2. Windows Azure 存储管理器 (2014)

     Windows Azure存储用户经常希望能够在"管理器"中查看他们的数据,管理器指的是一款可用于显示存储帐户数据的工具.我们之前提供了我们所知的存储管理器列表.在本文中,我 ...

  3. MapReduce流程、如何统计任务数目以及Partitioner

    核心功能描述 应用程序通常会通过提供map和reduce来实现 Mapper和Reducer接口,它们组成作业的核心. Map是一类将输入记录集转换为中间格式记录集的独立任务. 这种转换的中间格式记录 ...

  4. euctb

  5. Android 之窗口小部件详解(三)  部分转载

    原文地址:http://blog.csdn.net/iefreer/article/details/4626274. (一) 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget ...

  6. ubuntu14.04 + cocos2d-x-2.2.6 + eclipse发布android + Qt Creator4

    先把需要的东西准备好,打开控制台,执行以下语句: sudo apt--jdk lib32z1 lib32ncurses5 lib32bz2- 接下来,准备好cocos2d-x-2.2.6和 andro ...

  7. css中的段落样式及背景

    一.段落样式 css中关于段落的样式主要有行高,缩进,段落对齐,文字间距,文字溢出,段落换行等.它们的具体语法如下: line-height : normal | length text-indent ...

  8. 用SNMP协议实现系统信息监控--Windows Server 2008

    简单了解: SNMP简单网络管理协议,是一种属于应有层的协议,主要有三个部分组成,被管理部分.代理部分和网络管理系统. 被管理部分是一个网络节点,也称为网络单元.SNMP代理是被管理设备上的一个网络管 ...

  9. python学习-day3

    今天是第三天学习,加油! 第一部分 集合 一.集合 1.什么是集合以及特性? 特性:无序的,不重复的序列,可嵌套. 2.创建集合 方法一:创建空集合 s1 = set() print(type(s1) ...

  10. (转)第一天 XHTML CSS基础知识 文章出处:标准之路(http://www.aa25.cn/div_css/902.shtml)

    欢迎大家学习<十天学会web标准>,也就是我们常说的DIV+CSS.不过这里的DIV+CSS是一种错误的叫法,建议大家还是称之为web标准. 学习本系列教程需有一定html和css基础,也 ...