Water Jug Drawback Synthetic Intelligence
/* Water Jug Drawback
This downside is mainly asking you to carry out a Breadth First Search on
a directed graph. The nodes within the graph corresponds to a “state”, and
a state is mainly a pair (n, m) the place n and m are the quantity of water
within the jugs. There’s an edge from (n, m) to (n1, m1) if a sound operation
exists such that the results of the operation is (n1, m1).
The implementation for BFS is finished by utilizing queue. Additionally, a state (n, m)
is translated into an integer i for comfort.
*/
Code For Water Jug Drawback
#embody
#embody
#embody
class Queue
{
public:
Queue()
: head(NULL), tail(NULL)
{
}
void enqueue(int i)
{
if (head == NULL)
head = tail = new Node(i, NULL);
else
tail = tail->subsequent = new Node(i, NULL);
}
int dequeue()
{
Node* previous = head;
head = head->subsequent;
int i = old->i;
delete previous;
return i;
}
int isEmpty()
{
return (head == NULL);
}
Queue()
{
whereas (!isEmpty())
dequeue();
}
personal:
struct Node
{
int i;
Node* subsequent;
Node(int iP, Node* nextP)
: i(iP), subsequent(nextP)
{
}
} *head, *tail;
} iQueue;
const int MAX = 100;
const int MAX_I = (MAX + 1) * (MAX + 1);
int N, M, okay, n, m;
int distance[MAX_I];
int prev[MAX_I];
int nmToI(int n, int m)
{
return n * (M + 1) + m;
}
int iToN(int i)
{
return i / (M + 1);
}
int iToM(int i)
{
return i % (M + 1);
}
void hint(int i)
” <
void check(int n, int m, int n1, int m1)
int remedy()
{
n = m = 0;
distance[0] = 1;
iQueue.enqueue(0);
whereas (!iQueue.isEmpty())
return -1;
}
<
span lang="EN-US">void foremost()
{
clrscr();
cout<<"Please enter the variety of gallons in first jug: ";
cin>>N;
cout<<"Please enter the variety of gallons in second jug: ";
cin>>M;
cout<<"Please enter the vol. of water to be left lastly: ";
cin>>okay;
int i = remedy();
cout<<" JUG 1 "<<" JUG 2 n";
cout<<"----------------n";
if (i == -1)
cout << 0 << "n";
else
{
cout << distance[i] << "n";
hint(i);
}
cout << -1 << "n";
getch();
}
OUTPUT
Please enter the variety of gallons in first jug: 5
Please enter the variety of gallons in second jug: 3
Please enter the vol. of water to be left lastly: 4
JUG 1 JUG 2
—————-
7
0 | 0
5 | 0
2 | 3
2 | 0
0 | 2
5 | 2
4 | 3
-1
Supply projectgeek.com