Question

In: Computer Science

The purpose of this problem is to gain familiarity with stacks and queues. You have three...

The purpose of this problem is to gain familiarity with stacks and queues. You have three jugs that can hold c1, c2, and c3 liters of water, respectively. Initially, jug 1 is full and the other two jugs are empty. You can repeat the following procedure any number of times: Choose two of the jugs and pour the contents of one into the other until either the first is empty or the second is full. Your goal is to end up with exactly d liters in one of the jugs. Make a program called WaterTransfer.java to determine the transfers required to reach the goal. The input is a single line containing four integers between 2 and 100 (inclusive) representing c1, c2, c3, and d. The output is a minimal sequence of jug contents, starting with the initial contents and ending with one of the jugs containing d liters. Each line of the output should consist of 3 integers separated by spaces. If no solution exists, then your program should produce no output.

Good test case: 10 5 3 4 ; from movie ”Die hard: with a vengeance”; seehttps://www.youtube.com/watch?v=6cAbgAaEOVE

For example, if the input is 20 5 3 4t hen a valid output is

20 0 0

15 5 0

15 2 3

18 2 0

18 0 2

13 5 2

13 4 3

There may be other solutions, but none with fewer transfers (6) than this one. (this code should probably use queues)

Solutions

Expert Solution

Java Code:

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Stack;

import java.util.Scanner;

public class WaterJugs

{

public static void main(String args[])

{

Queue<State> q = new LinkedList<State>();

Stack<State> st = new Stack<State>();

ArrayList<State> visited = new ArrayList<State>();

System.out.println("Enter Input Values: ");

Scanner scan = new Scanner(System.in);

int[] cap = new int[3];

for (int i = 0; i < 3; ++i)

{

cap[i] = scan.nextInt();

}

int d = scan.nextInt();

State start = new State(cap);

State.setCapacities(cap);

visited.add(start);

q.add(start);

visited.add(start);

while(q.peek() != null)

{

State next = q.poll();

if (next.reachedGoal(d)) {

for (State x = next; x != null; x = x.pred)

st.push(x);

for (State y = next; y != null; y = y.pred)

{

st.pop().display();

}

break;

}

for(int i = 0; i <= 2; i++)

for(int j = 0; j <= 2; j++)

{

if(j == i) continue;

State p = next.move(i, j);

if(p == null || visited.contains(p)) continue;

visited.add(p);

q.add(p);

}

}

}

}

class State

{

static int[] capacity;

int[] contents;

State pred;

static void setCapacities(int[] c)

{

capacity = c;

}

State(int[] c) {

contents = new int[3];

contents[0] = c[0];

contents[1] = 0;

contents[2] = 0;

}

public State(State state)

{

contents = new int[3];

contents[0] = state.contents[0];

contents[1] = state.contents[1];

contents[2] = state.contents[2];

}

State move(int src, int dest)

{

State newState = new State(this);

if((contents[src] == 0) || (contents[dest] == capacity[dest])==true)

return null;

else{

int diff = capacity[dest] - newState.contents[dest];

if(newState.contents[src] >= diff)

{

newState.contents[dest] += diff;

newState.contents[src] -= diff;

}

else

{

if(newState.contents[dest] + newState.contents[src] > capacity[dest]) return null;

newState.contents[dest] += newState.contents[src];

newState.contents[src] = 0;

}

}

newState.pred = this;

return newState;

}

boolean reachedGoal(int d)

{

return(contents[0] == d || contents[1] == d || contents[2] == d);

}

void display()

{

System.out.println(contents[0] + " " + contents[1] + " " + contents[2]);

}

}

OUTPUT:


Related Solutions

The purpose of this problem is to gain familiarity with the dictionary ADT. A homophone is...
The purpose of this problem is to gain familiarity with the dictionary ADT. A homophone is one of two or more words that are pronounced alike but are different in meaning or spelling; for example, the words “two”, “too”, and “to”. Write a Java program that uses one of the implementations of the dictionary interface (UALDictionary or OALDictionary on Moodle) to find the word that has the most homophones. Do not use data structures that we have not covered so...
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
Difference between stacks and queues? Explain in your own words.
Difference between stacks and queues? Explain in your own words.
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write...
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the...
word level palindrome program with stacks and queues I have a good portion of my program...
word level palindrome program with stacks and queues I have a good portion of my program finished, I am just trying to figure out how to do some final things to it. I want the program to only check for letters a through z and A = Z, treating uppercase and lower case as the same thing. I want it to ignore any other characters and to just realize word level palindromes, not sentence level, so for example a%345b@a c24&c8d)9cc...
Please post screenshots of outputs. Also make sure to use stacks and queues. You will design...
Please post screenshots of outputs. Also make sure to use stacks and queues. You will design a program to keep track of a restaurants waitlist using a queue implemented with a linked list. Make sure to read pages 1215-1217 and 1227-1251 1. Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables. 2. Add the following operations to your program: a. Return the first person in the queue...
JAVA- List, Stacks, Queues, Sets, And Maps Question 1 Given that you want to be able...
JAVA- List, Stacks, Queues, Sets, And Maps Question 1 Given that you want to be able to return the items that come immediately before and after a given node. Which variation of a list would be best suited? 1. Circular single-linked list 2. All options are equally good 3. Double linked list 4. Single linked list with header and trailer nodes 5. Single-linked list Question 2 One of the statements is correct for stacks and queues 1. Both data structures...
Internal implementations of stacks and queues require the tracking of more information than simply the data...
Internal implementations of stacks and queues require the tracking of more information than simply the data being stored in the stack or queue. What is the nature of this information and why does it need to be stored? Does it differ between the objects? If so, how and why does it differ? If not, why can it be the same across object types?
Array-based implementations of stacks and queues require the knowing the location of data within the arrays....
Array-based implementations of stacks and queues require the knowing the location of data within the arrays. However, these two data structures store data differently. What is this difference and why does it exist? Could the stack-approach be used to store information within queues (or the other way around)? Why or why not? If it is possible, what would be the side effects of such an approach?
Programming Project – Deques, Stacks & Queues General Description: Design and develop array based and linked...
Programming Project – Deques, Stacks & Queues General Description: Design and develop array based and linked list-based implementations for the Dequeue ADT. Your implementation must support generic data types using C++ templates. Develop Adapter Files to provide Stack and Queue functionality for the Deques. Definitions: You should implement the ADTs precisely as described in the following partial header files. Deque.h template class Deque { public:         Deque();                    //constructor         ~Deque();                 //destructor         void insertFront(const E& e);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT