dfs 深度优先搜索
在C++中dfs是指对于某一个节点,进行拓展时可能有若干种方式,以深度为第一优先级进行遍历的方式。
例1
给出 和序列 ,在 中选择若干个数总和等于 。
#include<bits/stdc++.h>
using namespace std;//总和等于k
int ans=0;
int m,k;//s->选了的数字和
void dfs(int n,int s){//n->现在选到第几个数
//cout<<n<<" "<<s<<endl;
if(n>m){
if(s==k){
ans++;
}
return;
}
//选:
s+=n;
dfs(n+1,s);
//不选:
s-=n;
dfs(n+1,s);
}
int main(){
cin>>m>>k;
dfs(1,0);
cout<<ans;
}例2
给出 以及序列 {} 在其中选 个数字,使得这 个数字的和不超过 ,计算有多少种方式。
#include<bits/stdc++.h>
using namespace std;//总和小于等于w
const int N=1000050;
int a[N];//c->表示已经选了几个数了
int ans=0;//s->选的n个数的总和
int k,w;//n->选了几个数
void dfs(int n,int c,int s){
//cout<<n<<" "<<s<<endl;
if(n>k){
if(s<=w){
ans++;
}
return;
}
//选:
s+=a[n];
c++;
dfs(n+1,c,s);
//不选:
s-=a[n];
c--;
dfs(n+1,c,s);
}
int main(){
int n;
cin>>n>>w>>k;
for(int i=1;i<=n;i++) cin>>a[i];
dfs(1,0,0);
cout<<ans;
}例3
给出长度分别为 的两个序列 ,要求从中分别选出 个数字,使得相等,求有几种方案数
样例:
in: out:
5 3 2 7
1 2 3 4 5
2 4 5#include<bits/stdc++.h>
using namespace std;
const int N=1000050;
int A[N],B[N];
int ans=0;
int n,m,k; //A序列选到第 a 个数了,B序列选到第 b 个数了。
//A序列现在和为 s1 ,B序列现在和为 s2
//A序列选了 c1 个数了,B序列选出 c2 个数了
void dfs(int a,int b,int c1,int s1,int s2,int c2){
//cout<<a<<" "<<b<<" "<<s1<<" "<<s2<<endl;
if(c1>k or c2>k or a>n or b>m){
if(s1==s2){
ans++;
//cout<<" √"<<endl;
}
return;
}
//A选B选
s1+=A[a];
a++;
s2+=B[b];
b++;
dfs(a,b,c1+1,s1,s2,c2+1);
//A选B不选
b--;
s2-=B[b];
dfs(a,b+1,c1+1,s1,s2,c2);
//A不选B不选
a--;
s1-=A[a];
dfs(a+1,b+1,c1,s1,s2,c2);
//A不选B选
s2+=B[b];
b++;
dfs(a+1,b,c1,s1,s2,c2+1);
}
int main(){
cin>>n>>m>>k;
for(int i=1;i<=n;i++) cin>>A[i];
for(int i=1;i<=m;i++) cin>>B[i];
dfs(1,1,0,0,0,0);
cout<<ans;
}
例4
给出 个数字和长度为 的序列 ,要求从 中选出若干个数,满足选出的数中,任意三个数作边长不可以构成三角形。
样例:
in: out:
5 8
9 1 3 7 5#include<bits/stdc++.h>
using namespace std;
const int N=1000050;
int a[N];
//vector<int> v->已经被选的数
//s已经选了几个数
int ans=0;
bool check(int a,int b,int c){
if(a+b>c and b+c>a and a+c>b){
return true;
}
return false;
}
int n;
vector<int> v;
void dfs(int s,vector<int> v){
if(s>n){
if(v.size()>=3){
for(int i=0;i<v.size();i++){
for(int j=i+1;j<v.size();j++){
for(int k=j+1;k<v.size();k++){
if(check(v[i],v[j],v[k])==true){
return;
}
}
}
}
ans++;
}
return;
}
//选
v.push_back(a[s]);
dfs(s+1,v);
//不选
v.pop_back();
dfs(s+1,v);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
dfs(1,v);
cout<<ans;
}
例5
有一张地图用 的矩阵表示, 表示海洋, 表示陆地,求有多少片大洋与大陆。
主体思想
记地图为 ,将每一块大洋或大陆同时标记,将对应的变量增加。当 dfs 传入 和 数据时,先标记 再判断上,下,左,右方向的点,如果符合要求,继续调用 dfs 函数
样例:
in: out:
3 5 1 3
OO###
#OOOO
OOO###include<bits/stdc++.h>
using namespace std;
int A[8][8];
bool B[8][8];
int n,m;
void dfs(int x,int y) {
B[x][y]=1;
if(A[x-1][y]==A[x][y] and B[x-1][y]==false){
dfs(x-1,y);
}
if(A[x+1][y]==A[x][y] and B[x+1][y]==false){
dfs(x+1,y);
}
if(A[x][y-1]==A[x][y] and B[x][y-1]==false){
dfs(x,y-1);
}
if(A[x][y+1]==A[x][y] and B[x][y+1]==false){
dfs(x,y+1);
}
}
int main() {
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char c;
cin>>c;
if(c=='O'){
A[i][j]=1;
}else{
A[i][j]=2;
}
}
}
int sea=0,land=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(B[i][j]==false){
if(A[i][j]==1){
sea++;
}else{
land++;
}
dfs(i,j);
}
}
}
cout<<sea<<" "<<land;
} 例5拓展
在例5的基础上,输出正确答案并输出最大一片大洋的大小。
主题思想
在原代码的基础上增加变量 flg(当前判断的是否是大洋),size (当前判断的大洋的大小) ,maxsea(记录最后的答案)。
在定义变量时加入
int flg,size;
int maxsea=INT_MIN;在dfs函数开头加入
if(flg==1){
size++;
}在调用 dfs 函数的后面加入
if(A[i][j]==1){
maxsea=max(maxsea,size);
}最后输出加入
cout<<maxsea<<endl;