本文最后更新于 2025年3月20日 下午
P2089 烤鸡
https://www.luogu.com.cn/problem/p2089
题解
dfs搜索+剪枝优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include<bits/stdc++.h> using namespace std;
int n; int result[60000][10]; int path[10]; int res;
void dfs(int depth,int sum) { if(depth==10) { if(sum==n) { res++; for(int i=0;i<n;i++) { result[res][i]=path[i]; } } return; } for(int i=1;i<=3;i++) { if(sum+i+(9-depth)*1>n) continue; if(sum+i+(9-depth)*3<n) continue; path[depth]=i; dfs(depth+1,sum+i); } }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
cin>>n; dfs(0,0); cout<<res<<endl; for(int i=1;i<=res;i++) { for(int j=0;j<10;j++) { cout<<result[i][j]<<' '; } cout<<endl; }
return 0; }
|
P1618 三连击(升级版)
https://www.luogu.com.cn/problem/p1618
题解:
枚举
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| using namespace std;
int a,b,c,n1,n2,n3,flag,ans; int num[10];
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
cin>>a>>b>>c; for(int i=1;i<=1000/c;i++) { n1=i*a; n2=i*b; n3=i*c; for(int j=1;j<=3;j++) { num[n1%10]++; n1/=10; num[n2%10]++; n2/=10; num[n3%10]++; n3/=10; } for(int j=1;j<10;j++) { if(num[j]!=1) { flag=1; break; } } for(int j=1;j<10;j++) num[j]=0; if(!flag) { cout<<a*i<<' '<<b*i<<' '<<c*i<<endl; ans++; } else flag=0; } if(!ans) cout<<"No!!!";
return 0; }
|
P1036 选数
https://www.luogu.com.cn/problem/p1036
题解:
dfs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include<bits/stdc++.h> using namespace std;
int n,k; int arr[25]; int ans;
bool is_prime(int x) { if(x<2) return false; for(int i=2;i<=x/i;i++) if(x%i==0) return false; return true; }
void dfs(int now,int sum,int t) { if(now==k) { if(is_prime(sum)) ans++; return; }
for(int i=t;i<=n-(k-now-1);i++) { dfs(now+1,sum+arr[i],i+1); } return; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
cin>>n>>k; for(int i=1;i<=n;i++) cin>>arr[i];
dfs(0,0,1); cout<<ans;
return 0; }
|
P1157 组合的输出
https://www.luogu.com.cn/problem/p1157
题解
dfs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include<bits/stdc++.h> using namespace std;
const int N=25; int n,r; int arr[N];
void dfs(int k) { if(k>r) { for(int i=1;i<=r;i++) cout<<setw(3)<<arr[i]; cout<<endl; return; } for(int i=arr[k-1]+1;i<=n;i++) { arr[k]=i; dfs(k+1); } }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
cin>>n>>r; dfs(1);
return 0; }
|
P1706 全排列问题
https://www.luogu.com.cn/problem/p1706
题解
dfs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include<bits/stdc++.h> using namespace std;
int n,pd[100],used[100];
void print() { for(int i=1;i<=n;i++) cout<<setw(5)<<used[i]; cout<<endl; }
void dfs(int k) { if(k==n) { print(); return; } for(int i=1;i<=n;i++) { if(!pd[i]) { pd[i]=1; used[k+1]=i; dfs(k+1); pd[i]=0; } } }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
cin>>n; dfs(0);
return 0; }
|