-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathTestQuestion.java
More file actions
75 lines (65 loc) · 1.44 KB
/
Copy pathTestQuestion.java
File metadata and controls
75 lines (65 loc) · 1.44 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
package testQuestion;
import java.util.Iterator;
import java.util.TreeSet;
public class TestQuestion
{
private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean[n];
private int[][] a = new int[n][n];
private String result = "";
private TreeSet TreeSet = new TreeSet();
public static void main(String[] args)
{
new TestQuestion().start();
}
private void start()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}
a[3][5] = 0;
a[5][3] = 0;
for (int i = 0; i < n; i++)
{
this.depthFirstSearch(i);
}
Iterator it = TreeSet.iterator();
while (it.hasNext())
{
String string = (String) it.next();
if (string.indexOf("4") != 2)
{
System.out.println(string);
}
}
}
private void depthFirstSearch(int startIndex)
{
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n)
{
TreeSet.add(result);
}
for(int j = 0; j < n; j++)
{
if (a[startIndex][j] == 1 && visited[j] == false)
{
depthFirstSearch(j);
} else {
continue;
}
}
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}