Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 11661 | Accepted: 4940 | 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)
题意是每次对面前的两个壶有六种选择,倒掉1壶的水,倒掉2壶的水,填满1壶的水,填满2壶的水,把2壶的水倒入1壶,把1壶的水倒入2壶。
然后求想要达到结果C毫升水的最小路径,并输出这个路径。
这个题目形式又是 选择+最小路径 ,广度优先搜索无疑。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <queue> #include <string> #include <cstring> #pragma warning(disable:4996) using namespace std; queue <int> pot1; queue <int> pot2; int buzhou[105][105]; string path[105][105];// drop[1] drop[2] fill[1] fill[2] pour[2,1] pour[1,2] int main() { //freopen("i.txt","r",stdin); //freopen("o.txt","w",stdout); int A,B,C,flag,v1,v2,i; cin>>A>>B>>C; memset(buzhou,0,sizeof(buzhou)); pot1.push(0); pot2.push(0); flag=0; v1=0; v2=0; buzhou[v1][v2]=1; while(pot1.size()&&pot2.size()) { v1=pot1.front(); v2=pot2.front(); pot1.pop(); pot2.pop(); if(v1==C||v2==C) { flag=1; cout<<buzhou[v1][v2]-1<<endl; for(i=0;i<path[v1][v2].size();i++) { if(path[v1][v2][i]=='1') cout<<"DROP(2)"<<endl; else if(path[v1][v2][i]=='2') cout<<"DROP(1)"<<endl; else if(path[v1][v2][i]=='3') cout<<"FILL(1)"<<endl; else if(path[v1][v2][i]=='4') cout<<"FILL(2)"<<endl; else if(path[v1][v2][i]=='5') cout<<"POUR(2,1)"<<endl; else if(path[v1][v2][i]=='6') cout<<"POUR(1,2)"<<endl; } break; } if(!buzhou[0][v2])//drop1 { buzhou[0][v2]=buzhou[v1][v2]+1; path[0][v2] = path[v1][v2]+"2"; pot1.push(0); pot2.push(v2); } if(!buzhou[v1][0])//drop2 { buzhou[v1][0]=buzhou[v1][v2]+1; path[v1][0] = path[v1][v2]+"1"; pot1.push(v1); pot2.push(0); } if(!buzhou[v1][B])//fill2 { buzhou[v1][B]=buzhou[v1][v2]+1; path[v1][B] = path[v1][v2]+"4"; pot1.push(v1); pot2.push(B); } if(!buzhou[A][v2])//fill1 { buzhou[A][v2]=buzhou[v1][v2]+1; path[A][v2] = path[v1][v2]+"3"; pot1.push(A); pot2.push(v2); } if(v1+v2<A)//b to a { if(!buzhou[v1+v2][0]) { buzhou[v1+v2][0]=buzhou[v1][v2]+1; path[v1+v2][0]= path[v1][v2]+"5"; pot1.push(v1+v2); pot2.push(0); } } else { if(!buzhou[A][v1+v2-A]) { buzhou[A][v1+v2-A]=buzhou[v1][v2]+1; path[A][v1+v2-A]= path[v1][v2]+"5"; pot1.push(A); pot2.push(v1+v2-A); } } if(v1+v2<B) { if(!buzhou[0][v1+v2]) { buzhou[0][v1+v2]=buzhou[v1][v2]+1; path[0][v1+v2]= path[v1][v2]+"6"; pot1.push(0); pot2.push(v1+v2); } } else { if(!buzhou[v1+v2-B][B]) { buzhou[v1+v2-B][B]=buzhou[v1][v2]+1; path[v1+v2-B][B]= path[v1][v2]+"6"; pot1.push(v1+v2-B); pot2.push(B); } } } if(!flag) cout<<"impossible"<<endl; system("pause"); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。