【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 ...
随机推荐
- cms-后台eazyui搭建
1.引入eazyUi需要的js 2.布局:上.下.左.中 <%@ page language="java" contentType="text/html; char ...
- LeetCode Merge Sorted Array 合并已排序的数组
void merge(int A[], int m, int B[], int n) { int *a=A,*b=B; ,j=; ||m==){ //针对特殊情况,比如A或B中无元素的情况 & ...
- HDU 2955 Robberies抢劫案(01背包,变形)
题意:要抢劫,但是抢每个银行都有被抓的概率,问在低于规定的被抓概率情况下最多能抢到多少钱. 输入:第一行为T,表示共T个测试例子.每个例子的第一行给出一个浮点数P,是规定被抓的概率上限.第一行还有一个 ...
- windows下php7.1.5、mysql环境搭建
php http://windows.php.net/download/ 如果是使用ISAPI的方式来运行PHP就必须用Thread Safe(线程安全)的版本:而用FastCGI模式运行PHP的话就 ...
- vue+node+mongodb实现的页面
源代码地址:https://github.com/GainLoss/vue-node-mongodb 目前这个项目实现的是: 1.利用vue-cli实现前台页面的编写 (1)页面的跳转利用的是vue- ...
- Javascript 向量
向量 既有大小又有方向的量叫做向量(亦称矢量),与标量相对,用JS实现代码如下,直接搬miloyip的了 Vector2 = function(x, y) { this.x = x; this.y = ...
- angular6项目中使用echarts图表的方法(有一个坑,引用报错)
1.安装相关依赖(采用的webpack) npm install ecahrts --save npm install ngx-echarts --save 2.angular.json 配置echa ...
- 使用Scanner类获取键盘输入的会员卡号,并将该数据存储在变量中,输出这个变量的信息
package come.one01;import java.util.Scanner; // 导入Scanner类public class One03 { public static void ma ...
- 题解 P1280 【尼克的任务】
传送门 f[i]表示i~n的最长空闲时间: 如果当前无任务就休息一秒(f[i]=f[i+1]+1): 否则f[i]=max(f[i],f[i+当前工作时间]); 用结构体来记录,我们对于每一个时刻开一 ...
- 架构图(拓扑图)画图工具分析整理(静态,动态,可交互图.层级tu)
最近要画架构图. 一方面有图片洁癖,另外一方面又不想不停的挪动图片. 一开始想用脑图软件. 发现脑图是树状的,架构模块依赖图是网状的.(也可以简化为层级图,不画交互关系.类似 dubbo 的架构图. ...