-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanibell.java
173 lines (132 loc) · 5.6 KB
/
canibell.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.util.Vector;
class State{
public int C[] = new int[2], M[] = new int[2], B;
public State parent;
public String action="";
public State(){}
public State(int c1, int c2, int m1, int m2, int b, String act){
C[0] = c1; C[1] = c2; M[0] = m1; M[1] = m2; B = b;
action = act;
}
boolean isDanger(int c1, int c2, int m1, int m2){
if((c1 > m1 && m1 != 0) || (c2 > m2 && m2 !=0))
return true;
return false;
}
public Vector expandNode(){
Vector<State> children = new Vector<State>();
State newState;
// 2 Cann sitting in boat
if(C[B] >=2){
if (B == 0 && !isDanger(C[0] - 2, C[1] + 2, M[0], M[1])){
newState = new State(C[0] - 2, C[1] + 2, M[0], M[1], (B+1)%2, "2 Can in Boat at " + (B+1) );
newState.parent = this;
children.add(newState);
}else if(!isDanger(C[0] + 2 , C[1]- 2, M[0], M[1])){
newState = new State(C[0] + 2 , C[1]- 2, M[0], M[1], (B+1)%2, "2 Can in Boat at " + (B+1));
newState.parent = this;
children.add(newState);
}
}
// 2 Mis sitting in boat
if(M[B] >=2 ){
if (B == 0 && !isDanger(C[0] , C[1], M[0]- 2, M[1] + 2) ){
newState = new State(C[0] , C[1], M[0]- 2, M[1] + 2, (B+1)%2, "2 Mis in Boat at " + (B+1));
newState.parent = this;
children.add(newState);
}else if(!isDanger(C[0] , C[1], M[0] + 2, M[1]- 2)){
newState = new State(C[0] , C[1], M[0] + 2, M[1]- 2, (B+1)%2, "2 Mis in Boat at " + (B+1));
newState.parent = this;
children.add(newState);
}
}
// 1 Can 1 Mis sitting in boat
if(C[B] >= 1){
if (B == 0 && !isDanger(C[0] -1 , C[1] + 1, M[0]- 1, M[1]+1)){
newState = new State(C[0] -1 , C[1] + 1, M[0]- 1, M[1]+1, (B+1)%2, "1 can 1 Mis in Boat at " + (B+1));
newState.parent = this;
children.add(newState);
}else if (!isDanger(C[0] + 1, C[1]-1, M[0] +1 , M[1]- 1)){
newState = new State(C[0] + 1, C[1]-1, M[0] +1 , M[1]- 1, (B+1)%2, "1 Can 1 Mis in Boat at " + (B+1));
newState.parent = this;
children.add(newState);
}
}
// 1 Can sitting in boat
if(C[B] >= 1 ){
if (B == 0 && !isDanger(C[0] -1 , C[1] + 1, M[0], M[1])){
newState = new State(C[0] -1 , C[1] + 1, M[0], M[1], (B+1)%2, "1 can in Boat at " + (B+1));
newState.parent = this;
children.add(newState);
}else if(!isDanger(C[0]+ 1 , C[1]-1, M[0], M[1])){
newState = new State(C[0]+ 1 , C[1]-1, M[0], M[1], (B+1)%2, "1 Can in Boat at " + (B+1));
newState.parent = this;
children.add(newState);
}
}
// 1 Mis sitting in boat
if(M[B] >= 1 ){
if (B == 0 && !isDanger(C[0] , C[1], M[0]-1, M[1]+ 1) ){
newState = new State(C[0] , C[1], M[0]-1, M[1]+ 1, (B+1)%2, "1 Mis in Boat at " + (B+1));
newState.parent = this;
children.add(newState);
}else if(!isDanger(C[0] , C[1], M[0] + 1, M[1]-1) ){
newState = new State(C[0] , C[1], M[0] + 1, M[1]-1, (B+1)%2, "1 Mis in Boat at " + (B+1));
newState.parent = this;
children.add(newState);
}
}
return children;
}
public String toString(){
return "(C1 = " + C[0] + " M1 = " + M[0] + ")(C2 = " + C[1] + " M2 = " + M[1] + ")Boat = " + (B+1) ;//+ " Action = " + action;
}
public boolean isGoal(){
if(C[1] == 3 && M[1] == 3)
return true;
return false;
}
}
public class canibell {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
java.util.Stack<State> stack = new java.util.Stack<State>();
java.util.HashSet<State> seenStates = new java.util.HashSet<State>();
State start = new State(3,0,3,0,0,"start");
start.parent = null;
State end = null;
stack.push(start);
while( ! stack.empty()){
//System.out.println("Stack: "+stack);
//System.out.println("SeenSates: "+seenStates);
State temp = stack.remove(0);
//System.out.println("temp: "+temp);
//stack.remove(temp);
if(temp.isGoal()){
end = temp;
break;
}
seenStates.add(temp);
Vector<State> children = temp.expandNode();
//System.out.println("states:" + children );
Object objs[] = children.toArray();
for(int i=0;i<objs.length;i++){
State tempstate = (State)objs[i];
if( !seenStates.contains(tempstate))
stack.push(tempstate);
}
}
if(end == null)
System.out.println("Not Possible");
else{
System.out.println();
while (end != null){
System.out.print(" <--(" + end + ") <-" + end.action + " ");
end = end.parent;
}
}
}
}