poj 3414 Pots 和 hdu 1495 非常可乐 (bfs)

两道很像的题,都是倒水看你能不能倒出题目要求的样子。

BFS模拟倒水过程找到解就行了。

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20762 Accepted: 8856 Special Judge

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input

On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <stack>  
  4. #include <cstring>  
  5. #define INF 0x3f3f3f3f  
  6. #define MOD 1e9+7  
  7. using namespace std;  
  8. int vis[105][105];  
  9. char op[6][15]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};  
  10. struct pot  
  11. {  
  12.     int a,b;  
  13.     int f;  
  14.     int step;  
  15.     int op;  
  16. }que[100005];  
  17. stack <int> ss;  
  18.   
  19. int main()  
  20. {  
  21.     int A,B,C;  
  22.     int flag=0;  
  23.     cin>>A>>B>>C;  
  24.     if(C==0)  
  25.     {  
  26.         cout<<0<<endl;  
  27.         return 0;  
  28.     }  
  29.     int head,tail;  
  30.     head=tail=1;  
  31.     que[tail].a=que[tail].b=que[tail].step=0;  
  32.     que[tail].f=-1;  
  33.     vis[0][0]=1;  
  34.     tail++;  
  35.     while(head<tail)  
  36.     {  
  37.         int x,y;  
  38.         for(int i=0;i<6;i++)  
  39.         {  
  40.             switch(i)  
  41.             {  
  42.                 case 0:  
  43.                     x=A;  
  44.                     y=que[head].b;  
  45.                     break;  
  46.                 case 1:  
  47.                     x=que[head].a;  
  48.                     y=B;  
  49.                     break;  
  50.                 case 2:  
  51.                     x=0;  
  52.                     y=que[head].b;  
  53.                     break;  
  54.                 case 3:  
  55.                     x=que[head].a;  
  56.                     y=0;  
  57.                     break;  
  58.                 case 4:  
  59.                     y=que[head].a+que[head].b;  
  60.                     x=0;  
  61.                     if(y>B)  
  62.                     {  
  63.                         x=y-B;  
  64.                         y=B;  
  65.                     }  
  66.                     break;  
  67.                 case 5:  
  68.                     x=que[head].a+que[head].b;  
  69.                     y=0;  
  70.                     if(x>A)  
  71.                     {  
  72.                         y=x-A;  
  73.                         x=A;  
  74.                     }  
  75.                     break;  
  76.             }  
  77.             if(vis[x][y]==1)  
  78.                 continue;  
  79.             vis[x][y]=1;  
  80.             que[tail].a=x;  
  81.             que[tail].b=y;  
  82.             que[tail].f=head;  
  83.             que[tail].op=i;  
  84.             que[tail].step=que[head].step+1;  
  85.             if(x==C||y==C)  
  86.             {  
  87.                 flag=1;  
  88.                 break;  
  89.             }  
  90.             tail++;  
  91.         }  
  92.         if(flag)  
  93.             break;  
  94.         head++;  
  95.     }  
  96.     if(flag)  
  97.     {  
  98.         int now=tail;  
  99.         while(now>1)  
  100.         {  
  101.             ss.push(now);  
  102.         //    cout<<now<<endl;  
  103.             now=que[now].f;  
  104.         }  
  105.         cout<<que[tail].step<<endl;  
  106.         while(!ss.empty())  
  107.         {  
  108.             now=ss.top();  
  109.             ss.pop();  
  110.             cout<<op[que[now].op]<<endl;  
  111.         }  
  112.     }  
  113.     else  
  114.         cout<<"impossible"<<endl;  
  115.     return 0;  
  116. }  


非常可乐

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20345    Accepted Submission(s): 8247


Problem Description
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
 

Input
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
 

Output
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
 

Sample Input
 
 
7 4 3 4 1 3 0 0 0
 

Sample Output
 
 
NO 3
 

Author
seeyou
 

Source
 

Recommend
LL


