题目:

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.

For 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:
static vector<vector<string> > solveNQueens(int n)
{
vector<vector<string> > ret;
if ( n== ) return ret;
vector<string> tmp;
vector<bool> colUsed(n,false);
vector<bool> diagUsed1(*n-,false);
vector<bool> diagUsed2(*n-,false);
Solution::dfs(ret, tmp, n, colUsed, diagUsed1, diagUsed2);
return ret;
}
static void dfs(
vector<vector<string> >& ret,
vector<string>& tmp,
int n,
vector<bool>& colUsed,
vector<bool>& diagUsed1,
vector<bool>& diagUsed2 )
{
const int row = tmp.size();
if ( row==n )
{
ret.push_back(tmp);
return;
}
string curr(n,'.');
for ( size_t col = ; col<n; ++col )
{
if ( !colUsed[col] && !diagUsed1[col+n--row] && !diagUsed2[col+row] )
{
colUsed[col] = !colUsed[col];
diagUsed1[col+n--row] = !diagUsed1[col+n--row];
diagUsed2[col+row] = !diagUsed2[col+row];
curr[col] = 'Q';
tmp.push_back(curr);
Solution::dfs(ret, tmp, n, colUsed, diagUsed1, diagUsed2);
tmp.pop_back();
curr[col] = '.';
diagUsed2[col+row] = !diagUsed2[col+row];
diagUsed1[col+n--row] = !diagUsed1[col+n--row];
colUsed[col] = !colUsed[col];
}
}
}
};

tips:

深搜写法:

1. 找到一个解的条件是tmp的长度等于n

2. 在一列中遍历每个位置,是否能够放置Q,并继续dfs;返回结果后,回溯tmp之前的状态,继续dfs。

一开始遗漏了对角线也不能在有超过一个Q的条件,补上之后就AC了。

========================================

第二次过这道题,string(n, '.')一直写成了string('.', n),改过来就AC了。

class Solution {
public:
vector<vector<string> > solveNQueens(int n)
{
vector<vector<string> > ret;
if ( n< ) return ret;
vector<string> tmp;
vector<bool> colUsed(n, false);
vector<bool> r2l(*n-, false);
vector<bool> l2r(*n-, false);
Solution::dfs(ret, tmp, n, colUsed, r2l, l2r);
return ret;
}
static void dfs(
vector<vector<string> >& ret,
vector<string>& tmp,
int n,
vector<bool>& colUsed,
vector<bool>& r2l,
vector<bool>& l2r)
{
if ( tmp.size()==n )
{
ret.push_back(tmp);
return;
}
int r = tmp.size();
string row(n, '.');
for ( int i=; i<n; ++i )
{
if ( colUsed[i] || r2l[i-r+n-] || l2r[i+r] ) continue;
colUsed[i] = true;
r2l[i-r+n-] = true;
l2r[i+r] = true;
row[i] = 'Q';
tmp.push_back(row);
Solution::dfs(ret, tmp, n, colUsed, r2l, l2r);
tmp.pop_back();
row[i] = '.';
colUsed[i] = false;
r2l[i-r+n-] = false;
l2r[i+r] = false;
}
}
};

【N-Queens】cpp的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  6. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  8. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  10. 【Same Tree】cpp

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

随机推荐

  1. /pentest/cisco/cisco-auditing-tool

    /pentest/cisco/cisco-auditing-tool ./CAT    -h host 扫描单个主机 -w  wordlist 猜测团体名称列表 -a passlist   猜测密码列 ...

  2. 不该被忽视的CoreJava细节(一)

    一.系列文章导言 <不该被忽视的CoreJava细节>系列文章将会持续更新.我希望自己通过这一系列文章的写作,能与读者一起进步,逐步完善对Java体系结构的了解. 二.本期关注点 几乎翻看 ...

  3. ADS主要仿真器介绍

    ADS主要仿真器介绍        ADS ( Advanced Design System ) 是美国Agilent公司推出的电路和系统分析软件,它集成多种仿真软件的优点,仿真手段丰富多样,可实现包 ...

  4. C基础的练习集及测试答案(40-50)

    40.(课堂)打印杨辉三角型前10行 #if 0 40.(课堂)打印杨辉三角型前10行 思路分析: 一.打印十行杨辉三角得第十行长度为十,所以建立一个长度为十的数组,作为每行的数据存储 二.按 0-9 ...

  5. linux 命令——28 tar

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大.tar命令可以为linux的 ...

  6. IOS 移除栈顶的控制器

    - (IBAction)back2Two:(id)sender { // 移除当前栈顶的控制器 [self.navigationController popViewControllerAnimated ...

  7. World Wind Java开发之八——加载本地缓存文件构建大范围三维场景(

    http://blog.csdn.net/giser_whu/article/details/42044599 上一篇博客主要是针对小文件直接导入WW中显示,然而当文件特别大时,这种方式就不太可行.因 ...

  8. 【洛谷2522】[HAOI2011] Problem b(莫比乌斯反演)

    点此看题面 大致题意: 求\(\sum_{x=a}^b\sum_{y=c}^d[gcd(x,y)==k]\). 关于另一道题目 在看这篇博客之前,如果你做过一道叫做[BZOJ1101][POI2007 ...

  9. Portal简介

    Portal 在英语中是入口的意思.Portal 认证通常也称为 Web 认证,一般将 Portal 认 证网站称为门户网站. 未认证用户上网时,设备强制用户登录到特定站点,用户可以免费访问其中的服务 ...

  10. 文件系统 - Linux 支持的文件系统类型

    NAME 文件系统 - Linux 支持的文件系统类型:minix, ext, ext2, xia, msdos, umsdos, vfat, proc, nfs, iso9660, hpfs, sy ...