双向广搜常见用途:
1.bfs剪枝策略,分两侧展开分支,哪侧数量少就从哪侧展开
2.解决不允许全部样本递归展开,但半量样本可以完全展开的问题:把数据分成两部分,每部分各自展开计算结果,然后整合两部分统计答案(其实叫双向搜索更合适)
class Solution {
public:
int ladderLength(string beginWord, string endWord,
vector<string>& wordList) {
unordered_set<string> dic(wordList.begin(), wordList.end());
if (dic.find(endWord) == dic.end())
return 0;
unordered_set<string> small;//小端字符串
unordered_set<string> big;//大端字符串
unordered_set<string> next;//下一层字符串
int level = 2;
small.insert(beginWord);
big.insert(endWord);
while (!small.empty()) {
for (auto str : small) {
for (int i = 0; i < str.size(); i++) {
string s = str;
for (char c = 'a'; c <= 'z'; c++) {
s[i] = c;
if (s != str) {
if (big.find(s) != big.end())//当小端的下一层字符串在大端发现时
return level; //说明就找到了
if(dic.find(s)!=dic.end()){
next.insert(s);
dic.erase(s);
}
}
}
}
}
level++;
if (next.size() <= big.size()) {//判断下一次哪边是大端和小端
small = next;
next.clear();
} else {
small = big;
big = next;
next.clear();
}
}
return 0;
}
};
双向广搜可以进行剪枝优化,传统bfs就是从begin开始直至找到end,其实我们可以从begin和end两端一起向中间找,哪端路径少就展开哪端,可以减少展开次数
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxnum = 1 << 20;
vector<long long>nums(41, 0);
int dfs(int i, int e, long long s, long long w, vector<long long>& ans, int j) {
if (s > w) return j;//超过限制没必要找下去了
if (i == e) ans[j++] = s;//统计答案
else {
j = dfs(i + 1, e, s, w, ans, j);//不选当前的数,j接收ans此时的下标
j = dfs(i + 1, e, s + nums[i], w, ans, j);//选当前的数,j接收下标
}
return j;//返回ans填到哪了
}
int main() {
long long n, m;
cin >> n >> m;
long long size = n;
for(int i=0;i<size;i++){
int a;
cin>>a;
nums[i]=a;
}
vector<long long>left1(maxnum, 0);
vector<long long>right1(maxnum, 0);
int lsize = dfs(0, size >> 1, 0, m, left1, 0);//左部分的大小
int rsize = dfs(size >> 1, size, 0, m, right1, 0);//右部分的大小
sort(left1.begin(), left1.begin() + lsize);//右部分排序
sort(right1.begin(), right1.begin() + rsize);//左部分排序
long long ans = 0;
for (int i = lsize - 1, j = 0; i >= 0; i--) {//双指针寻找满足题意的答案
while (j < rsize && left1[i] + right1[j] <= m)
j++;
ans += j;
}
cout << ans;
return 0;
}
根据这题的数据量,是不可能用动态规划开创那么大的背包的,但如果暴力的dfs一共有40个点,就会有2^40中可能,超过了10^7~10^8,所以暴力dfs会超时,这就需要双向搜索:将节点分两半,查询一半答案的有2^20种可能,没有超过10^7~10^8,最后将两部分合并统计答案即可
class Solution {
public:
static const int maxnum = 1 << 20;
int dfs(int i, int e, long s, int j, vector<int>& nums, vector<long>& rem) {
if (i == e)
rem[j++] = s;
else {
int k = i + 1;
while (k < e && nums[k] == nums[i])
k++;
for (int l = 0; l <= k - i; l++) {//剪枝,按选不同个数的相同数字展开
j = dfs(k, e, s + l * nums[i], j, nums, rem);
}
}
return j;
}
int minAbsDifference(vector<int>& nums, int goal) {
vector<long> left1(maxnum, 0);
vector<long> right1(maxnum, 0);
sort(nums.begin(), nums.end());
int n = nums.size();
int lsize = dfs(0, n >> 1, 0, 0, nums, left1);
int rsize = dfs(n >> 1, n, 0, 0, nums, right1);
sort(left1.begin(), left1.begin() + lsize);
sort(right1.begin(), right1.begin() + rsize);
int ans = abs(goal);
for (int i = 0, j = rsize - 1; i < lsize; i++) {//从距离的角度理解双指针移动
while (j > 0 && abs(left1[i] + right1[j - 1] - goal) <=
abs(left1[i] + right1[j] - goal))
j--;
int r = abs(left1[i] + right1[j] - goal);
ans = min(ans, r);
}
return ans;
}
};
此题和"冰球赛"基本一致