当前位置: 代码迷 >> 综合 >> 问题 A: Jugs
  详细解决方案

问题 A: Jugs

热度:29   发布时间:2023-12-06 22:09:15.0

时间限制: 1 Sec  内存限制: 32 MB
提交: 288  解决: 0
[提交][状态][讨论版][命题人:外部导入]

题目描述

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

    You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

    A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

    fill A 
    fill B 
    empty A 
    empty B 
    pour A B 
    pour B A 
    success

    where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.

    You may assume that the input you are given does have a solution.

 

输入

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another. 

输出

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

样例输入

3 7 1
9 32 6

样例输出

fill B
pour B A
empty A
pour B A
success
fill B
pour B A
empty A
pour B A
empty A
pour B A
empty A
pour B A
fill B
pour B A
empty A
pour B A
empty A
pour B A
empty A
pour B A
empty A
pour B A
fill B
pour B A
empty A
pour B A
empty A
pour B A
success

 

总结:

        Ca与Cb互质,且n<Cb 则经过有限步的操作一定可以成功,所以不用考虑不成功的情况。

         每种情况的子情况都有6种,并不一定是6种都成立。

 

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#include<set>
using namespace std;string str[6]={"fill A","fill B","empty A","empty B" ,"pour A B" ,"pour B A" };struct node{vector<int> v;vector<int> step;//每一步所作的操作对应的str下标 };int Ca,Cb,k,V[2];
queue<node> q;
set<vector<int> > inq;bool Op(vector<int> &v,int i,int j){if(v[i]!=0&&v[j]!=V[j]){if(v[i]+v[j]<=V[j]){v[j]+=v[i];v[i]=0;}else{v[i]=v[i]-(V[j]-v[j]);v[j]=V[j];}//细节性的错误就不要再回顾思路与代码了,直接设立间断点,查看问题 return true;}return false;
}void bfs(){node S;S.v.push_back(0);S.v.push_back(0);q.push(S);inq.insert(S.v);while(!q.empty()){node top=q.front();q.pop();//cout<<"top.v[1]="<<top.v[1]<<endl;////cout<<"k="<<k<<endl;////judgeif(top.v[1]==k){//cout<<"top.step.size()="<<top.step.size()<<endl;//for(int i=0;i<top.step.size();i++){int idx=top.step[i];cout<<str[idx]<<endl;}return ;}//Opfor(int i=0;i<6;i++){//cout<<"i="<<i<<endl;///	node t=top;switch(i){case 0:if(t.v[0]<Ca){t.v[0]=Ca;if(inq.find(t.v)==inq.end()){t.step.push_back(0);//	cout<<"t.step.size()="<<t.step.size()<<endl;//q.push(t);inq.insert(t.v);}}break;case 1:	if(t.v[1]<Cb){t.v[1]=Cb;if(inq.find(t.v)==inq.end()){t.step.push_back(1);//	cout<<"t.step.size()="<<t.step.size()<<endl;//q.push(t);inq.insert(t.v);}}break;case 2:if(t.v[0]!=0&&t.v[0]<=Ca){t.v[0]=0;if(inq.find(t.v)==inq.end()){t.step.push_back(2);q.push(t);inq.insert(t.v);}}break;case 3:if(t.v[1]!=0&&t.v[1]<=Cb){t.v[1]=0;if(inq.find(t.v)==inq.end()){t.step.push_back(3);q.push(t);inq.insert(t.v);}}break;case 4:if(Op(t.v,0,1)&&inq.find(t.v)==inq.end()){t.step.push_back(4);q.push(t);inq.insert(t.v);}break;case 5:if(Op(t.v,1,0)&&inq.find(t.v)==inq.end()){t.step.push_back(5);q.push(t);inq.insert(t.v);}break;}			}
}}int main(){while(cin>>Ca>>Cb>>k){while(!q.empty())	q.pop();inq.clear();//集合是直接清空 V[0]=Ca; V[1]=Cb;bfs();cout<<"success"<<endl;}	}