-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadthFirstSearch.java
More file actions
119 lines (112 loc) · 2.89 KB
/
BreadthFirstSearch.java
File metadata and controls
119 lines (112 loc) · 2.89 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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
class Pair{
int x, y;
Pair parent = null;
public Pair(int x, int y, Pair parent){
this.x = x;
this.y = y;
this.parent = parent;
}
}
public class BreadthFirstSearch {
public static void main(String[] args){
char[][] map = {
{'*', '*', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', '*', ' '},
{' ', '*', ' ', '*', ' '},
{' ', ' ', ' ', '*', ' '},
{' ', ' ', ' ', '*', ' '},
{' ', ' ', ' ', '*', ' '},
{' ', ' ', ' ', ' ', ' '}};
Pair S = new Pair(2, 0, null);
Pair E = new Pair(7, 3, null);
//reversed order
List<Pair> shortestPath = BFS(map, S, E);
System.out.println("Smallest Step number is: " + shortestPath.size());
printPath(shortestPath, map.length, map[0].length);
}
public static void printPath(List<Pair> path, int m, int n){
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
boolean flag = false;
for(int k = 0; k < path.size(); ++k){
if(i == path.get(k).x && j == path.get(k).y){
flag = true;
path.remove(k);
break;
}
}
if(flag)
System.out.print(" . ");
else
System.out.print(" ");
}
System.out.println();
}
}
public static boolean checkEqual(Pair p1, Pair p2){
return p1.x == p2.x && p1.y == p2.y;
}
public static void printWalked(boolean[][] used){
System.out.println();
for(int i = 0; i < used.length; ++i){
for(int j = 0; j < used[i].length; ++j){
if(used[i][j])
System.out.print(" . ");
else
System.out.print(" ");
}
System.out.println();
}
System.out.print("----------------");
}
public static List<Pair> BFS(char[][] map, Pair S, Pair E){
//int curLevel = 0;
Queue<Pair> q = new LinkedList<Pair>();
int m = map.length, n = map[0].length;
boolean[][] used = new boolean[m][n];
q.offer(S);
int curCount = 1;
int nextCount = 0;
while(q.size() != 0){
Pair cur = q.poll();
used[cur.x][cur.y] = true;
--curCount;
if(checkEqual(cur, E)){
List<Pair> st = new ArrayList<Pair>();
while(cur != null){
st.add(cur);
cur = cur.parent;
}
return st;
}
if(cur.x + 1 < m && !used[cur.x + 1][cur.y] && map[cur.x + 1][cur.y] != '*'){
++nextCount;
q.offer(new Pair(cur.x + 1, cur.y, cur));
}
if(cur.x - 1 >= 0 && !used[cur.x - 1][cur.y] && map[cur.x - 1][cur.y] != '*'){
++nextCount;
q.offer(new Pair(cur.x - 1, cur.y, cur));
}
if(cur.y + 1 < n && !used[cur.x][cur.y + 1] && map[cur.x][cur.y + 1] != '*'){
++nextCount;
q.offer(new Pair(cur.x, cur.y + 1, cur));
}
if(cur.y - 1 >= 0 && !used[cur.x][cur.y - 1] && map[cur.x][cur.y - 1] != '*'){
++nextCount;
q.offer(new Pair(cur.x, cur.y - 1, cur));
}
if(curCount == 0){
curCount = nextCount;
nextCount = 0;
//++curLevel;
}
printWalked(used);
}
return null;
}
}