ARTIFICIAL INTELLIGENCE
The first problem is called 3-4 gallon water jug problem as described in the book by Elaine Rich. We begin with two empty jugs of the capacity of 3 and 4 gallons respectively. We have an infinite supply of water available and we want the 4-gallon jug to be filled with exactly 2 gallons of water.
Another problem is called 8-5-3 milk jug problem which begins with 8-gallon milk jug completely full while other two are empty. We want exactly 4 gallons of milk in the 8-gallon jug.
In either case, there are no other measuring devices available to us.
ANS:-
I will answer the second problem here and I will give the code which you can run yourself and improve it if you want.
One solution to the second problem is
8-0-0, 3-5-0, 3-2-3, 6-2-0, 6-0-2, 1-5-2, 1-4-3, 4-4-0.
let us consider X-Y-Z as 8-5-3 jugs
The code is :-
#include<iostream>
using namespace std;
int main()
{
int limX = 8, limY = 5, limZ = 3;
int x = 8, y = 0, z = 0;
int a,b,c;
for(int i = 0; i < 2; i++)
{
// Minus water from x
if((x - (limY - y)) >= 0)
{
a = x - (x - (limY - y));
y += a; // Add Required water
x -= a;
}
else
{
y += x;
x = 0;
}
if((y - (limZ - z)) >= 0)
{
b = y - (y - (limZ - z));
z += b;
y -= b;
}
else
{
z += y;
y = 0;
}
if((z - (limX - x)) >= 0)
{
c = z - (z - (limX - x));
x += c;
z -= c;
}
else
{
x += z;
z = 0;
}
if(i>0)
{
break;
}
else
{
z = y;
y = 0;
}
}
cout<<x<<y<<z;
}
Comments
Post a Comment