标签:can size 表示 搜索结果 osd public 个数 oid vector
一. 八皇后问题
第一种解法将棋盘的所有格子都初始化为‘.’, 定义递归函数为前l-1行的格子已经排好(给定排面的情况下), 从第l层开始继续排得到的八皇后搜索结果。具体做法是从第l行的每一个列逐列尝试,如果不冲突则加入,再进行l+1的问题求解,求解完后进行回溯。空间复杂度为O(N*N)
class Solution {
public:
int totalNQueens(int n) {
vector> round(n, vector(n, 0));
int total = 0;
btsearch(round, 0, n, total);
return total;
}
private:
void btsearch(vector> &round, int t, int n, int &total) {
if (t == n) {
total++;
return;
}
for (int i = 0; i ) {
if (isok(round, t, i)) {
round[t][i] = 1;
btsearch(round, t+1, n, total);
round[t][i] = 0;
}
}
}
bool isok(const vector>round, int t, int j) {
int n = round.size();
for (int c=0; c) {
if (round[c][j] == 1) return false;
}
for (int r=0; r) {
if (round[t][r] == 1) return false;
}
// 45 o
for (int c=t-1, r=j+1; c>=0 && r) {
if (round[c][r] == 1) return false;
}
// 135 o
for (int c=t-1, r=j-1; c>=0 && r>=0; c--, r-- ) {
if (round[c][r] == 1) return false;
}
// nqueque = 4。
return true;
}
};
class Solution {
public:
int totalNQueens(int n) {
vector> round(n, vector(n, 0));
int total = 0;
btsearch(round, 0, n, total);
return total;
}
private:
void btsearch(vector> &round, int t, int n, int &total) {
if (t == n) {
total++;
return;
}
for (int i = 0; i ) {
if (isok(round, t, i)) {
round[t][i] = 1;
btsearch(round, t+1, n, total);
round[t][i] = 0;
}
}
}
bool isok(const vector>round, int t, int j) {
int n = round.size();
for (int c=0; c) {
if (round[c][j] == 1) return false;
}
for (int r=0; r) {
if (round[t][r] == 1) return false;
}
第二种方法类似,不过可以将格子表示成一个一维数组,第i个元素的值j表示将皇后放在第i行第j列
二. Permutation
对一个数组进行全排列,无重复元素,定义递归函数为前i-1个元素全排列已经排好, 将第i个元素以及后面的元素进行全排列。过程为从第i个元素到最后一个元素轮流放在第i个位置上, 然后对第i+1个元素以及后续元素进行全排列。
class Solution {
public:
vector> permute(vector& nums) {
vector> res;
permutehelper(nums, 0, res);
return res;
}
private:
void permutehelper(vector&nums, int l, vector> &res){
if (l == nums.size()) {
res.push_back(nums);}
for (int i = l; i ) {
swap(nums[l], nums[i]);
permutehelper(nums, l+1, res);
swap(nums[i], nums[l]);
}
}
};
若有重复,则先进行sort, 并且轮流当头那部分相同的数字只能出现一次,
class Solution {
public:
vector> permuteUnique(vector& nums) {
vector> res;
sort(nums.begin(), nums.end());
helper(nums, 0, res);
return res;
}
private:
void helper(vector& nums, int l, vector> &res) {
if (l == nums.size()) res.push_back(nums);
set vis;
for (int i=l; i) {
if (vis.count(nums[i])) continue;
vis.insert(nums[i]);
swap(nums[l], nums[i]);
helper(nums, l+1, res);
swap(nums[i], nums[l]);
}
}
};
三. 整数拆分
四. CombineSum
class Solution {
public:
vector> combinationSum(vector& candidates, int target) {
vector> res;
vector out;
btsearch(candidates, 0, target, out, res);
return res;
}
void btsearch(vector candidates, int l, int target, vector &out, vector>&res) {
if (target ) {
return;
}
if (target == 0) {
res.push_back(out);
return;
}
for (int i = l ; i) {
out.push_back(candidates[i]);
btsearch(candidates, i, target - candidates[i], out, res);
out.pop_back();
}
}
};
五. CombineSumII
class Solution {
public:
vector > combinationSum2(vector &num, int target) {
vector > res;
vector out;
sort(num.begin(), num.end());
combinationSum2DFS(num, target, 0, out, res);
return res;
}
void combinationSum2DFS(vector &num, int target, int start, vector &out, vector > &res) {
if (target ;
else if (target == 0) res.push_back(out);
else {
for (int i = start; i i) {
if (i > start && num[i] == num[i - 1]) continue;
out.push_back(num[i]);
combinationSum2DFS(num, target - num[i], i + 1, out, res);
out.pop_back();
}
}
}
};
六. subsets
https://www.cnblogs.com/TenosDoIt/p/3451902.html
每一个元素有选择放或者不放两种选择。
class Solution {
public:
vector> subsets(vector& nums) {
vector> res;
vector out;
helper(nums, 0, out, res);
return res;
}
private:
void helper(vectornums, int l, vector&out, vector>&res) {
if (l == nums.size()) {
res.push_back(out);
return;
}
out.push_back(nums[l]);
helper(nums, l+1, out, res);
out.pop_back();
helper(nums, l+1, out, res);
}
};
算法整理-回溯和DFS
标签:can size 表示 搜索结果 osd public 个数 oid vector
原文地址:https://www.cnblogs.com/cookcoder-mr/p/11080053.html