https://vjudge.net/contest/359331#problem/B

倒水问题,dfs暴力23333

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <queue>
  4 #include <algorithm>
  5 #include <stack>
  6 #include <cstring>
  7 #define inf 2147483647
  8 #define N 1000010
  9 #define p(a) putchar(a)
 10 #define For(i,a,b) for(int i=a;i<=b;++i)
 11 
 12 using namespace std;
 13 int a,b,c;
 14 string k[]={"fill A","fill B","pour A B","pour B A","empty A","empty B"}; 
 15 bool vis[1010][1010],flag;
 16 stack<string>s;
 17 void in(int &x){
 18     int y=1;char c=getchar();x=0;
 19     while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
 20     while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
 21     x*=y;
 22 }
 23 void o(int x){
 24     if(x<0){p('-');x=-x;}
 25     if(x>9)o(x/10);
 26     p(x%10+'0');
 27 }
 28 
 29 void dfs(int x,int y){
 30     if(x==c||y==c){
 31         flag=1;
 32         s.push("success");
 33         return;
 34     }
 35     if(!vis[a][y]){
 36         vis[a][y]=1;
 37         dfs(a,y);
 38         if(flag){
 39             s.push(k[0]);
 40             return;
 41         }
 42         vis[a][y]=0;
 43     }
 44     if(!vis[x][b]){
 45         vis[x][b]=1;
 46         dfs(x,b);
 47         if(flag){
 48             s.push(k[1]);
 49             return;
 50         }
 51         vis[x][b]=0;
 52     }
 53     if(x+y<=b){
 54         if(!vis[0][x+y]){
 55             vis[0][x+y]=1;
 56             dfs(0,x+y);
 57             if(flag){
 58                 s.push(k[2]);
 59                 return;
 60             }
 61             vis[0][x+y]=0;
 62         }
 63     }
 64     else{
 65         if(!vis[x+y-b][b]){
 66             vis[x+y-b][b]=1;
 67             dfs(x+y-b,b);
 68             if(flag){
 69                 s.push(k[2]);
 70                 return;
 71             }
 72             vis[x+y-b][b]=0;
 73         }
 74     }
 75     if(x+y<=a){
 76         if(!vis[x+y][0]){
 77             vis[x+y][0]=1;
 78             dfs(x+y,0);
 79             if(flag){
 80                 s.push(k[3]);
 81                 return;
 82             }
 83             vis[x+y][0]=0;
 84         }
 85     }
 86     else{
 87         if(!vis[a][x+y-a]){
 88             vis[a][x+y-a]=1;
 89             dfs(a,x+y-a);
 90             if(flag){
 91                 s.push(k[3]);
 92                 return;
 93             }
 94             vis[a][x+y-a]=0;
 95         }
 96     }
 97     if(!vis[0][y]){
 98         vis[0][y]=1;
 99         dfs(0,y);
100         if(flag){
101             s.push(k[4]);
102             return;
103         }
104         vis[0][y]=0;
105     }
106     if(!vis[x][0]){
107         vis[x][0]=1;
108         dfs(x,0);
109         if(flag){
110             s.push(k[5]);
111             return;
112         }
113         vis[x][0]=0;
114     }
115 }
116 
117 signed main(){
118     while(cin>>a>>b>>c){
119         memset(vis,0,sizeof(vis));
120         flag=0;
121         dfs(0,0);
122         while(!s.empty()){
123             cout<<s.top()<<'
';s.pop();
124         }
125     }
126     return 0;
127 }
原文地址:https://www.cnblogs.com/war1111/p/12372224.html