【N-Queens】cpp
题目:
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的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- 【Same Tree】cpp
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
随机推荐
- leetcode 355 Design Twitte
题目连接 https://leetcode.com/problems/design-twitter Design Twitte Description Design a simplified vers ...
- MeshLab中插件的添加过程
MeshLab中主要插件类型有 filter plugins, i/o plugins, edit plugins,这些插件实现了MeshLab的大部分功能.新加入的插件命名规则最好也遵循规范,可命名 ...
- centos7 源码编译nginx
使用configure命令配置编译.它定义了系统的各个方面,包括允许使用 nginx 进行连接处理的方法.最后它创建一个Makefile.该configure命令支持以下参数: --prefix=pa ...
- 时序js插件cubism使用
document http://iwantmyreal.name/blog/2012/09/16/visualising-conair-data-with-cubism-dot-js https:// ...
- 如何在InstallShield的MSI工程中调用Merge Module的Custom Action
使用InstallShield创建了合并模块安装程序,定义自定义活动,可如何调用却不太清楚,网上也就找到这点信息,还是没有成功,到底该在什么地方执行合并模块的自定义活动? http://1662487 ...
- 完全卸载TeamViewer与重新安装TeamViewer 7(含单文件版V12主控端)
卸载teamviewer: 删除:%AppData%\Teamviewer.%tmp%\TeamViewer.C:\Users\Administrator\AppData\Local\TeamView ...
- 《剑指offer》【调整数组顺序使奇数位于偶数前面】(python版)
题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分 思路: 我认真看了一下,题目应该是要求在原地调整,所以这里不能再 ...
- windows平台下MongoDB安装和环境搭建
下载安装包或者压缩包 添加db存储和日志存储文件夹 添加服务.配置环境变量.启动Mongo 本例:安装路径:D:Program Files/MongoDB 配置文件的路径:D:MongoDB 一.安装 ...
- JS位运算和遍历
JS位运算符 整数 有符号整数:允许使用正数和负数,第32位作为符号位,前31位才是存储位 无符号整数:只允许用正数 如果用n代表位 位数 = 2^n-1 由于位数(1.2.4.8.16...)中只有 ...
- ORTP-0.27.0移植
注意: a. 对于0.27一下版本的ORTP的交叉编译则没有一下依赖库 b. 交叉编译工具链是: arm-linux-gnueabihf-gcc-4.9.1 (4.9版本一下的编译bctoolbox出 ...