forked from algorithm010/algorithm010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecond.java
More file actions
34 lines (29 loc) · 1.06 KB
/
second.java
File metadata and controls
34 lines (29 loc) · 1.06 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
//题目: 最小基因变化
public static void main(String[] args) {
DFS brute = new DFS();
brute.minMutation("AACCGGTT", "AACCGGTA", new String[]{"AACCGGTA"});
}
int count = Integer.MAX_VALUE;
public int minMutation(String start, String end, String[] bank) {
HashSet<String> vistied = new HashSet<>();
dfs(0, start, end, bank, vistied);
return count == Integer.MAX_VALUE ? -1 : count;
}
private void dfs(int minStepCount, String start, String end, String[] bank, HashSet<String> vistied) {
if (start.equals(end)) {
count = Math.min(count, minStepCount);
return;
}
for (int i = 0; i < bank.length; i++) {
int diff = 0;
for (int j = 0; j < 8; j++) {
if (bank[i].charAt(j) != start.charAt(j)) diff++;
if (diff > 1) break;
}
if (diff == 1 && !vistied.contains(bank[i])) {
vistied.add(bank[i]);
dfs(minStepCount + 1, bank[i], end, bank, vistied);
vistied.remove(bank[i]);
}
}
}