分析:我们用vis[i][j][k]来记录A,B,C三个状态是否被访问过,同时用s[i]来记录C的所有可能值,当i==0时,如果j合法,则标记s[k]=1,最后统计所有为1的s即可
1 /* 2 PROB:milk3 3 ID:wanghan 4 LANG:C++ 5 */ 6 #include "iostream" 7 #include "cstdio" 8 #include "cstring" 9 #include "string" 10 #include "vector" 11 using namespace std; 12 const int maxn=25; 13 int vis[maxn][maxn][maxn]; 14 int s[maxn]; 15 int A,B,C; 16 void dfs(int i,int j,int k){ 17 if(vis[i][j][k]) return; 18 if(i>A||j>B||k>C) return; 19 if(i==0){ 20 s[k]=1; 21 } 22 vis[i][j][k]=1; 23 if(k>=A-i) //C->A 24 dfs(A,j,k-(A-i)); 25 else 26 dfs(i+k,j,0); 27 if(k>=B-j) //C->B 28 dfs(i,B,k-(B-j)); 29 else 30 dfs(i,j+k,0); 31 if(i>=C-k) //A->C 32 dfs(i-(C-k),j,C); 33 else 34 dfs(0,j,k+i); 35 if(j>=C-k) //B->C 36 dfs(i,j-(C-k),C); 37 else 38 dfs(i,0,j+k); 39 if(i>=B-j) //A->B 40 dfs(i-(B-j),B,k); 41 else 42 dfs(0,i+j,k); 43 if(j>=A-i) //B->A 44 dfs(A,j-(A-i),k); 45 else 46 dfs(i+j,0,k); 47 } 48 int main() 49 { 50 freopen("milk3.in","r",stdin); 51 freopen("milk3.out","w",stdout); 52 cin>>A>>B>>C; 53 memset(vis,0,sizeof(vis)); 54 memset(s,0,sizeof(s)); 55 dfs(0,0,C); 56 vector<int> h; 57 for(int i=0;i<=C;i++) 58 if(s[i]) 59 h.push_back(i); 60 for(int i=0;i<h.size();i++){ 61 if(i==h.size()-1) 62 cout<<h[i]<<endl; 63 else 64 cout<<h[i]<<" "; 65 } 66 return 0; 67 }