-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheNumber.java
395 lines (385 loc) · 12.6 KB
/
eNumber.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* @author (林剑锋)
* @ID 038054132
*/
import java.util.*;
import java.io.*;
class Node //定义节点
{ int id[][]; //一个节点中所存的九个数值的位置情况
int p=-1;
int value;
public Node()
{
id=new int[3][3];
}
}
public class eNumber
{
int step=0;
int index;
int b;
Node First=new Node();// 存储初始状态节点的数值顺序
Node End=new Node();//目标状态
LinkedList closeList=new LinkedList();//存储已经扩展的节点
LinkedList openList=new LinkedList();//存储待扩展的节点
LinkedList templeList=new LinkedList();//存放扩展出来的新节点
//---------------------------------------------------------------------------------
public eNumber()
{
First.id[0][0]=1; First.id[0][1]=0; First.id[0][2]=3;
First.id[1][0]=7; First.id[1][1]=2; First.id[1][2]=4;
First.id[2][0]=6; First.id[2][1]=8; First.id[2][2]=5;
End.id[0][0]=1; End.id[0][1]=2; End.id[0][2]=3;
End.id[1][0]=8; End.id[1][1]=0; End.id[1][2]=4;
End.id[2][0]=7; End.id[2][1]=6; End.id[2][2]=5;
}
//-------------------------------------------------------------------------------
public static void main(String[] args)
{
eNumber en=new eNumber();
System.out.println("请按任意顺序输入'0-8'(如课本示例:1 0 3 7 2 4 6 8 5)");
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
String str;
int n=0;
char c;
try
{
//str=input.readLine();
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
//c=str.charAt(n);
System.out.print("第"+(n+1)+"个数:");
str=input.readLine();
en.First.id[i][j]=Integer.parseInt(str);
//System.out.print(en.First.id[i][j]);
n++;
}
}
}
catch(IOException e){}
en.begin(en.First,en.End);
en.end(en.closeList);
}
// ------------------------判断是否相等-----------------------------------------------------
public boolean isEqual(Node fnode,Node lnode)
{
int n=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(fnode.id[i][j]==lnode.id[i][j])
n=n+1;
}
//System.out.println(n);
}
if(n==9)
return true;
else
return false;
}
//---------------------主操作-------------------------------------
public void begin(Node First1,Node End1)
{
if(!isEqual(First1,End1))
{
int len=templeList.size();
templeList.clear(); //清空临时表
int n=0;
int i=0,j=0;
step=step+1;
openList.remove(First1);
closeList.addLast(First1);
int k=0;
int h=1;
int gg;
gg=0;
l:while(gg==0)
{
for( k=0;k<3;k++)
{
for( h=0;h<3;h++)
{
if(First1.id[k][h]==0)
{
gg=-1;
continue l;
}
}
}
}
//System.out.print(k+" ");
//System.out.println(h);
left(copy(First1),k,h);
right(copy(First1),k,h);
up(copy(First1),k,h);
down(copy(First1),k,h);
int Oh=openList.size();
if(Oh>0)
{
Node minnode=new Node();
minnode=(Node)openList.getFirst();
for(int l=1;l<Oh;l++)
{
Node compnode=new Node();
compnode=(Node)openList.get(l);
if(minnode.value>=compnode.value)
minnode=compnode; //找open表中最小的节点
}
if(templeList.contains(minnode)) First1.p=1;
openList.remove(minnode);
if(step<10)
begin(minnode,End1);
}
}
else
{
First1.p=1;
closeList.addLast(First1);
}
}
//******************复制节点****************
public Node copy(Node nod)
{
Node no=new Node();
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
no.id[i][j]=nod.id[i][j];
}
}
return no;
}
//---------------------------------------------------------------------------------------------------
public void left(Node node,int x,int y)
{
int n=0,sum=0;
Node tnode=new Node();
//int i,j,a,b;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
tnode.id[i][j]=node.id[i][j];
}
}
if(y!=0)
{
node.id[x][y]=node.id[x][y-1];
node.id[x][y-1]=0;
if((!closeList.contains(node))&&(!openList.contains(node))&&(!isEqual(tnode,node)))
{ int y2;
for(int x1=0;x1<3;x1++)
{
for(int y1=0;y1<3;y1++)
{
for(int x2=0;x2<3;x2++)
{
if(node.id[x1][y1]==End.id[x2][0])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-0));
break;
}
if(node.id[x1][y1]==End.id[x2][y1])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-1));
break;
}
if(node.id[x1][y1]==End.id[x2][2])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-2));
break;
}
}
}
}
node.value=sum;
//System.out.println(sum);
openList.addLast(node);
}
else node=null;
}
if(node!=null) templeList.addLast(node);
}
//-------------------------------------------------------------------------------------------------------
public void right(Node node,int x,int y)
{
int n=0,sum=0;
Node tnode=new Node();
int i,j,a;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
tnode.id[i][j]=node.id[i][j];
}
}
if(y!=2)
{
node.id[x][y]=node.id[x][y+1];
node.id[x][y+1]=0;
if((!closeList.contains(node))&&(!openList.contains(node))&&(!isEqual(tnode,node)))
{ int y2;
for(int x1=0;x1<3;x1++)
{
for(int y1=0;y1<3;y1++)
{
for(int x2=0;x2<3;x2++)
{
if(node.id[x1][y1]==End.id[x2][0])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-0));
break;
}
if(node.id[x1][y1]==End.id[x2][y1])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-1));
break;
}
if(node.id[x1][y1]==End.id[x2][2])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-2));
break;
}
}
}
}
node.value=sum;
//System.out.println(sum);
openList.addLast(node);
}
else node=null;
}
if(node!=null) templeList.addLast(node);
}
//-------------------------------------------------------------------------------------------------------
public void up(Node node,int x,int y)
{
int n=0,sum=0;
Node tnode=new Node();
int i,j,a,b;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
tnode.id[i][j]=node.id[i][j];
}
}
if(x!=0)
{
node.id[x][y]=node.id[x-1][y];
node.id[x-1][y]=0;
if((!closeList.contains(node))&&(!openList.contains(node))&&(!isEqual(tnode,node)))
{ int y2;
for(int x1=0;x1<3;x1++)
{
for(int y1=0;y1<3;y1++)
{
for(int x2=0;x2<3;x2++)
{
if(node.id[x1][y1]==End.id[x2][0])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-0));
break;
}
if(node.id[x1][y1]==End.id[x2][y1])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-1));
break;
}
if(node.id[x1][y1]==End.id[x2][2])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-2));
break;
}
}
}
}
node.value=sum;
//System.out.println(sum);
openList.addLast(node);
}
else node=null;
}
if(node!=null) templeList.addLast(node);
}
//------------------------------------------------------------------------------------------------
public void down(Node node,int x,int y)
{
int n=0,sum=0;
Node tnode=new Node();
int i,j,a,b;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
tnode.id[i][j]=node.id[i][j];
}
}
if(x!=2)
{
node.id[x][y]=node.id[x+1][y];
node.id[x+1][y]=0;
if((!closeList.contains(node))&&(!openList.contains(node))&&(!isEqual(tnode,node)))
{ int y2;
for(int x1=0;x1<3;x1++)
{
for(int y1=0;y1<3;y1++)
{
for(int x2=0;x2<3;x2++)
{
if(node.id[x1][y1]==End.id[x2][0])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-0));
break;
}
if(node.id[x1][y1]==End.id[x2][y1])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-1));
break;
}
if(node.id[x1][y1]==End.id[x2][2])
{
sum=sum+(Math.abs(x1-x2)+Math.abs(y1-2));
break;
}
}
}
}
node.value=sum;
//System.out.println(sum);
openList.addLast(node);
}
else node=null;
}
if(node!=null) templeList.addLast(node);
}
//***************输出函数*****************************************************
public void end(LinkedList list)
{
int count=0;
int ppt=closeList.size();
for(int v=0;v<ppt;v++)
{
System.out.println();
Node outnode=(Node)list.get(v);
if(outnode.p==1)
{
count=count+1;
System.out.println("第"+count+"步:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(outnode.id[i][j]+" ");
}
System.out.println();
}
}
}
System.out.println("程序执行结束....");
}
}