Repository navigation
Expand file tree
/
Copy pathDFSBFSTest.java
More file actions
81 lines (69 loc) · 1.81 KB
/
Copy pathDFSBFSTest.java
File metadata and controls
81 lines (69 loc) · 1.81 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
package test;
import java.util.LinkedList;
import java.util.Queue;
public class DFSBFSTest {
static int[] moveY = { -1, 1, 0, 0 };
static int[] moveX = { 0, 0, -1, 1 };
public static void main(String[] args) {
// TODO Auto-generated method stub
// for DFS Test
// int[][] adj = {
// {1, 1, 1, 1, 1, 1, 1},
// {1, 0, 0, 0, 0, 0, 1},
// {1, 1, 1, 1, 1, 0, 1},
// {1, 0, 0, 0, 0, 0, 1},
// {1, 0, 1, 1, 1, 0, 1},
// {1, 0, 0, 0, 0, 0, 1},
// {1, 1, 1, 1, 1, 1, 1}
// };
//for BFS Test
int[][] adj = {
{1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1}
};
boolean[][] visit = new boolean[7][7];
// dfs(adj, visit, 1, 1);
Queue<Integer> q = new LinkedList<>();
q.add(11);
bfs(adj, visit, q);
}
public static void dfs(int[][] adj, boolean[][] visit, int i, int j) {
visit[i][j] = true;
print(visit);
for(int k = 0; k < 4; k++) {
int indexY = i + moveY[k];
int indexX = j + moveX[k];
if(adj[indexY][indexX] == 0 && visit[indexY][indexX] == false) {
dfs(adj, visit, indexY, indexX);
}
}
}
public static void bfs(int[][] adj, boolean[][] visit, Queue<Integer> q) {
while(!q.isEmpty()) {
int v = q.poll();
visit[v/10][v%10] = true;
print(visit);
for(int k = 0; k < 4; k++) {
int indexY = v/10 + moveY[k];
int indexX = v%10 + moveX[k];
if(adj[indexY][indexX] == 0 && visit[indexY][indexX] == false) {
q.add(indexY * 10 + indexX);
}
}
}
}
public static void print(boolean[][] visit) {
for(int i = 0; i < visit.length; i++) {
for(int j = 0; j < visit.length; j++) {
System.out.print(visit[i][j] ? "1 " : "0 ");
}
System.out.println();
}
System.out.println("-------------");
}
}