LeetCode_N-Queens II
- Follow up for N-Queens problem.
- Now, instead outputting board configurations, return the total number of distinct solutions.
- class Solution {
- public:
- bool isValid(int* row, int curRow)
- {
- for(int i = ; i< curRow; i++)
- if(row[i] == row[curRow] || abs(row[i] - row[curRow]) == curRow - i)
- return false;
- return true;
- }
- void nqueue(int* row,int curRow)
- {
- if(curRow == n)
- {
- res++;
- return ;
- }
- for(int i = ; i< n ;i++)
- {
- row[curRow] = i;
- if(isValid(row,curRow))
- nqueue(row,curRow+);
- }
- }
- int totalNQueens(int n) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- res = ;
- if( n < ) return res;
- this->n = n;
- int *row = new int[n];
- nqueue(row, );
- return res;
- }
- private:
- int n;
- int res;
- };
LeetCode_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题目是这样的 ...
随机推荐
- 面试题 41 和为s的两个数字VS 和为S的连续整数序列
(1)和为S的两个数字 bool findNumberWithSum(int data[], int length, int sum, int &numb1, int &numb2){ ...
- Windows Azure 存储管理器 (2014)
Windows Azure存储用户经常希望能够在"管理器"中查看他们的数据,管理器指的是一款可用于显示存储帐户数据的工具.我们之前提供了我们所知的存储管理器列表.在本文中,我 ...
- MapReduce流程、如何统计任务数目以及Partitioner
核心功能描述 应用程序通常会通过提供map和reduce来实现 Mapper和Reducer接口,它们组成作业的核心. Map是一类将输入记录集转换为中间格式记录集的独立任务. 这种转换的中间格式记录 ...
- euctb
- Android 之窗口小部件详解(三) 部分转载
原文地址:http://blog.csdn.net/iefreer/article/details/4626274. (一) 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget ...
- ubuntu14.04 + cocos2d-x-2.2.6 + eclipse发布android + Qt Creator4
先把需要的东西准备好,打开控制台,执行以下语句: sudo apt--jdk lib32z1 lib32ncurses5 lib32bz2- 接下来,准备好cocos2d-x-2.2.6和 andro ...
- css中的段落样式及背景
一.段落样式 css中关于段落的样式主要有行高,缩进,段落对齐,文字间距,文字溢出,段落换行等.它们的具体语法如下: line-height : normal | length text-indent ...
- 用SNMP协议实现系统信息监控--Windows Server 2008
简单了解: SNMP简单网络管理协议,是一种属于应有层的协议,主要有三个部分组成,被管理部分.代理部分和网络管理系统. 被管理部分是一个网络节点,也称为网络单元.SNMP代理是被管理设备上的一个网络管 ...
- python学习-day3
今天是第三天学习,加油! 第一部分 集合 一.集合 1.什么是集合以及特性? 特性:无序的,不重复的序列,可嵌套. 2.创建集合 方法一:创建空集合 s1 = set() print(type(s1) ...
- (转)第一天 XHTML CSS基础知识 文章出处:标准之路(http://www.aa25.cn/div_css/902.shtml)
欢迎大家学习<十天学会web标准>,也就是我们常说的DIV+CSS.不过这里的DIV+CSS是一种错误的叫法,建议大家还是称之为web标准. 学习本系列教程需有一定html和css基础,也 ...