forked from huyvohcmc/8-puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.java
33 lines (25 loc) · 795 Bytes
/
Node.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
public class Node {
public Node parent;
public int[][] matrix;
// Blank tile cordinates
public int x, y;
// Number of misplaced tiles
public int cost;
// The number of moves so far
public int level;
public Node(int[][] matrix, int x, int y, int newX, int newY, int level, Node parent) {
this.parent = parent;
this.matrix = new int[matrix.length][];
for (int i = 0; i < matrix.length; i++) {
this.matrix[i] = matrix[i].clone();
}
// Swap value
this.matrix[x][y] = this.matrix[x][y] + this.matrix[newX][newY];
this.matrix[newX][newY] = this.matrix[x][y] - this.matrix[newX][newY];
this.matrix[x][y] = this.matrix[x][y] - this.matrix[newX][newY];
this.cost = Integer.MAX_VALUE;
this.level = level;
this.x = newX;
this.y = newY;
}
}