【LeetCode】52. N-Queens II
N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

简单的回溯法。
cur[i] == j 代表第i行的皇后位于第j列。
对于每一行,尝试所有列,
如果当前行尝试的位置不与前面所有行的位置冲突,则可以递归到下一行。
class Solution {
public:
    int totalNQueens(int n) {
        int count = ;
        vector<int> cur;
        Helper(count, cur, , n);
        return count;
    }
    void Helper(int& count, vector<int> cur, int pos, int n)
    {
        if(pos == n)
            count ++;
        else
        {
            for(int i = ; i < n; i ++)
            {
                cur.push_back(i);
                if(check(cur))
                    Helper(count, cur, pos+, n);
                cur.pop_back();
            }
        }
    }
    bool check(vector<int> cur)
    {
        int size = cur.size();
        int loc = cur[size-];
        for(int i = ; i < size-; i ++)
        {
            //never same row
            //col
            if(cur[i] == loc)
                return false;
            //diag
            if(abs(cur[i]-loc) == abs(i-size+))
                return false;
        }
        return true;
    }
};

【LeetCode】52. N-Queens II的更多相关文章
- 【LeetCode】Pascal's Triangle II 解题报告
		
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
 - 【LeetCode】731. My Calendar II 解题报告(Python)
		
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
 - 【LeetCode】137. Single Number II 解题报告(Python)
		
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
 - 【LeetCode】227. Basic Calculator II 解题报告(Python)
		
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
 - 【LeetCode】113. Path Sum II 解题报告(Python)
		
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
 - 【Leetcode】Linked List Cycle II
		
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
 - 【LeetCode】52. N-Queens II(位运算)
		
[题意] 输出N皇后问题的解法个数. [题解] 解法一:传统dfs回溯,模拟Q放置的位置即可,应该不难,虽然能通过,但是时间复杂度很高. 解法二:位运算大法好! 首先要明白这道题里两个核心的位运算 1 ...
 - 【LeetCode】Single Number I & II & III
		
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
 - 【LeetCode】167. Two Sum II - Input array is sorted
		
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
 - 【LeetCode】680. Valid Palindrome II
		
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...
 
随机推荐
- javascript和python取dict字典对象的不同
			
dict1={"a":1,"b":2,"22a":44} JS: dict1.a 和 dict1["a"]都可以 pyt ...
 - perfect-rectangle
			
https://leetcode.com/problems/perfect-rectangle/ // https://discuss.leetcode.com/topic/55944/o-n-log ...
 - 用VLC读取摄像头产生RTSP流,DSS主动取流转发(一)(二) 【转】
			
http://blog.csdn.net/fm0517/article/details/38110633 http://blog.csdn.net/fm0517/article/details/381 ...
 - WebView 加载网页 加载资源 总结 MD
			
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
 - C# WCF 完整实例,winform 窗体作为 宿主
			
上一次提到,我们的WCF程序宿主是发布到IIS上面的.虽然这样做未尝不可,不过不便于我们进行“开始”或“停止”WCF服务的操作.所以再次尝试了编写以窗体应用程序作为WCF服务宿主的方式,并取得了成功. ...
 - POJ 1265:Area
			
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4725 Accepted: 2135 Description ...
 - js数组对象深度复制
			
var deepCopy = function(o) { if (o instanceof Array) { var n = []; for (var i = 0; i < o.length; ...
 - 使用Python脚本批量裁切栅格
			
对栅格的裁切,我们通常使用裁切(数据管理-栅格-栅格处理)或按掩膜提取(空间分析-提取分析)来裁切,裁切的矢量要素通常是一个要素图层或Shape文件.如果要进行批量处理,可以使用ToolBox中的批量 ...
 - 在windows资源管理器添加进入当前目录dos窗口的快捷菜单
			
regedit.exe进入注册表 定位到HKEY_CLASSES_ROOT\Folder\shell 新建一项cmd,在cmd下再新建一项command,修改command的值为cmd.exe /k ...
 - uni - 条件渲染
			
vue官方文档和uni官方同步:https://cn.vuejs.org/v2/guide/conditional.html 1.多次切换建议使用v-show(始终保存在BOM) 2.因为if是惰性判 ...