Fill
There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greaterthan 200). The first and the second jug are initially empty, while the third is completely filled withwater. It is allowed to pour water from one jug into another until either the first one is empty or thesecond one is full. This operation can be performed zero, one or more times.
You are to write a program that computes the least total amount of water that needs to be poured;so that at least one of the jugs contains exactly d liters of water (d is a positive integer not greaterthan 200). If it is not possible to measure d liters this way your program should find a smaller amountof water d′ < d which is closest to d and for which d′liters could be produced. When d′is found, yourprogram should compute the least total amount of poured water needed to produce d′liters in at leastone of the jugs.
InputThe first line of input contains the number of test cases. In the next T lines, T test cases follow. Eachtest case is given in one line of input containing four space separated integers — a, b, c and d.
OutputThe output consists of two integers separated by a single space. The first integer equals the least totalamount (the sum of all waters you pour from one jug to another) of poured water. The second integerequals d, if d liters of water could be produced by such transformations, or equals the closest smallervalue d′that your program has found.
Sample Input
2
2 3 4 2
96 97 199 62
Sample Out
2 2
9859 62
题意:给你三个杯子,a,b,c;只有c杯子装满
水,给的测试数据是每个杯子的容量,第四
个是要求得的水的体积;若得不到,则取最
接近的值,并输出倒水总量;
思路:广搜和深搜都可以,在这用的是广搜
首先,用一个三维数组标记三个杯子的状
态。再用一个数组VIS,下标存的是水的状态
对应值是达到这种状态的最小倒水量。
例如:2 2 4 2;
开始时,第一次vis[4]=0,
后面无论c杯多少
次
达到4体积,VIS[4]的值永远为零。
总共有12种倒水,例如:a杯倒水,
a可以倒到b杯,也可以倒到c杯。a倒b杯,
可能有剩余,也可能没剩余。即a有四种,
所以应该有12种。
#include <cstdio>
#include <queue>
#include<string.h>
#define min(A,B)(A<B?A:B)
#define MAX1 999999999
using namespace std;
int book[210][210][210];//标记三个杯子的状态
int a,b,c,d,flag;
int vis[210]; //vis数组,下标存的是杯子中水的量,值是倒水的总量
struct note
{
int a[3];
int sum;
friend bool operator<(note t1,note t2)
{
return t1.sum>t2.sum;
}
};
void bfs()
{
priority_queue<note>Q;
note p,q;
p.a[0]=0;
p.a[1]=0;
p.a[2]=c;
p.sum=0;
book[0][0][c]=1;
Q.push(p);
while(!Q.empty())
{
p=Q.top();
Q.pop();
int x=p.a[0];
int y=p.a[1];
int z=p.a[2];
vis[x]=min(p.sum,vis[x]); //更新VIS[i]的值
vis[y]=min(p.sum,vis[y]);
vis[z]=min(p.sum,vis[z]);
//printf("%d %d %d\n",x,y,z);
if(x==d||y==d||z==d)
{
flag=1;
printf("%d %d\n",p.sum,d);
break;
}
if(x) //a杯子有水
{
if(y<b) //b杯子未满
{
if(x<b-y&&!book[0][x+y][z]) //a不能把b杯倒满。
{
book[0][x+y][z]=1;
q.a[0]=0;
q.a[1]=x+y;
q.a[2]=z;
q.sum=p.sum+x;
Q.push(q);
}
else if(x>=b-y&&!book[x-b+y][b][z])//a能把b杯倒满
{
book[x-b+y][b][z]=1;
q.a[0]=x-b+y;
q.a[1]=b;
q.a[2]=z;
q.sum=p.sum+b-y;
Q.push(q);
}
}
if(z<c) //c杯未满
{
if(x<c-z&&!book[0][y][z+x]) //a杯不能把c杯倒满
{
book[0][y][z+x]=1;
q.a[0]=0;
q.a[1]=y;
q.a[2]=z+x;
q.sum=p.sum+x;
Q.push(q);
}
else if(x>=c-z&&!book[x+z-c][y][c])//a杯能把c杯倒满
{
book[0][y][c]=1;
q.a[0]=x+z-c;
q.a[1]=y;
q.a[2]=c;
q.sum=p.sum+c-z;
Q.push(q);
}
}
}
if(y)
{
if(x<a)
{
if(y<a-x&&!book[x+y][0][z])
{
book[x+y][0][z]=1;
q.a[0]=x+y;
q.a[1]=0;
q.a[2]=z;
q.sum=p.sum+y;
Q.push(q);
}
else if(y>=a-x&&!book[a][y+x-a][z])
{
book[a][y+x-a][z]=1;
q.a[0]=a;
q.a[1]=y+x-a;
q.a[2]=z;
q.sum=p.sum+a-x;
Q.push(q);
}
}
if(z<c)
{
if(y<c-z&&!book[x][0][y+z])
{
book[x][0][y+z]=1;
q.a[0]=x;
q.a[1]=0;
q.a[2]=y+z;
q.sum=p.sum+y;
Q.push(q);
}
else if(y>=c-z&&!book[x][y+z-c][c])
{
book[x][y+z-c][c]=1;
q.a[0]=x;
q.a[1]=y+z-c;
q.a[2]=c;
q.sum=p.sum+c-z;
Q.push(q);
}
}
}
if(z)
{
if(x<a)
{
if(z<a-x&&!book[x+z][y][0])
{
book[x+z][y][0]=1;
q.a[0]=x+z;
q.a[1]=y;
q.a[2]=0;
q.sum=p.sum+z;
Q.push(q);
}
else if(z>=a-x&&!book[a][y][z-a+x])
{
book[a][y][z-a+x]=1;
q.a[0]=a;
q.a[1]=y;
q.a[2]=z-a+x;
q.sum=p.sum+a-x;
Q.push(q);
}
}
if(y<b)
{
if(z<b-y&&!book[x][z+y][0])
{
book[x][z+y][0]=1;
q.a[0]=x;
q.a[1]=z+y;
q.a[2]=0;
q.sum=p.sum+z;
Q.push(q);
}
else if(z>=b-y&&!book[x][b][z+y-b])
{
book[x][b][z+y-b]=1;
q.a[0]=x;
q.a[1]=b;
q.a[2]=z+y-b;
q.sum=p.sum+b-y;
Q.push(q);
}
}
}
}
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d %d",&a,&b,&c,&d);
memset(book,0,sizeof(book));
for(int i=0; i<210; i++)
vis[i]=MAX1;
flag=0;
bfs();
if(!flag)
for(int i=d; i>=0; i--)
if(vis[i]!=MAX1)
{
printf("%d %d\n",vis[i],i);
break;
}
}
return 0;
}

本文介绍了一种使用广度优先搜索解决倒水问题的方法,旨在找出最少倒水次数使某容器含特定水量或最接近该水量的情况。通过定义状态和操作,实现了有效搜索。
290

被折叠的 条评论
为什么被折叠?



