/
8-Puzzle_BFS
303 lines (235 loc) · 6.01 KB
/
8-Puzzle_BFS
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package com.puzzle;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
public class Puzzle {
public static class Pair{
int i;
int j;
Pair(int i, int j){
this.i=i;
this.j=j;
}
@Override
public String toString() {
return "Pair [i=" + i + ", j=" + j + "]";
}
}
public static class State{
@Override
public String toString() {
return "State [currBlank=" + currBlank + ", state=" + Arrays.toString(state) + ", parentBlank="
+ parentBlank + "]";
}
Pair currBlank;
int[][] state=new int[3][3];
Pair parentBlank;
State(Pair currBlank, int[][] state, Pair parentBlank){
//currBlank
this.currBlank=currBlank;
//array
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
this.state[i][j]=state[i][j];
}
}
//parentBlank
this.parentBlank=parentBlank;
}
}
public static int checkIfGoalFound(int[][] arr, int[][] goal) {
int rows=arr.length;
int cols=arr[0].length;
for(int i=0; i<rows; i++) {
for(int j=0; j<arr[0].length; j++) {
if(arr[i][j]!=goal[i][j]) {
return 0;
}
}
}
return 1;
}
public static void solve(int[][] arr, int[][] endGoal, Pair startPos) {
Queue<State> q=new ArrayDeque<>();
q.add(new State(startPos, arr, new Pair(-1, -1)));
int iter=0;
while(q.size()!=0) {
State s=q.peek();
//parent Blank
Pair currBlank=s.currBlank;
int rows=s.state.length;
int cols=s.state[0].length;
//check if goal is found
// int res=checkIfGoalFound(s.state, endGoal);
// if(res==1) {
// System.out.println("Goal Found");
// System.out.println("iter: "+iter);
// break;
// }
//print array
System.out.println("iter: "+(iter+1));
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
System.out.print(s.state[i][j]+" ");
}
System.out.println();
}
System.out.println(s.toString());
System.out.println();
//parent blank
Pair parentBlank=s.parentBlank;
//find neighbors
//left
if(currBlank.j!=0) {
if(currBlank.j-1 == parentBlank.j && currBlank.i == parentBlank.i) {
//do nothing
}
else {
Pair pos=new Pair(currBlank.i, currBlank.j-1);
//swap respective position
int temp[][]=new int[rows][cols];
//current array
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
temp[i][j]=s.state[i][j];
}
}
int swap=temp[currBlank.i][currBlank.j];
temp[currBlank.i][currBlank.j]=temp[currBlank.i][currBlank.j-1];
temp[currBlank.i][currBlank.j-1]=swap;
//check goal
int res=checkIfGoalFound(temp, endGoal);
if(res==1) {
System.out.println("Goal Found");
System.out.println("iter: "+iter);
break;
}
Pair parent;
if(iter==0) {
parent=currBlank;
}
else {
parent=parentBlank;
}
q.offer(new State(pos, temp, parent));
}
}
//top
if(currBlank.i!=0) {
if(currBlank.i-1 == parentBlank.i && currBlank.j == parentBlank.j) {
//do nothing
}
else {
Pair pos=new Pair(currBlank.i-1, currBlank.j);
//swap respective position
int temp[][]=new int[rows][cols];
//current array
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
temp[i][j]=s.state[i][j];
}
}
int swap=temp[currBlank.i][currBlank.j];
temp[currBlank.i][currBlank.j]=temp[currBlank.i-1][currBlank.j];
temp[currBlank.i-1][currBlank.j]=swap;
//check goal
int res=checkIfGoalFound(temp, endGoal);
if(res==1) {
System.out.println("Goal Found");
System.out.println("iter: "+iter);
break;
}
Pair parent;
if(iter==0) {
parent=currBlank;
}
else {
parent=parentBlank;
}
q.offer(new State(pos, temp, parent));
}
}
//bottom
if(currBlank.i!=rows-1) {
if(currBlank.i+1 == parentBlank.i && currBlank.j == parentBlank.j) {
//do nothing
}
else {
Pair pos=new Pair(currBlank.i+1, currBlank.j);
//swap respective position
int temp[][]=new int[rows][cols];
//current array
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
temp[i][j]=s.state[i][j];
}
}
int swap=temp[currBlank.i][currBlank.j];
temp[currBlank.i][currBlank.j]=temp[currBlank.i+1][currBlank.j];
temp[currBlank.i+1][currBlank.j]=swap;
//check goal
int res=checkIfGoalFound(temp, endGoal);
if(res==1) {
System.out.println("Goal Found");
System.out.println("iter: "+iter);
break;
}
Pair parent;
if(iter==0) {
parent=currBlank;
}
else {
parent=parentBlank;
}
q.offer(new State(pos, temp, parent));
}
}
//right
if(currBlank.j!=cols-1) {
if(currBlank.j+1!=parentBlank.j && currBlank.i==parentBlank.i) {
//do nothing
}
else {
Pair pos=new Pair(currBlank.i, currBlank.j+1);
//swap respective position
int temp[][]=new int[rows][cols];
//current array
for(int i=0; i<rows; i++) {
for(int j=0; j<cols; j++) {
temp[i][j]=s.state[i][j];
}
}
int swap=temp[currBlank.i][currBlank.j];
temp[currBlank.i][currBlank.j]=temp[currBlank.i][currBlank.j+1];
temp[currBlank.i][currBlank.j+1]=swap;
//check goal
int res=checkIfGoalFound(temp, endGoal);
if(res==1) {
System.out.println("Goal Found");
System.out.println("iter: "+iter);
break;
}
Pair parent;
if(iter==0) {
parent=currBlank;
}
else {
parent=parentBlank;
}
q.offer(new State(pos, temp, parent));
}
}
q.poll();
iter++;
}
}
public static void main(String[] args) {
int[][] arr= {{2, 8, 3},
{1, 6, 4},
{7, 0, 5}};
int[][] endGoal= {{1, 2, 3},
{8, 0, 4},
{7, 6, 5}};
solve(arr, endGoal, new Pair(2,1));
}
}