-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadthFirstSearch.java
More file actions
120 lines (115 loc) · 3.82 KB
/
BreadthFirstSearch.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
package basic.search;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
public class BreadthFirstSearch {
private int M;
private int N;
private int DOWN=1;
private int RIGHT=2;
private SearchIteam bestIteam;
private List<SearchIteam> queue=new ArrayList<SearchIteam>();
class SearchIteam {
private int curX;
private int curY;
private int dir;
private int value;
private SearchIteam lastIteam;
public SearchIteam(int curX, int curY, int dir, int value) {
this.curX = curX;
this.curY = curY;
this.dir = dir;
this.value = value;
}
}
/**
* 如果找到且队列中的value较小,替换
* @param newIteam
* @return 是否在搜索队列中找到相应Iteam
*/
private boolean replaceIteam(SearchIteam newIteam) {
boolean find=false;
for (int i=0;i<queue.size();i++) {
SearchIteam searchIteam=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;
}
}
return find;
}
/**
* 观察了下,似乎重复的元素都是位于搜索队列最后一个元素,是否可以只比对最后一个元素
* @param newIteam
* @return
*/
private boolean replaceIteam2(SearchIteam newIteam) {
boolean find=false;
if(queue.size()!=0) {
SearchIteam searchIteam=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);
}
}
}
return find;
}
private void search(int[][] matrix) {
while (queue.size()>0) {
SearchIteam searchIteam=queue.remove(0);
int curX=searchIteam.curX;
int curY=searchIteam.curY;
int curValue=searchIteam.value;
if(curX==M-1 && curY==N-1) {
bestIteam=searchIteam;
}
if(curX<M-1) {
SearchIteam newItem=new SearchIteam(curX+1,curY,DOWN,curValue+matrix[curX+1][curY]);
newItem.lastIteam=searchIteam;
if(!replaceIteam2(newItem)) {
queue.add(newItem);
}
}
if(curY<N-1) {
SearchIteam newItem=new SearchIteam(curX,curY+1,RIGHT,curValue+matrix[curX][curY+1]);
newItem.lastIteam=searchIteam;
if(!replaceIteam2(newItem)) {
queue.add(newItem);
}
}
}
}
public int getMaxAward(int[][] matrix) {
M=matrix.length;
N=matrix[0].length;
SearchIteam searchIteam=new SearchIteam(0,0,0,matrix[0][0]);
queue.add(searchIteam);
search(matrix);
return bestIteam.value;
}
public void printBestPath() {
List<Integer> dirList=new ArrayList<>();
SearchIteam curIteam=bestIteam;
while (curIteam!=null) {
dirList.add(curIteam.dir);
curIteam=curIteam.lastIteam;
}
//从dirList.size()-2开始输出,不输出起始点的dir
for (int i=dirList.size()-2;i>=0;i--) {
if(dirList.get(i) == DOWN) {
System.out.print("下 ");
} else if(dirList.get(i) == RIGHT) {
System.out.print("右 ");
}
}
System.out.println();
}
}