Question

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.

Write 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

and another test case: 76 45 32 18

For example, if the input is

20 5 3 4

then 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.

Homework Answers

Answer #1

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:

Thank you Sir.....!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Suppose you have three stacks. Stack source is full of random data and stacks aux and...
Suppose you have three stacks. Stack source is full of random data and stacks aux and dest are empty To sort the data in the source, you can repeatedly remove the largest value by popping each item off of source and pushing it onto aux, remembering the maximum value seen as you empty source. When source is empty, you pop each item off of aux and push it back onto source except for the largest one that you remembered, which...
Vollmer Manufacturing makes three components for sale to refrigeration companies. The components are processed on two...
Vollmer Manufacturing makes three components for sale to refrigeration companies. The components are processed on two machines: a shaper and a grinder. The times (in minutes) required on each machine are as follows: Machine Component Shaper Grinder 1 6 4 2 4 5 3 4 2 The shaper is available for 120 hours, and the grinder for 110 hours. No more than 200 units of component 3 can be sold, but up to 1,000 units of each of the other...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the...
Use the following algorithm: (1) Create three stacks of characters: stk1, stk2, stk3. (2) Read the input infix expression one character at a time and push every character read ( other than ')' ) onto stk1. Do not push the character read if it is ')'. (3) As long as stk1 is not empty, do the following:            Fetch the top character ( call it c ) of stk1 and pop stk1.            if c is an alphabetic character (a-z),...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
language: JAVA the Problem Below are a series of problems you need to solve using recursive...
language: JAVA the Problem Below are a series of problems you need to solve using recursive methods. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck   Write a recursive method that checks whether an array of integers -...
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules...
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each disk will include some marker to denote to whom it belongs.  At the end...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
C# Lab 7A: Thank you, Grace Hopper. You may have heard the story, but the programming...
C# Lab 7A: Thank you, Grace Hopper. You may have heard the story, but the programming term “bug” is attributed to the computer legend Grace Hopper who, after diagnosing the problem with a program, determined it was because a bug (in this case, a moth) physically wedged itself under a vacuum tube – causing the machine to no longer work. We’ve come a long way since then... For this part of the lab, you are going to write a program,...
Problem Set 4 If you insulate your office for $10,000, you will save $1,000 a year...
Problem Set 4 If you insulate your office for $10,000, you will save $1,000 a year in heating expenses. These savings will last forever. What is the NPV of this investment when the cost of capital is 8%? 10%? What is the IRR? A project costs $5,000 at t = 0 and will generate annual cash flows of $750 for 10 years, starting at t = 1. The discount rate is 6%. What is the NPV? What is the IRR?...
USING C++ The purpose of this assignment is the use of 2-dimensional arrays, reading and writing...
USING C++ The purpose of this assignment is the use of 2-dimensional arrays, reading and writing text files, writing functions, and program planning and development. You will read a data file and store all of the input data in a two dimensional array. You will perform calculations on the data and store the results in the 2 dimensional array. You will sort the array and print the results in a report. Instructions You will read in the same input file...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT