N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

 
Example

There exist two distinct solutions to the 4-queens puzzle:

[

[".Q..", // Solution 1

"...Q",

"Q...",

"..Q."],

["..Q.", // Solution 2

"Q...",

"...Q",

".Q.."]

]

 class Solution {
public:
/**
* Get all distinct N-Queen solutions
* @param n: The number of queens
* @return: All distinct solutions
* For example, A string '...Q' shows a queen on forth position
*/
bool isOK(vector<string> &v, int n, int x, int y) {
for (int i = ; i < x; ++i)
if (v[i][y] == 'Q') return false;
for (int i = ; x - i >= && y - i >= ; ++i)
if (v[x-i][y-i] == 'Q') return false;
for (int i = ; x - i >= && y + i < n; ++i)
if (v[x-i][y+i] == 'Q') return false;
return true;
}
void dfs(vector<vector<string>> &res, vector<string> &v, int n, int idx) {
if (idx == n) {
res.push_back(v);
return;
}
for (int i = ; i < n; ++i) {
v[idx][i] = 'Q';
if (isOK(v, n, idx, i)) dfs(res, v, n, idx + );
v[idx][i] = '.';
}
}
vector<vector<string> > solveNQueens(int n) {
// write your code here
vector<vector<string>> res;
string s;
for (int i = ; i < n; ++i) s.push_back('.');
vector<string> v(n, s);
dfs(res, v, n, );
return res;
}
};

[LintCode] N-Queens的更多相关文章

  1. lintcode 中等题:N Queens II N皇后问题 II

    题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...

  2. lintcode 中等题:N Queens N皇后问题

    题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...

  3. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  4. 【LeetCode/LintCode】丨Google面试题:N皇后问题

    n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击(任意两个皇后不能位于同一行,同一列,同一斜线). 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案包含一个明确的 ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  7. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  8. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  9. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  10. Lintcode 175. 翻转二叉树

    -------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...

随机推荐

  1. OpenERP7测试手记之 - EMail配置 转

    转自http://blog.sina.com.cn/s/blog_6d5929a00101b74y.html 在OpenERP中进行Email配置要注意以下几点: 1.如下面两个图,公司的“电子邮件” ...

  2. Linux内核中的软中断、tasklet和工作队列具体解释

    [TOC] 本文基于Linux2.6.32内核版本号. 引言 软中断.tasklet和工作队列并非Linux内核中一直存在的机制,而是由更早版本号的内核中的"下半部"(bottom ...

  3. 3299 有序数组合并求第K大问题

    题目描述 Description 给出两个有序数组A和B(从小到大有序),合并两个有序数组后新数组c也有序,询问c数组中第k大的数 假设不计入输入输出复杂度,你能否给出一个O(logN)的方法? 输入 ...

  4. 使用Adobe Audition 处理声音步骤

      软件: Adobe Audition 3.0 处理声音 插件:单独安装各种DirectX音效处理插件    一.录音 * 录音笔.手机 * Adobe Audition专业的录音软件   二..润 ...

  5. iOS手势的综合运用

    //自定义一个VIEW封装手势功能 // CustormView.m // gesterDemoo // // Created by ganchaobo on 13-7-13. // Copyrigh ...

  6. Ubuntu远程桌面,如何退出全屏

    首先安装Linux 下远程桌面客户端软件-rdesktop 打开终端 执行sudo apt-get install rdesktop 远程连接XP 系统(前提是windows xp 必须打开并且允许远 ...

  7. HDUOJ-------单词数

    单词数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. Java实现多线程的四种实现方式

    以计算0到1000之间的和为例 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; impo ...

  9. maven Missing artifact com.sun:tools:jar:1.5.0

    转自:http://blog.csdn.net/caolaosanahnu/article/details/7918929 http://zuoshahao.com/work/others/missi ...

  10. POJ 1486 Sorting Slides (KM)

    Sorting Slides Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2831   Accepted: 1076 De ...