SlideShare a Scribd company logo
Change the value of jug1 and jug 2 accordingly. 1 File WaterJugBSF.java
import java.util.*;
import java.util.LinkedList;
class Test {
HashSet uniqueStates;
void letsRoll() {
/*
* The capacity of jug1(jug1 capacity is greater than jug2 capacity)
*/
int jug1 = 3;
/*
* The capacity of jug two
*/
int jug2 = 2;
/*
* Final Amount needed in jug1
*/
int amtNeeded = 2;
State initialState = new State(0, 0);
State finalState = new State(amtNeeded, 0);
State finalPath = null;
uniqueStates = new HashSet();
/*
* Change queue to stack to convert to DFS
*/
LinkedList queue = new LinkedList();
queue.add(initialState);
while (!queue.isEmpty()) {
State currState = queue.poll();
if (currState.equals(finalState)) {
finalPath = currState;
break;
}
// if x is zero fill it
if (currState.x == 0) {
State nextState = new State(jug1, currState.y, currState);
//System.out.println(currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
// if y is empty fill it
if (currState.y == 0) {
State nextState = new State(currState.x, jug2, currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
// if x is not empty , empty it
if (currState.x > 0) {
State nextState = new State(0, currState.y, currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
// if y is not empty, empty it
if (currState.y > 0) {
State nextState = new State(currState.x, 0, currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
// transfer from x to y, when x non empty and y is not full
if (currState.x > 0 && currState.y < jug2) {
int amtToTransfer = Math.min(currState.x, jug2 - currState.y);
State nextState = new State(currState.x - amtToTransfer, currState.y
+ amtToTransfer,
currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
// transfer from y to x, when y is not empty and x is not full
if (currState.y > 0 && currState.x < jug1) {
int amtToTransfer = Math.min(currState.y, jug1 - currState.x);
State nextState = new State(currState.x + amtToTransfer, currState.y
- amtToTransfer,
currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
}
if (finalPath != null) {
System.out.println("Final Result");
System.out.println("J1 J2");
System.out.println(finalPath);
}
else{
System.out.println("Not Possible");
}
}
/*
* Checks whether State toCheck has been generated before, if not it is
* added to queue and uniqueStates Set
*/
void checkUniqueStates(HashSet uniqueStates, State toCheck,
LinkedList queue) {
if (!uniqueStates.contains(toCheck)) {
uniqueStates.add(toCheck);
queue.add(toCheck);
}
}
public static void main(String[] args) {
new Test().letsRoll();
}
}
2 State.java
class State {
/*
* Amount in jug1 for current State
*/
int x;
/*
* Amount in jug2 for current State
*/
int y;
/*
* Parent of current State
*/
State pre;
public State(int x, int y) {
super();
this.x = x;
this.y = y;
}
public State(int x, int y, State pre) {
super();
this.x = x;
this.y = y;
this.pre = pre;
System.out.println("("+x+","+y+")");
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
State other = (State) obj;
if (x != other.x) {
return false;
}
if (y != other.y) {
return false;
}
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (pre != null) {
builder.append(pre);
}
builder.append(x);
builder.append(" ").append(y).append(" ");
return builder.toString();
}
}
Solution
Change the value of jug1 and jug 2 accordingly. 1 File WaterJugBSF.java
import java.util.*;
import java.util.LinkedList;
class Test {
HashSet uniqueStates;
void letsRoll() {
/*
* The capacity of jug1(jug1 capacity is greater than jug2 capacity)
*/
int jug1 = 3;
/*
* The capacity of jug two
*/
int jug2 = 2;
/*
* Final Amount needed in jug1
*/
int amtNeeded = 2;
State initialState = new State(0, 0);
State finalState = new State(amtNeeded, 0);
State finalPath = null;
uniqueStates = new HashSet();
/*
* Change queue to stack to convert to DFS
*/
LinkedList queue = new LinkedList();
queue.add(initialState);
while (!queue.isEmpty()) {
State currState = queue.poll();
if (currState.equals(finalState)) {
finalPath = currState;
break;
}
// if x is zero fill it
if (currState.x == 0) {
State nextState = new State(jug1, currState.y, currState);
//System.out.println(currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
// if y is empty fill it
if (currState.y == 0) {
State nextState = new State(currState.x, jug2, currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
// if x is not empty , empty it
if (currState.x > 0) {
State nextState = new State(0, currState.y, currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
// if y is not empty, empty it
if (currState.y > 0) {
State nextState = new State(currState.x, 0, currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
// transfer from x to y, when x non empty and y is not full
if (currState.x > 0 && currState.y < jug2) {
int amtToTransfer = Math.min(currState.x, jug2 - currState.y);
State nextState = new State(currState.x - amtToTransfer, currState.y
+ amtToTransfer,
currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
// transfer from y to x, when y is not empty and x is not full
if (currState.y > 0 && currState.x < jug1) {
int amtToTransfer = Math.min(currState.y, jug1 - currState.x);
State nextState = new State(currState.x + amtToTransfer, currState.y
- amtToTransfer,
currState);
checkUniqueStates(uniqueStates, nextState, queue);
}
}
if (finalPath != null) {
System.out.println("Final Result");
System.out.println("J1 J2");
System.out.println(finalPath);
}
else{
System.out.println("Not Possible");
}
}
/*
* Checks whether State toCheck has been generated before, if not it is
* added to queue and uniqueStates Set
*/
void checkUniqueStates(HashSet uniqueStates, State toCheck,
LinkedList queue) {
if (!uniqueStates.contains(toCheck)) {
uniqueStates.add(toCheck);
queue.add(toCheck);
}
}
public static void main(String[] args) {
new Test().letsRoll();
}
}
2 State.java
class State {
/*
* Amount in jug1 for current State
*/
int x;
/*
* Amount in jug2 for current State
*/
int y;
/*
* Parent of current State
*/
State pre;
public State(int x, int y) {
super();
this.x = x;
this.y = y;
}
public State(int x, int y, State pre) {
super();
this.x = x;
this.y = y;
this.pre = pre;
System.out.println("("+x+","+y+")");
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
State other = (State) obj;
if (x != other.x) {
return false;
}
if (y != other.y) {
return false;
}
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (pre != null) {
builder.append(pre);
}
builder.append(x);
builder.append(" ").append(y).append(" ");
return builder.toString();
}
}

More Related Content

Similar to Change the value of jug1 and jug 2 accordingly. 1 File WaterJugBSF.j.pdf

I need to write a java program about translating the DNA sequenc.pdf
I need to write a java program about translating the DNA sequenc.pdfI need to write a java program about translating the DNA sequenc.pdf
I need to write a java program about translating the DNA sequenc.pdf
infoeyecare
 
Higher-Order Components — Ilya Gelman
Higher-Order Components — Ilya GelmanHigher-Order Components — Ilya Gelman
Higher-Order Components — Ilya Gelman
500Tech
 
Максим Климишин "Борьба с асинхронностью в JS"
Максим Климишин "Борьба с асинхронностью в JS"Максим Климишин "Борьба с асинхронностью в JS"
Максим Климишин "Борьба с асинхронностью в JS"
Fwdays
 
Fighting async JavaScript (CSP)
Fighting async JavaScript (CSP)Fighting async JavaScript (CSP)
Fighting async JavaScript (CSP)
Max Klymyshyn
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
curwenmichaela
 
What's new in jQuery 1.5
What's new in jQuery 1.5What's new in jQuery 1.5
What's new in jQuery 1.5
Martin Kleppe
 

Similar to Change the value of jug1 and jug 2 accordingly. 1 File WaterJugBSF.j.pdf (6)

I need to write a java program about translating the DNA sequenc.pdf
I need to write a java program about translating the DNA sequenc.pdfI need to write a java program about translating the DNA sequenc.pdf
I need to write a java program about translating the DNA sequenc.pdf
 
Higher-Order Components — Ilya Gelman
Higher-Order Components — Ilya GelmanHigher-Order Components — Ilya Gelman
Higher-Order Components — Ilya Gelman
 
Максим Климишин "Борьба с асинхронностью в JS"
Максим Климишин "Борьба с асинхронностью в JS"Максим Климишин "Борьба с асинхронностью в JS"
Максим Климишин "Борьба с асинхронностью в JS"
 
Fighting async JavaScript (CSP)
Fighting async JavaScript (CSP)Fighting async JavaScript (CSP)
Fighting async JavaScript (CSP)
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
 
What's new in jQuery 1.5
What's new in jQuery 1.5What's new in jQuery 1.5
What's new in jQuery 1.5
 

More from Lalkamal2

steps are as follows Dissolve the (pressumably.pdf
                     steps are as follows   Dissolve the (pressumably.pdf                     steps are as follows   Dissolve the (pressumably.pdf
steps are as follows Dissolve the (pressumably.pdf
Lalkamal2
 
the two features of the asynchronus connection that allow the receiv.pdf
the two features of the asynchronus connection that allow the receiv.pdfthe two features of the asynchronus connection that allow the receiv.pdf
the two features of the asynchronus connection that allow the receiv.pdf
Lalkamal2
 
Telomerase is the enzyme responsible for maintenance of the length o.pdf
Telomerase is the enzyme responsible for maintenance of the length o.pdfTelomerase is the enzyme responsible for maintenance of the length o.pdf
Telomerase is the enzyme responsible for maintenance of the length o.pdf
Lalkamal2
 
Suppose Bob sends an encrypted document to Alice. To be verifiable, .pdf
Suppose Bob sends an encrypted document to Alice. To be verifiable, .pdfSuppose Bob sends an encrypted document to Alice. To be verifiable, .pdf
Suppose Bob sends an encrypted document to Alice. To be verifiable, .pdf
Lalkamal2
 
probablity = 212 100 = 16.6 Solutionprobablity = 212 10.pdf
probablity = 212  100 = 16.6 Solutionprobablity = 212  10.pdfprobablity = 212  100 = 16.6 Solutionprobablity = 212  10.pdf
probablity = 212 100 = 16.6 Solutionprobablity = 212 10.pdf
Lalkamal2
 
Platinum complexes like Asplastin are powerful antitumor medications.pdf
Platinum complexes like Asplastin are powerful antitumor medications.pdfPlatinum complexes like Asplastin are powerful antitumor medications.pdf
Platinum complexes like Asplastin are powerful antitumor medications.pdf
Lalkamal2
 
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdfPart 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Lalkamal2
 
none of them are correct!Solutionnone of them are correct!.pdf
none of them are correct!Solutionnone of them are correct!.pdfnone of them are correct!Solutionnone of them are correct!.pdf
none of them are correct!Solutionnone of them are correct!.pdf
Lalkamal2
 
notor^Solutionnotor^.pdf
notor^Solutionnotor^.pdfnotor^Solutionnotor^.pdf
notor^Solutionnotor^.pdf
Lalkamal2
 
Negative control plate with no DNA transforms competent cells withou.pdf
Negative control plate with no DNA transforms competent cells withou.pdfNegative control plate with no DNA transforms competent cells withou.pdf
Negative control plate with no DNA transforms competent cells withou.pdf
Lalkamal2
 
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdfModifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Lalkamal2
 
Message types used by the DHCP boot sequenceop Message type.h.pdf
Message types used by the DHCP boot sequenceop Message type.h.pdfMessage types used by the DHCP boot sequenceop Message type.h.pdf
Message types used by the DHCP boot sequenceop Message type.h.pdf
Lalkamal2
 
Law of conservation of matter(mass)In any closed system subjected .pdf
Law of conservation of matter(mass)In any closed system subjected .pdfLaw of conservation of matter(mass)In any closed system subjected .pdf
Law of conservation of matter(mass)In any closed system subjected .pdf
Lalkamal2
 
It is a unit of measurement for the amount of substance.The mole is .pdf
It is a unit of measurement for the amount of substance.The mole is .pdfIt is a unit of measurement for the amount of substance.The mole is .pdf
It is a unit of measurement for the amount of substance.The mole is .pdf
Lalkamal2
 
In insurance, the term risk pooling refers to the spreading of f.pdf
In insurance, the term risk pooling refers to the spreading of f.pdfIn insurance, the term risk pooling refers to the spreading of f.pdf
In insurance, the term risk pooling refers to the spreading of f.pdf
Lalkamal2
 
Some CO is Added The Pressure of CH4 and H2O will.pdf
                     Some CO is Added The Pressure of CH4 and H2O will.pdf                     Some CO is Added The Pressure of CH4 and H2O will.pdf
Some CO is Added The Pressure of CH4 and H2O will.pdf
Lalkamal2
 
dasSolutiondas.pdf
dasSolutiondas.pdfdasSolutiondas.pdf
dasSolutiondas.pdf
Lalkamal2
 
Comments Figures are not properly numbered. Further signal of figur.pdf
Comments Figures are not properly numbered. Further signal of figur.pdfComments Figures are not properly numbered. Further signal of figur.pdf
Comments Figures are not properly numbered. Further signal of figur.pdf
Lalkamal2
 
Balance sheetBalance sheet is the statement of financial position .pdf
Balance sheetBalance sheet is the statement of financial position .pdfBalance sheetBalance sheet is the statement of financial position .pdf
Balance sheetBalance sheet is the statement of financial position .pdf
Lalkamal2
 
Answer BaSO4K+ is always soluble because it is an alkaline metal..pdf
Answer BaSO4K+ is always soluble because it is an alkaline metal..pdfAnswer BaSO4K+ is always soluble because it is an alkaline metal..pdf
Answer BaSO4K+ is always soluble because it is an alkaline metal..pdf
Lalkamal2
 

More from Lalkamal2 (20)

steps are as follows Dissolve the (pressumably.pdf
                     steps are as follows   Dissolve the (pressumably.pdf                     steps are as follows   Dissolve the (pressumably.pdf
steps are as follows Dissolve the (pressumably.pdf
 
the two features of the asynchronus connection that allow the receiv.pdf
the two features of the asynchronus connection that allow the receiv.pdfthe two features of the asynchronus connection that allow the receiv.pdf
the two features of the asynchronus connection that allow the receiv.pdf
 
Telomerase is the enzyme responsible for maintenance of the length o.pdf
Telomerase is the enzyme responsible for maintenance of the length o.pdfTelomerase is the enzyme responsible for maintenance of the length o.pdf
Telomerase is the enzyme responsible for maintenance of the length o.pdf
 
Suppose Bob sends an encrypted document to Alice. To be verifiable, .pdf
Suppose Bob sends an encrypted document to Alice. To be verifiable, .pdfSuppose Bob sends an encrypted document to Alice. To be verifiable, .pdf
Suppose Bob sends an encrypted document to Alice. To be verifiable, .pdf
 
probablity = 212 100 = 16.6 Solutionprobablity = 212 10.pdf
probablity = 212  100 = 16.6 Solutionprobablity = 212  10.pdfprobablity = 212  100 = 16.6 Solutionprobablity = 212  10.pdf
probablity = 212 100 = 16.6 Solutionprobablity = 212 10.pdf
 
Platinum complexes like Asplastin are powerful antitumor medications.pdf
Platinum complexes like Asplastin are powerful antitumor medications.pdfPlatinum complexes like Asplastin are powerful antitumor medications.pdf
Platinum complexes like Asplastin are powerful antitumor medications.pdf
 
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdfPart 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
 
none of them are correct!Solutionnone of them are correct!.pdf
none of them are correct!Solutionnone of them are correct!.pdfnone of them are correct!Solutionnone of them are correct!.pdf
none of them are correct!Solutionnone of them are correct!.pdf
 
notor^Solutionnotor^.pdf
notor^Solutionnotor^.pdfnotor^Solutionnotor^.pdf
notor^Solutionnotor^.pdf
 
Negative control plate with no DNA transforms competent cells withou.pdf
Negative control plate with no DNA transforms competent cells withou.pdfNegative control plate with no DNA transforms competent cells withou.pdf
Negative control plate with no DNA transforms competent cells withou.pdf
 
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdfModifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
 
Message types used by the DHCP boot sequenceop Message type.h.pdf
Message types used by the DHCP boot sequenceop Message type.h.pdfMessage types used by the DHCP boot sequenceop Message type.h.pdf
Message types used by the DHCP boot sequenceop Message type.h.pdf
 
Law of conservation of matter(mass)In any closed system subjected .pdf
Law of conservation of matter(mass)In any closed system subjected .pdfLaw of conservation of matter(mass)In any closed system subjected .pdf
Law of conservation of matter(mass)In any closed system subjected .pdf
 
It is a unit of measurement for the amount of substance.The mole is .pdf
It is a unit of measurement for the amount of substance.The mole is .pdfIt is a unit of measurement for the amount of substance.The mole is .pdf
It is a unit of measurement for the amount of substance.The mole is .pdf
 
In insurance, the term risk pooling refers to the spreading of f.pdf
In insurance, the term risk pooling refers to the spreading of f.pdfIn insurance, the term risk pooling refers to the spreading of f.pdf
In insurance, the term risk pooling refers to the spreading of f.pdf
 
Some CO is Added The Pressure of CH4 and H2O will.pdf
                     Some CO is Added The Pressure of CH4 and H2O will.pdf                     Some CO is Added The Pressure of CH4 and H2O will.pdf
Some CO is Added The Pressure of CH4 and H2O will.pdf
 
dasSolutiondas.pdf
dasSolutiondas.pdfdasSolutiondas.pdf
dasSolutiondas.pdf
 
Comments Figures are not properly numbered. Further signal of figur.pdf
Comments Figures are not properly numbered. Further signal of figur.pdfComments Figures are not properly numbered. Further signal of figur.pdf
Comments Figures are not properly numbered. Further signal of figur.pdf
 
Balance sheetBalance sheet is the statement of financial position .pdf
Balance sheetBalance sheet is the statement of financial position .pdfBalance sheetBalance sheet is the statement of financial position .pdf
Balance sheetBalance sheet is the statement of financial position .pdf
 
Answer BaSO4K+ is always soluble because it is an alkaline metal..pdf
Answer BaSO4K+ is always soluble because it is an alkaline metal..pdfAnswer BaSO4K+ is always soluble because it is an alkaline metal..pdf
Answer BaSO4K+ is always soluble because it is an alkaline metal..pdf
 

Recently uploaded

Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 

Recently uploaded (20)

Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 

Change the value of jug1 and jug 2 accordingly. 1 File WaterJugBSF.j.pdf

  • 1. Change the value of jug1 and jug 2 accordingly. 1 File WaterJugBSF.java import java.util.*; import java.util.LinkedList; class Test { HashSet uniqueStates; void letsRoll() { /* * The capacity of jug1(jug1 capacity is greater than jug2 capacity) */ int jug1 = 3; /* * The capacity of jug two */ int jug2 = 2; /* * Final Amount needed in jug1 */ int amtNeeded = 2; State initialState = new State(0, 0); State finalState = new State(amtNeeded, 0); State finalPath = null; uniqueStates = new HashSet(); /* * Change queue to stack to convert to DFS */ LinkedList queue = new LinkedList(); queue.add(initialState); while (!queue.isEmpty()) { State currState = queue.poll(); if (currState.equals(finalState)) { finalPath = currState; break; } // if x is zero fill it
  • 2. if (currState.x == 0) { State nextState = new State(jug1, currState.y, currState); //System.out.println(currState); checkUniqueStates(uniqueStates, nextState, queue); } // if y is empty fill it if (currState.y == 0) { State nextState = new State(currState.x, jug2, currState); checkUniqueStates(uniqueStates, nextState, queue); } // if x is not empty , empty it if (currState.x > 0) { State nextState = new State(0, currState.y, currState); checkUniqueStates(uniqueStates, nextState, queue); } // if y is not empty, empty it if (currState.y > 0) { State nextState = new State(currState.x, 0, currState); checkUniqueStates(uniqueStates, nextState, queue); } // transfer from x to y, when x non empty and y is not full if (currState.x > 0 && currState.y < jug2) { int amtToTransfer = Math.min(currState.x, jug2 - currState.y); State nextState = new State(currState.x - amtToTransfer, currState.y + amtToTransfer, currState); checkUniqueStates(uniqueStates, nextState, queue); } // transfer from y to x, when y is not empty and x is not full if (currState.y > 0 && currState.x < jug1) { int amtToTransfer = Math.min(currState.y, jug1 - currState.x); State nextState = new State(currState.x + amtToTransfer, currState.y - amtToTransfer, currState); checkUniqueStates(uniqueStates, nextState, queue); }
  • 3. } if (finalPath != null) { System.out.println("Final Result"); System.out.println("J1 J2"); System.out.println(finalPath); } else{ System.out.println("Not Possible"); } } /* * Checks whether State toCheck has been generated before, if not it is * added to queue and uniqueStates Set */ void checkUniqueStates(HashSet uniqueStates, State toCheck, LinkedList queue) { if (!uniqueStates.contains(toCheck)) { uniqueStates.add(toCheck); queue.add(toCheck); } } public static void main(String[] args) { new Test().letsRoll(); } } 2 State.java class State { /* * Amount in jug1 for current State */ int x; /* * Amount in jug2 for current State */
  • 4. int y; /* * Parent of current State */ State pre; public State(int x, int y) { super(); this.x = x; this.y = y; } public State(int x, int y, State pre) { super(); this.x = x; this.y = y; this.pre = pre; System.out.println("("+x+","+y+")"); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + y; return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; }
  • 5. State other = (State) obj; if (x != other.x) { return false; } if (y != other.y) { return false; } return true; } @Override public String toString() { StringBuilder builder = new StringBuilder(); if (pre != null) { builder.append(pre); } builder.append(x); builder.append(" ").append(y).append(" "); return builder.toString(); } } Solution Change the value of jug1 and jug 2 accordingly. 1 File WaterJugBSF.java import java.util.*; import java.util.LinkedList; class Test { HashSet uniqueStates; void letsRoll() { /* * The capacity of jug1(jug1 capacity is greater than jug2 capacity) */ int jug1 = 3; /* * The capacity of jug two
  • 6. */ int jug2 = 2; /* * Final Amount needed in jug1 */ int amtNeeded = 2; State initialState = new State(0, 0); State finalState = new State(amtNeeded, 0); State finalPath = null; uniqueStates = new HashSet(); /* * Change queue to stack to convert to DFS */ LinkedList queue = new LinkedList(); queue.add(initialState); while (!queue.isEmpty()) { State currState = queue.poll(); if (currState.equals(finalState)) { finalPath = currState; break; } // if x is zero fill it if (currState.x == 0) { State nextState = new State(jug1, currState.y, currState); //System.out.println(currState); checkUniqueStates(uniqueStates, nextState, queue); } // if y is empty fill it if (currState.y == 0) { State nextState = new State(currState.x, jug2, currState); checkUniqueStates(uniqueStates, nextState, queue); } // if x is not empty , empty it if (currState.x > 0) { State nextState = new State(0, currState.y, currState); checkUniqueStates(uniqueStates, nextState, queue);
  • 7. } // if y is not empty, empty it if (currState.y > 0) { State nextState = new State(currState.x, 0, currState); checkUniqueStates(uniqueStates, nextState, queue); } // transfer from x to y, when x non empty and y is not full if (currState.x > 0 && currState.y < jug2) { int amtToTransfer = Math.min(currState.x, jug2 - currState.y); State nextState = new State(currState.x - amtToTransfer, currState.y + amtToTransfer, currState); checkUniqueStates(uniqueStates, nextState, queue); } // transfer from y to x, when y is not empty and x is not full if (currState.y > 0 && currState.x < jug1) { int amtToTransfer = Math.min(currState.y, jug1 - currState.x); State nextState = new State(currState.x + amtToTransfer, currState.y - amtToTransfer, currState); checkUniqueStates(uniqueStates, nextState, queue); } } if (finalPath != null) { System.out.println("Final Result"); System.out.println("J1 J2"); System.out.println(finalPath); } else{ System.out.println("Not Possible"); } } /* * Checks whether State toCheck has been generated before, if not it is
  • 8. * added to queue and uniqueStates Set */ void checkUniqueStates(HashSet uniqueStates, State toCheck, LinkedList queue) { if (!uniqueStates.contains(toCheck)) { uniqueStates.add(toCheck); queue.add(toCheck); } } public static void main(String[] args) { new Test().letsRoll(); } } 2 State.java class State { /* * Amount in jug1 for current State */ int x; /* * Amount in jug2 for current State */ int y; /* * Parent of current State */ State pre; public State(int x, int y) { super(); this.x = x; this.y = y; } public State(int x, int y, State pre) { super(); this.x = x; this.y = y;
  • 9. this.pre = pre; System.out.println("("+x+","+y+")"); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + y; return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } State other = (State) obj; if (x != other.x) { return false; } if (y != other.y) { return false; } return true; } @Override public String toString() { StringBuilder builder = new StringBuilder(); if (pre != null) { builder.append(pre);