-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.java
129 lines (112 loc) · 2.3 KB
/
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
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
import java.util.*;
public class Node {
int [] puzzle = new int[9];
Node pirent ;
List<Node> chlidern = new ArrayList<Node>();
int x=0 ;
int col=3;
public Node(int[] p){
for(int i=0;i<p.length;i++) {
puzzle[i]=p[i];
}
}
boolean isGaol(int[] gol) {
boolean isgol=true ;
int[] theGol ;
theGol =gol;
for(int i= 0;i<puzzle.length;i++) {
if(theGol[i] != puzzle[i]) {
isgol=false ;}
}
return isgol;
}
void copyarr(int[] a,int[] b) {
for(int i= 0;i<b.length;i++) {
b[i]=a[i];
}
}
void MoveToRight(int[] p ,int i) {
if(i%col <col-1) {
int[] pa = new int[9];
copyarr(p , pa);
int temp = pa[i+1];
pa[i+1]=pa[i];
pa[i] = temp;
Node child = new Node(pa);
chlidern.add(child);
child.pirent=this;
}
}
void MoveToLift(int[] p ,int i) {
if(i%col>0) {
int[] pa = new int[9];
copyarr(p , pa);
int temp = pa[i-1];
pa[i-1]=pa[i];
pa[i] = temp;
Node child = new Node(pa);
chlidern.add(child);
child.pirent=this;
}
}
void MoveToUp(int[] p ,int i) {
if (i - col >=0) {
int[] pa = new int[9];
copyarr(p , pa);
int temp = pa[i-3];
pa[i-3]=pa[i];
pa[i] = temp;
Node child = new Node(pa);
chlidern.add(child);
child.pirent=this;
}
}
void MoveToDown(int[] p ,int i) {
if (i+col < puzzle.length) {
int[] pa = new int[9];
copyarr(p , pa);
int temp = pa[i+3];
pa[i+3]=pa[i];
pa[i] = temp;
Node child = new Node(pa);
chlidern.add(child);
child.pirent=this;
}
}
String PrintPiz() {
int t =0 ;
String s="";
for(int i= 0;i<col;i++) {
for(int j= 0;j<col;j++) {
if(puzzle[t]==0){
s+="- ";
t++;
}else {
s+=puzzle[t++]+" ";
}
}
s+="\n";
}
s+="\n";
return s;
}
boolean isSamePuz(int[] p) {
boolean isSame=true ;
for(int i=0 ;i<puzzle.length;i++) {
if(puzzle[i]!=p[i]) {
isSame =false;
}
}
return isSame;
}
void expandMove() {
for(int i=0;i<puzzle.length;i++) {
if(puzzle[i]==0)
x=i;
}
MoveToRight(puzzle,x);
MoveToLift(puzzle,x);
MoveToUp(puzzle,x);
MoveToDown(puzzle,x);
}
}