-
Notifications
You must be signed in to change notification settings - Fork 0
/
waterjugbfs.java
128 lines (92 loc) · 3.39 KB
/
waterjugbfs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sem5collexpt;
/**
*
* @author Rishabh
*/
import java.util.*;
public class waterjugbfs {
public static int total = 0;
class Node {
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
void solveBFS(int jug1, int jug2, int target) {
int m[][] = new int[5][5];
for (int[] i : m) {
Arrays.fill(i, -1);
}
boolean isSolveable = false;
Vector<Node> result = new Vector<>();
Queue<Node> q = new LinkedList<>();
// add the starting position
q.add(new Node(0, 0));
while (!q.isEmpty()) {
Node temp = q.poll();
// Out of capacity
if (temp.x > jug1 || temp.x < 0 || temp.y > jug2 || temp.y < 0) {
continue;
}
total++;
// already visited node
if (m[temp.x][temp.y] > -1)
continue;
// add to the result
result.add(new Node(temp.x, temp.y));
m[temp.x][temp.y] = 1;// Mark as a visited node
// reach to the target
if (temp.x == target || temp.y == target) {
isSolveable = true;
if (temp.x == target && temp.y != 0) {
// add the final state node by transferring water
System.out.println("222( " + temp.x + "," + "0)");
result.add(new Node(temp.x, 0));
} else if (temp.y == target && temp.x != 0) {
// add the final state node by transferring water
System.out.println("111( 0" + "," + temp.y + ")");
result.add(new Node(0, temp.y));
}
for (int i = 0; i < result.size(); i++) {
System.out.println("(" + result.get(i).x + "," + result.get(i).y + ")");
}
break;
}
// fill the second jug
q.add(new Node(temp.x, jug2));
// fill the first jug
q.add(new Node(jug1, temp.y));
for (int j = 0; j <= Math.max(jug1, jug2); j++) {
int state1 = temp.x + j;
int state2 = temp.y - j;
if (state1 == jug1 || (state2 == 0 && state2 >= 0)) {
q.add(new Node(state1, state2));
}
state1 = temp.x - j;
state2 = temp.y + j;
if ((state1 == 0 && state1 >= 0) || state2 == jug2) {
q.add(new Node(state1, state2));
}
}
// empty the second jug
q.add(new Node(jug1, 0));
// empty the first jug
q.add(new Node(0, jug2));
}
if (!isSolveable) {
System.out.println("No solution");
}
}
public static void main(String[] args) {
waterjugbfs obj = new waterjugbfs();
obj.solveBFS(4, 3, 2);
System.out.println("Total nodes: " + (total + 1));
}
}