两道很像的题,都是倒水看你能不能倒出题目要求的样子。
BFS模拟倒水过程找到解就行了。
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:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- 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 A, B, 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)
- #include <iostream>
- #include <cstdio>
- #include <stack>
- #include <cstring>
- #define INF 0x3f3f3f3f
- #define MOD 1e9+7
- using namespace std;
- int vis[105][105];
- char op[6][15]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
- struct pot
- {
- int a,b;
- int f;
- int step;
- int op;
- }que[100005];
- stack <int> ss;
- int main()
- {
- int A,B,C;
- int flag=0;
- cin>>A>>B>>C;
- if(C==0)
- {
- cout<<0<<endl;
- return 0;
- }
- int head,tail;
- head=tail=1;
- que[tail].a=que[tail].b=que[tail].step=0;
- que[tail].f=-1;
- vis[0][0]=1;
- tail++;
- while(head<tail)
- {
- int x,y;
- for(int i=0;i<6;i++)
- {
- switch(i)
- {
- case 0:
- x=A;
- y=que[head].b;
- break;
- case 1:
- x=que[head].a;
- y=B;
- break;
- case 2:
- x=0;
- y=que[head].b;
- break;
- case 3:
- x=que[head].a;
- y=0;
- break;
- case 4:
- y=que[head].a+que[head].b;
- x=0;
- if(y>B)
- {
- x=y-B;
- y=B;
- }
- break;
- case 5:
- x=que[head].a+que[head].b;
- y=0;
- if(x>A)
- {
- y=x-A;
- x=A;
- }
- break;
- }
- if(vis[x][y]==1)
- continue;
- vis[x][y]=1;
- que[tail].a=x;
- que[tail].b=y;
- que[tail].f=head;
- que[tail].op=i;
- que[tail].step=que[head].step+1;
- if(x==C||y==C)
- {
- flag=1;
- break;
- }
- tail++;
- }
- if(flag)
- break;
- head++;
- }
- if(flag)
- {
- int now=tail;
- while(now>1)
- {
- ss.push(now);
- // cout<<now<<endl;
- now=que[now].f;
- }
- cout<<que[tail].step<<endl;
- while(!ss.empty())
- {
- now=ss.top();
- ss.pop();
- cout<<op[que[now].op]<<endl;
- }
- }
- else
- cout<<"impossible"<<endl;
- return 0;
- }
非常可乐
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 20345 Accepted Submission(s): 8247
- #include <bits/stdc++.h>
- #include <cstring>
- #define INF 0x3f3f3f3f
- #define MOD 1e9+7
- using namespace std;
- struct colo
- {
- int s,n,m;
- int step;
- };
- int vis[105][105][105];
- int main()
- {
- int s,n,m;
- while(cin>>s>>n>>m)
- {
- if(s==0&&n==0&&m==0)
- break;
- if(s%2)
- {
- cout<<"NO"<<endl;
- continue;
- }
- memset(vis,0,sizeof(vis));
- vis[s][0][0]=1;
- colo st={s,0,0,0};
- queue<colo> que;
- que.push(st);
- int ans=0,flag=0;
- while(!que.empty())
- {
- colo now=que.front();
- que.pop();
- colo temp;
- for(int i=0;i<6;i++)
- {
- switch(i)
- {
- case 0:
- temp.m=now.m;
- temp.s=0;
- temp.n=now.s+now.n;
- if(temp.n>n)
- {
- temp.s=temp.n-n;
- temp.n=n;
- }
- break;
- case 1:
- temp.m=now.m;
- temp.s=now.s+now.n;
- temp.n=0;
- break;
- case 2:
- temp.s=now.s;
- temp.m=now.n+now.m;
- temp.n=0;
- if(temp.m>m)
- {
- temp.n=temp.m-m;
- temp.m=m;
- }
- break;
- case 3:
- temp.s=now.s;
- temp.n=now.n+now.m;
- temp.m=0;
- if(temp.n>n)
- {
- temp.m=temp.n-n;
- temp.n=n;
- }
- break;
- case 4:
- temp.n=now.n;
- temp.m=now.s+now.m;
- temp.s=0;
- if(temp.m>m)
- {
- temp.s=temp.m-m;
- temp.m=m;
- }
- break;
- case 5:
- temp.n=now.n;
- temp.s=now.s+now.m;
- temp.m=0;
- break;
- }
- if(vis[temp.s][temp.n][temp.m]==1)
- continue;
- temp.step=now.step+1;
- vis[temp.s][temp.n][temp.m]=1;
- que.push(temp);
- 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)
- {
- ans=temp.step;
- flag=1;
- break;
- }
- }
- if(flag)
- break;
- }
- if(flag)
- cout<<ans<<endl;
- else
- cout<<"NO"<<endl;
- }
- return 0;
- }