[cpp]  view plain  copy
  1. #include <bits/stdc++.h>  
  2. #include <cstring>  
  3. #define INF 0x3f3f3f3f  
  4. #define MOD 1e9+7  
  5. using namespace std;  
  6. struct colo  
  7. {  
  8.     int s,n,m;  
  9.     int step;  
  10. };  
  11. int vis[105][105][105];  
  12.   
  13. int main()  
  14. {  
  15.     int s,n,m;  
  16.     while(cin>>s>>n>>m)  
  17.     {  
  18.         if(s==0&&n==0&&m==0)  
  19.             break;  
  20.         if(s%2)  
  21.         {  
  22.             cout<<"NO"<<endl;  
  23.             continue;  
  24.         }  
  25.         memset(vis,0,sizeof(vis));  
  26.         vis[s][0][0]=1;  
  27.         colo st={s,0,0,0};  
  28.         queue<colo> que;  
  29.         que.push(st);  
  30.         int ans=0,flag=0;  
  31.         while(!que.empty())  
  32.         {  
  33.             colo now=que.front();  
  34.             que.pop();  
  35.             colo temp;  
  36.             for(int i=0;i<6;i++)  
  37.             {  
  38.                 switch(i)  
  39.                 {  
  40.                     case 0:  
  41.                         temp.m=now.m;  
  42.                         temp.s=0;  
  43.                         temp.n=now.s+now.n;  
  44.                         if(temp.n>n)  
  45.                         {  
  46.                             temp.s=temp.n-n;  
  47.                             temp.n=n;  
  48.                         }  
  49.                         break;  
  50.                     case 1:  
  51.                         temp.m=now.m;  
  52.                         temp.s=now.s+now.n;  
  53.                         temp.n=0;  
  54.                         break;  
  55.                     case 2:  
  56.                         temp.s=now.s;  
  57.                         temp.m=now.n+now.m;  
  58.                         temp.n=0;  
  59.                         if(temp.m>m)  
  60.                         {  
  61.                             temp.n=temp.m-m;  
  62.                             temp.m=m;  
  63.                         }  
  64.                         break;  
  65.                     case 3:  
  66.                         temp.s=now.s;  
  67.                         temp.n=now.n+now.m;  
  68.                         temp.m=0;  
  69.                         if(temp.n>n)  
  70.                         {  
  71.                             temp.m=temp.n-n;  
  72.                             temp.n=n;  
  73.                         }  
  74.                         break;  
  75.                     case 4:  
  76.                         temp.n=now.n;  
  77.                         temp.m=now.s+now.m;  
  78.                         temp.s=0;  
  79.                         if(temp.m>m)  
  80.                         {  
  81.                             temp.s=temp.m-m;  
  82.                             temp.m=m;  
  83.                         }  
  84.                         break;  
  85.                     case 5:  
  86.                         temp.n=now.n;  
  87.                         temp.s=now.s+now.m;  
  88.                         temp.m=0;  
  89.                         break;  
  90.                 }  
  91.                 if(vis[temp.s][temp.n][temp.m]==1)  
  92.                     continue;  
  93.                 temp.step=now.step+1;  
  94.                 vis[temp.s][temp.n][temp.m]=1;  
  95.                 que.push(temp);  
  96.                 if(temp.m==s/2&&temp.n==s/2||temp.m==s/2&&temp.s==s/2||temp.n==s/2&&temp.s==s/2)  
  97.                 {  
  98.                     ans=temp.step;  
  99.                     flag=1;  
  100.                     break;  
  101.                 }  
  102.             }  
  103.             if(flag)  
  104.                 break;  
  105.         }  
  106.         if(flag)  
  107.             cout<<ans<<endl;  
  108.         else  
  109.             cout<<"NO"<<endl;  
  110.     }  
  111.     return 0;  
  112. }  
一个要输出路径,一个倒得方法多,也说不上哪个难,反正都差不多。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值