问题描述
倒水问题 ,给你两个容器,容量分别为A, B ,问是否能够经过有限的步骤倒水, 得到容量为 C 的水。" f i l l A fill\thinspace A fillA" 表示倒满A杯," e m p t y A empty \thinspace A emptyA“表示倒空A杯,” p o u r A B pour \thinspace A\thinspace B pourAB" 表示把A的水倒到B杯并且把B杯倒满或A倒空。
Input
输入包含多组数据。每组数据输入 A, B, C 数据范围 0 < A <= B 、C <= B <=1000 、A和B互质。
Output
你的程序的输出将由一系列的指令组成。这些输出行将导致任何一个罐子正好包含C单位的水。每组数据的最后一行输出应该是“ s u c c e s s success success”。输出行从第1列开始,不应该有空行或任何尾随空格。
Sample Input
2 7 5
2 7 4
Sample Output
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
success
Notes
如果你的输出与Sample Output不同,那没关系。对于某个"A B C"本题的答案是多解的,不能通过标准的文本对比来判定你程序的正确与否。 所以本题由 SPJ(Special Judge)程序来判定你写的代码是否正确。
名词解释-隐式图
隐式图,顾名思义,并没有给你完全的一个图结构,只是给了你初始节点、终止节点、生成子节点的约束条件(题目中隐含)。本题就是一个典型的隐式图问题,初始节点是A和B两个空杯子,终止节点是一个杯子中水的体积是C。生成子节点的约束条件即为三种操作: f i l l fill fill, p o u r pour pour, e m p t y empty empty。
解题思路
这个隐式图问题可以使用暴力的BFS直接搜索,由于生成子节点的操作共有六种( f i l l A fill\thinspace A fillA, f i l l B fill\thinspace B fillB, e m p t y A empty\thinspace A emptyA, e m p t y B empty\thinspace B emptyB, p o u r A B pour\thinspace A \thinspace B pourAB, p o u r A B pour\thinspace A \thinspace B pourAB),所以我们只需要向这六个方向搜索就行了。整体代码的结构及其类似于二维地图搜索。具体代码如下。
代码
//#pragma GCC optimize(2)//比赛禁止使用!
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int maxn=1000+1;
int a,b,c;
struct status//存储当前状态,这就是节点,类似于地图中的坐标
{
int A,B;
bool operator <(const status &S) const
{
return A!=S.A ? A<S.A : B<S.B;
}
status& operator =(const status &s)
{
A=s.A; B=s.B;
return *this;
}
};
status dab(status s,int i)//i从1-6代表六种操作:fill A,fill B,empty A,empty B,pour A B,pour B A;类似于迷宫的dx,dy
{
if(i==1) s.A=a;
else if(i==2) s.B=b;
else if(i==3) s.A=0;
else if(i==4) s.B=0;
else if(i==5)
{
if(s.A+s.B>b)
{
s.A=s.A+s.B-b;
s.B=b;
}
else s.B=s.A+s.B,s.A=0;
}
else if(i==6)
{
if(s.A+s.B>a)
{
s.B=s.A+s.B-a;
s.A=a;
}
else s.A=s.A+s.B,s.B=0;
}
return s;
}
map<status,bool> mp;//就是迷宫问题中的vis数组,用来判断当前状态是否到达过
map<status,pair<int,status>> path;//用于回溯路径,记录当前状态的上一个状态
void output(int temp)
{
if(temp==1) cout<<"fill A"<<endl;
else if(temp==2) cout<<"fill B"<<endl;
else if(temp==3) cout<<"empty A"<<endl;
else if(temp==4) cout<<"empty B"<<endl;
else if(temp==5) cout<<"pour A B"<<endl;
else if(temp==6) cout<<"pour B A"<<endl;
}
void findthepath(status news)
{
if(path[news].second.A==0 && path[news].second.B==0)//回到初始节点
{
output(path[news].first);
return;
}
findthepath(path[news].second);
output(path[news].first);
}
void bfs()
{
queue<status> q;
while(!q.empty()) q.pop();
mp.clear(); path.clear();
status start;
start.A=0,start.B=0;
q.push(start);//初始状态,两个杯子都是空的
mp[start]=true;
while(!q.empty())
{
status s=q.front(); q.pop();
if(s.A==c || s.B==c)//结束条件
{
findthepath(s);//回溯路径
cout<<"success"<<endl;
return ;
}
for (int i=1; i<=6; i++)//类似于二维迷宫搜索四个方向
{
status news;
news=dab(s,i);//找出新的情况
if(!mp[news])//当前状态没有搜索到过,类似于迷宫问题当前位置没有到达过
{
mp[news]=true;
path[news]=make_pair(i,s);//当前news状态是通过i这个步骤来的,之前的步骤是s
q.push(news);//将新情况加入队列
}
}
}
}
int getint()
{
int x=0,s=1;
char ch=' ';
while(ch<'0' || ch>'9')
{
ch=getchar();
if(ch=='-') s=-1;
}
while(ch>='0' && ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*s;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
while(cin>>a>>b>>c)
{
bfs();
}
return 0;
}