POJ 3414:Pots
来源:互联网 发布:羽毛球鹰眼算法 编辑:程序博客网 时间:2024/06/05 05:23
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
6FILL(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;}
- POJ 3414 Pots
- poj 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots BFS
- POJ 3414 Pots
- poj 3414 Pots
- poj 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- POJ 3414(Pots)
- POJ 3414 Pots
- POJ 3414 Pots
- POJ-3414-Pots
- POJ 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- 安全漏洞的分类、描述与解决方案
- [水题] HDU 1049 Climbing Worm
- spring jpa 行级锁的实现
- GRE写作必备句型
- tabView的手势冲突问题解决方法
- POJ 3414:Pots
- Snail—OC学习之可变字典NSMutableDictionary
- GRE写作必备句型
- WPF 自定义MessageBox
- Tomcat内存设置详解
- TCP/IP网络编程之四书五经
- Ubuntu编译vnc源码 realvnc
- java的基本编程模式
- 2012年5月SAT香港真题解析