2013
12-10

# Jugs

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 5 4
5 7 3 

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

这里添加一个水壶编号为0,容积为a和b的水壶分别编号为1和2,编号为0的水壶的容积置为a+b(保证0号水壶可以向1或2中加水,或者1或2向0中加水),再用BFS来进行求解.

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=110;
int sh[maxn],result;
int vis[maxn][maxn];
struct node
{
int v[3];
int fa;
}q[maxn*maxn];
node path[maxn*maxn];
void Print_path(int ans)
{
int i=0,j,k;
node p1,p2;
while(q[ans].fa!=ans)
{
path[i++]=q[ans];
ans=q[ans].fa;
}
path[i]=q[ans];
while(i>=1)
{
p1=path[i];
p2=path[--i];
if(p1.v[0]==p2.v[0])//用标号为0的水壶中德前后两个状态作为判断标准
{
if(p1.v[1]<p2.v[1])
printf("pour B A\n");
else
printf("pour A B\n");
}
else
{
for(j=1;j<3;j++)
if(p1.v[j]==p2.v[j])
break;
k=3-j;
if(p1.v[k]<p2.v[k])
printf("fill %c\n",k-1+'A');
else
printf("empty %c\n",k-1+'A');
}
}
printf("success\n\n");
}
void bfs()
{
int i,j,k,start=0,tail=1;
int amount;
q[0].v[0]=sh[0];
q[0].v[1]=q[0].v[2]=q[0].fa=0;
vis[0][0]=1;
while(start<tail)
{
node &u=q[start];
if(u.v[1]==result||u.v[2]==result)
{
Print_path(start);//到达目标，打印方案
return;
}
for(i=0;i<3;i++)//标号为i的水壶向标号为j的水壶中倒水
for(j=0;j<3;j++)
if(i!=j)
{
node &v=q[tail];
if(u.v[i]<sh[j]-u.v[j])
amount=u.v[i];//amount是此次操作可以倒出的水量
else
amount=sh[j]-u.v[j];
for(k=0;k<3;k++)
v.v[k]=u.v[k];//扩展新节点
v.v[i]-=amount;//倒出
v.v[j]+=amount;//倒进
if(!vis[v.v[1]][v.v[2]])
{
vis[v.v[1]][v.v[2]]=1;//标记为已访问
v.fa=start;
tail++;
}
}
start++;
}
}
int main(void)
{
while(scanf("%d%d%d",&sh[1],&sh[2],&result)!=EOF)
{
sh[0]=sh[1]+sh[2];
memset(vis,0,sizeof(vis));
bfs();
}
}

1. 站长好。我是一个准备创业的互联网小白,我们打算做一个有关国*际*游*学的平台。手上也有了一些境外资源。现阶段的团队现在没有cto.原意出让一些管理股寻找一个靠谱的技术专家做合伙人, 不知道是不是能得到您的帮助。发个帖子或者其他方式。期待您的回应。可以加我微信tianxielemon聊聊。

2. 学算法中的数据结构学到一定程度会乐此不疲的，比如其中的2－3树，类似的红黑树，我甚至可以自己写个逻辑文件系统结构来。