题目大意:
输入 A B C 为三个容器的容量
一开始A B是空的 C是满的
每一次倾倒只能在 盛的容器满 或 倒的容器空 时才停止
输出当A容器空时 C容器内剩余量的所有可能值
Sample Input
8 9 10
2 5 10
Sample Output
1 2 8 9 10
5 6 7 8 9 10
#include<iostream> #include<algorithm> #include<stdio.h> #include<cstring> #include<math.h> using namespace std; int flag[25],vis[25][25],ans[25]; int a,b,c,len; void DFS(int nowa,int nowc) { if(vis[nowa][nowc]) return; vis[nowa][nowc]=1; if(nowa==0&&!flag[nowc]) flag[nowc]=1, ans[len++]=nowc; int nowb=c-nowa-nowc; if(nowa&&nowc<c) ///a->c DFS(nowa-min(nowa,c-nowc),nowc+min(nowa,c-nowc)); if(nowa&&nowb<b) ///a->b DFS(nowa-min(nowa,b-nowb),nowc); if(nowc&&nowa<a) ///c->a DFS(nowa+min(nowc,a-nowa),nowc-min(nowc,a-nowa)); if(nowc&&nowb<b) ///c->b DFS(nowa,nowc-min(nowc,b-nowb)); if(nowb&&nowa<a) ///b->a DFS(nowa+min(a-nowa,nowb),nowc); if(nowb&&nowc<c) ///b->c DFS(nowa,nowc+min(c-nowc,nowb)); } int main() { while(~scanf("%d%d%d",&a,&b,&c)) { memset(ans,0,sizeof(ans)); memset(vis,0,sizeof(vis)); memset(flag,0,sizeof(flag)); len=0; DFS(0,c); sort(ans,ans+len); for(int i=0;i<len-1;i++) printf("%d ",ans[i]); printf("%d ",ans[len-1]); } return 0; }