-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlidingBreadthFirst.java
More file actions
204 lines (166 loc) · 7.29 KB
/
SlidingBreadthFirst.java
File metadata and controls
204 lines (166 loc) · 7.29 KB
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
package slidingboard;
import java.util.LinkedList;
public class SlidingBreadthFirst {
private class Board {
private int[][] array; //board array
private Board parent; //parent board
private Board(int[][] array, int n, int m) {
this.array = new int[n][m]; //create board array
for (int i = 0; i < n; i++) //copy given array
for (int j = 0; j < m; j++)
this.array[i][j] = array[i][j];
this.parent = null; //no parent
}
}
private final Board initial; //initial board
private final Board goal; //goal board
private final int n; //number of rows
private final int m; //number of columns
public SlidingBreadthFirst(int[][] initial, int[][] goal, int n, int m) {
this.initial = new Board(initial, n, m); //create initial board
this.goal = new Board(goal, n, m); //create final board
this.n = n;
this.m = m;
}
public void solve() {
LinkedList<Board> openList = new LinkedList<>(); //open list
LinkedList<Board> closedList = new LinkedList<>(); //closed list
openList.addFirst(initial); //add initial board to open list
while (!openList.isEmpty()) { //while open list has more boards
Board board = openList.removeFirst(); //remove first board from openlist
closedList.addLast(board); //add board to closed list
if (complete(board)) //if board is goal
{
System.out.println("Final path: \n");
displayPath(board);
System.out.println("States explored: " + closedList.size());
System.out.println("States waiting to be explored: " + openList.size());
return;
}
else
{
LinkedList<Board> children = generate(board); //create children
for (int i = 0; i < children.size(); i++)
{
Board child = children.get(i); //for each child
if (!exists(child, openList) && !exists(child, closedList)){
openList.addLast(child); //if it is not in open or
} //closed list then add to
} //end of open list
}
}
System.out.println("no solution"); //no solution if there are
} //no boards in open list
//Method creates children of a board
private LinkedList<Board> generate(Board board)
{
int i = 0, j = 0;
boolean found = false;
for (i = 0; i < n; i ++) //find location of empty slot
{ //of board
for (j = 0; j < m; j++){
if (board.array[i][j] == 0)
{
found = true;
break;
}
}
if (found)
break;
}
boolean north, south, east, west;
north = i == 0 ? false : true; //decide whether empty slot
south = i == n - 1 ? false : true; //has N, S, E, Q neighbors
east = j == m - 1 ? false : true;
west = j == 0 ? false : true;
LinkedList<Board> children = new LinkedList<>();
if (north) children.addLast(createChild(board, i, j, 'N')); //add N, S
if (south) children.addLast(createChild(board, i, j, 'S')); //E, W
if (east) children.addLast(createChild(board, i, j, 'E')); //children
if (west) children.addLast(createChild(board, i, j, 'W')); //if they
//exist
return children; //return children
}
//Method creates a child of a board byb swaping empty slot in a
//given direction
private Board createChild(Board board, int i , int j, char direction)
{
Board child = copy(board); //create copy of board
if (direction == 'N') //swap empty slot to north
{
child.array[i][j] = child.array[i-1][j];
child.array[i - 1][j] = 0;
}
else if (direction == 'S') //swap empty slot to south
{
child.array[i][j] = child.array[i + 1][j];
child.array[i + 1][j] = 0;
}
else if (direction == 'E') //swap empty slot to East
{
child.array[i][j] = child.array[i][j + 1];
child.array[i][j + 1] = 0;
}
else //swap empty slot to West
{
child.array[i][j] = child.array[i][j-1];
child.array[i][j - 1] = 0;
}
child.parent = board; //assign parent to child
return child; //return child
}
//Method creates copy of a board
private Board copy(Board board)
{
return new Board(board.array, n, m);
}
//Method decides whether a board is complete
private boolean complete(Board board)
{
return identical(board, goal); //compare baord with
} //goal
//Method decides whether a board exists in a list
private boolean exists(Board board, LinkedList<Board> list)
{
for (int i = 0; i < list.size(); i++) //compare baord eith each
if (identical(board, list.get(i))) //element of list
return true;
return false;
}
//Method decides whether two are identical
private boolean identical(Board p, Board q)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (p.array[i][j] != q.array[i][j])
return false; //if there is a mismatch then false
return true; //otherwise true
}
//Method displays path from initial to current board
private void displayPath(Board board)
{
LinkedList<Board> list = new LinkedList<>();
Board pointer = board; //start at current board
int count = 0; //count the number of boards in path
while (pointer != null) //go back towards initial board
{
list.addFirst(pointer); //add boards to list
count++; //count boards
pointer = pointer.parent; //keep going back
}
for (int i = 0; i < list.size(); i++) //print boards in list
displayBoard(list.get(i));
System.out.println("Length of the swap: " + count); //print # of boards
}
//Method displays board
private void displayBoard(Board board)
{
for (int j = 0; j < n; j++)
{
for (int i = 0; i < m; i ++)
System.out.print(board.array[j][i] + " ");
System.out.println();
}
System.out.println();
}
}