-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q38.java
51 lines (45 loc) · 1.13 KB
/
Q38.java
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
import java.util.BitSet;
public class Q38 {
static int ans = Integer.MAX_VALUE;
static String ans_path;
static int[][] disp = {
{1,1,1,0,1,1,1},
{0,0,1,0,0,1,0},
{1,0,1,1,1,0,1},
{1,0,1,1,0,1,1},
{0,1,1,1,0,1,0},
{1,1,0,1,0,1,1},
{1,1,0,1,1,1,1},
{1,0,1,0,0,1,0},
{1,1,1,1,1,1,1},
{1,1,1,1,0,1,1},
};
public static void main (String[] args) {
BitSet is_used = new BitSet(10);
solve(is_used, 0, -1, "");
System.out.println(ans_path + "\n" + ans);
}
public static void solve (BitSet is_used, int sum, int now_disp, String path) {
if (sum >= ans) return;
if (is_used.cardinality() == 10) {
if (ans > sum) {
ans = sum;
ans_path = path;
}
return;
}
for (int i = 0; i < 10; i++) {
if (is_used.get(i)) continue;
is_used.set(i, true);
solve(is_used, sum + calc(now_disp, i), i, path.equals("") ? (i+"") : (path+"->"+i));
is_used.set(i, false);
}
}
public static int calc (int from, int to) {
if (from == -1) return 0;
int ret = 0;
for (int i = 0; i < 7; i++)
if (disp[from][i] != disp[to][i]) ret++;
return ret;
}
}