求助:这题用DFS怎么做

P2089 烤鸡

ac代码(~~很刺激的代码谁试谁知道~~) ``` #include<bits/stdc++.h> using namespace std; int main() { int a,b,c,d,e,f,g,h,i,j,ans,x=0; cin>>ans; for (a=1;a<=3;a++) { for (b=1;b<=3;b++) { for (c=1;c<=3;c++) { for (d=1;d<=3;d++) { for (e=1;e<=3;e++) { for (f=1;f<=3;f++) { for (g=1;g<=3;g++) { for(h=1;h<=3;h++) { for (i=1;i<=3;i++) { for (j=1;j<=3;j++) { if (a+b+c+d+e+f+g+h+i+j==ans) { x++; } } } } } } } } } } } cout<<x<<endl; for (a=1;a<=3;a++) { for (b=1;b<=3;b++) { for (c=1;c<=3;c++) { for (d=1;d<=3;d++) { for (e=1;e<=3;e++) { for (f=1;f<=3;f++) { for (g=1;g<=3;g++) { for(h=1;h<=3;h++) { for (i=1;i<=3;i++) { for (j=1;j<=3;j++) { if (a+b+c+d+e+f+g+h+i+j==ans) { cout<<a<<" "; cout<<b<<" "; cout<<c<<" "; cout<<d<<" "; cout<<e<<" "; cout<<f<<" "; cout<<g<<" "; cout<<h<<" "; cout<<i<<" "; cout<<j<<endl; } } } } } } } } } } } return 0; } ```
by Alpha丶 @ 2018-11-11 18:05:19


~~~ #include<iostream> #include<cmath> using namespace std; int now[10],zs; void print() { for(int i=0;i<10;i++)cout<<now[i]<<" "; cout<<endl; } void ans_z(int n,int i) { if(i==9){now[9]=n;zs++;return;} for(int j=max(n-27+3*i,1);j<=min(3,n-9+i);j++) { now[i]=j; ans_z(n-j,i+1); } return; } void ans_p(int n,int i) { if(i==9){now[9]=n;print();return;} for(int j=max(n-27+3*i,1);j<=min(3,n-9+i);j++) { now[i]=j; ans_p(n-j,i+1); } return; } int main() { int n; cin>>n; ans_z(n,0); cout<<zs<<endl; ans_p(n,0); return 0; } ~~~
by ttklwxx @ 2018-11-11 18:05:39


至于dfs我也很无能为力啊 (~~毕竟只是新手村的题目~~)
by Alpha丶 @ 2018-11-11 18:06:11


退役后第一次手写一下核心代码,CE别怪我 ~~方案什么的就自己预处理去吧~~ ```cpp int use[1000], n; inline void dfs(int now) { if(now == 11) { int all = 0; for(int i = 1; i <= 10; ++i) all += use[i]; if(all == n) { for(int i = 1; i <= 10; ++i) printf("%d ", use[i]); cout << endl; } return ; } for(int i = 1; i <= 3; ++i) { use[now] = i; dfs(now+1); } return ; } ```
by Fraction @ 2018-11-11 18:13:44


```cpp #include<iostream> using namespace std; int a[11],x=0,n,s=0,ans[11][10001]; void so(int m) { for(int i=1;i<=3;i++) if(m<=10) { x+=i; a[m]=i; if(m==10&&x==n) { s++; for(int j=1;j<=10;j++) ans[j][s]=a[j]; } else so(m+1); x-=i; } } int main() { cin>>n; so(1); cout<<s<<endl; for(int i=1;i<=s;i++) { for(int j=1;j<=10;j++) cout<<ans[j][i]<<" "; cout<<endl; } return 0; } ```
by csdfret @ 2018-11-11 18:15:25


入门难度的dfs
by FCBM71 @ 2018-11-11 18:23:29


|