- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadthFirstSearch.java
More file actions
Latest commit
120 lines (115 loc) · 3.82 KB
/
Copy pathBreadthFirstSearch.java
File metadata and controls
120 lines (115 loc) · 3.82 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
packagebasic.search;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Queue;
importjava.util.concurrent.LinkedBlockingDeque;
publicclassBreadthFirstSearch {
privateintM;
privateintN;
privateintDOWN=1;
privateintRIGHT=2;
privateSearchIteambestIteam;
privateList<SearchIteam> queue=newArrayList<SearchIteam>();
classSearchIteam {
privateintcurX;
privateintcurY;
privateintdir;
privateintvalue;
privateSearchIteamlastIteam;
publicSearchIteam(intcurX, intcurY, intdir, intvalue) {
this.curX = curX;
this.curY = curY;
this.dir = dir;
this.value = value;
}
}
/**
* 如果找到且队列中的value较小,替换
* @param newIteam
* @return 是否在搜索队列中找到相应Iteam
*/
privatebooleanreplaceIteam(SearchIteamnewIteam) {
booleanfind=false;
for (inti=0;i<queue.size();i++) {
SearchIteamsearchIteam=queue.get(i);
if(searchIteam.curX==newIteam.curX && searchIteam.curY == newIteam.curY) {
find=true;
if(searchIteam.value<newIteam.value) {
queue.remove(i);
queue.add(newIteam);
}
break;
}
}
returnfind;
}
/**
* 观察了下,似乎重复的元素都是位于搜索队列最后一个元素,是否可以只比对最后一个元素
* @param newIteam
* @return
*/
privatebooleanreplaceIteam2(SearchIteamnewIteam) {
booleanfind=false;
if(queue.size()!=0) {
SearchIteamsearchIteam=queue.get(queue.size()-1);
if(searchIteam.curX==newIteam.curX && searchIteam.curY == newIteam.curY) {
find=true;
if(searchIteam.value<newIteam.value) {
queue.remove(queue.size()-1);
queue.add(newIteam);
}
}
}
returnfind;
}
privatevoidsearch(int[][] matrix) {
while (queue.size()>0) {
SearchIteamsearchIteam=queue.remove(0);
intcurX=searchIteam.curX;
intcurY=searchIteam.curY;
intcurValue=searchIteam.value;
if(curX==M-1 && curY==N-1) {
bestIteam=searchIteam;
}
if(curX<M-1) {
SearchIteamnewItem=newSearchIteam(curX+1,curY,DOWN,curValue+matrix[curX+1][curY]);
newItem.lastIteam=searchIteam;
if(!replaceIteam2(newItem)) {
queue.add(newItem);
}
}
if(curY<N-1) {
SearchIteamnewItem=newSearchIteam(curX,curY+1,RIGHT,curValue+matrix[curX][curY+1]);
newItem.lastIteam=searchIteam;
if(!replaceIteam2(newItem)) {
queue.add(newItem);
}
}
}
}
publicintgetMaxAward(int[][] matrix) {
M=matrix.length;
N=matrix[0].length;
SearchIteamsearchIteam=newSearchIteam(0,0,0,matrix[0][0]);
queue.add(searchIteam);
search(matrix);
returnbestIteam.value;
}
publicvoidprintBestPath() {
List<Integer> dirList=newArrayList<>();
SearchIteamcurIteam=bestIteam;
while (curIteam!=null) {
dirList.add(curIteam.dir);
curIteam=curIteam.lastIteam;
}
//从dirList.size()-2开始输出,不输出起始点的dir
for (inti=dirList.size()-2;i>=0;i--) {
if(dirList.get(i) == DOWN) {
System.out.print("下 ");
} elseif(dirList.get(i) == RIGHT) {
System.out.print("右 ");
}
}
System.out.println();
}
}