• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Water Jug Problem Artificial Intelligence

September 3, 2011 by ProjectsGeek 6 Comments

Water Jug Problem Artificial Intelligence 


/* Water Jug Problem
This problem is basically asking you to perform a Breadth First Search on
a directed graph. The nodes in the graph corresponds to a “state”, and
a state is basically a pair (n, m) where n and m are the volume of water
in the jugs. There’s an edge from (n, m) to (n1, m1) if a valid operation
exists such that the result of the operation is (n1, m1).
The implementation for BFS is done by using queue. Also, a state (n, m)
is translated into an integer i for convenience.
*/
Code For  Water Jug Problem


#include
#include
#include
class Queue
{
            public:
                        Queue()
                        : head(NULL), tail(NULL)
                        {
                        }
                        void enqueue(int i)
                        {
                                    if (head == NULL)
                                                head = tail = new Node(i, NULL);
                                    else
                                                tail = tail->next = new Node(i, NULL);
                        }
                        int dequeue()
                        {
                                    Node* old = head;
                                    head = head->next;
                                    int i = old->i;
                                    delete old;
                                    return i;
                        }
                        int isEmpty()
                        {
                                    return (head == NULL);
                        }
                        ~Queue()
                        {
                                    while (!isEmpty())
                                    dequeue();
                        }
            private:
                        struct Node
                        {
                                    int i;
                                    Node* next;
                                    Node(int iP, Node* nextP)
                                    : i(iP), next(nextP)
                                    {
                                    }
                        } *head, *tail;
} iQueue;
const int MAX = 100;
const int MAX_I = (MAX + 1) * (MAX + 1);
int N, M, k, 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 trace(int i)
{
            if (i > 0)
                        trace(prev[i]);
            cout <<"    "<
}
void test(int n, int m, int n1, int m1)
{
            if (n1 < 0 || n1 > N || m1 < 0 || m1 > M)
                        return;
            int i1 = nmToI(n1, m1);
            if (distance[i1] != 0)
                        return;
            int i = nmToI(n, m);
            distance[i1] = distance[i] + 1;
            prev[i1] = i;
            iQueue.enqueue(i1);
}
int solve()
{
            n = m = 0;
            distance[0] = 1;
            iQueue.enqueue(0);
            while (!iQueue.isEmpty())
            {
                        int i = iQueue.dequeue();
                        int n = iToN(i);
                        int m = iToM(i);
                        if (n == k || m == k || n + m == k)
                        return i;
            // empty out a jug
                        test(n, m, 0, m);
                        test(n, m, n, 0);
            // fill a jug
                        test(n, m, N, m);
                        test(n, m, n, M);
            // pour one to another until source is empty
                        test(n, m, 0, n + m);
                        test(n, m, n + m, 0);
            // pour one to another until destination is full
                        test(n, m, n – M + m, M);
                        test(n, m, N, m – N + n);
            }
            return -1;
}
void main()
{
            clrscr();
            cout<<"Please enter the number of gallons in first jug:  ";
            cin>>N;
            cout<<"Please enter the number of gallons in second jug:  ";
            cin>>M;
            cout<<"Please enter the vol. of water to be left finally: ";
            cin>>k;
            int i = solve();
            cout<<"  JUG 1  "<<"  JUG 2 \n";
            cout<<"----------------\n";
            if (i == -1)
                        cout << 0 << "\n";
            else
            {
                        cout << distance[i] << "\n";
                        trace(i);
            }
            cout << -1 << "\n";
            getch();
}
OUTPUT
Please enter the number of gallons in first jug:  5
Please enter the number of gallons in second jug:  3
Please enter the vol. of water to be left finally: 4
  JUG 1    JUG 2
   —————-
7
    0     |   0
    5     |   0
    2     |   3
    2     |   0
    0     |   2
    5     |   2
    4     |   3
-1

Other Projects to Try:

  1. BFS AND DFS Algorithm using C Language
  2. Regular Expression to DFA Code in C Language
  3. Breadth First and Depth First Search C Language
  4. Hoffmans algorithm in C Language
  5. Single Link List code using C Language

Filed Under: Artificial Intelligence Problems

Reader Interactions

Comments

  1. Tutu says

    September 23, 2015 at 9:01 am

    some other errors like cout was not declared in this scope
    Line Col
    126 33 [Error] reference to ‘distance’ is ambiguous
    112 20 [Error] ‘clrscr’ was not declared in this scope
    130 19 [Error] ‘getch’ was not declared in this scope

    Reply
    • ProjectsGeek says

      November 1, 2015 at 12:09 pm

      These all are in build functions of C++. Please try below includes
      #include
      #include

  2. Tutu says

    September 23, 2015 at 8:38 am

    Hi, I can’t run the code, can you let me know which header to include?

    Reply
    • ProjectsGeek says

      November 1, 2015 at 12:07 pm

      Could you please explain what error message you are getting here ?

  3. Milan says

    July 19, 2015 at 6:09 am

    This code is not working..

    Reply
    • ProjectsGeek says

      July 30, 2015 at 2:56 pm

      Please provide us the exact error message you are getting while running this code. So that we can help you out.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy