-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q57.java
78 lines (61 loc) · 1.5 KB
/
Q57.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
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
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
public class Q57 {
static final int N = 7;
public static void main (String[] args) {
long s = System.currentTimeMillis();
Queue<Integer> q_nums = new LinkedList<>();
Queue<Integer> q_step = new LinkedList<>();
Set<Integer> set = new HashSet<>();
int start = 0;
for (int i = 1; i <= N; i++)
start = start * 10 + i;
q_nums.add(start);
q_step.add(0);
set.add(start);
while (!q_nums.isEmpty()) {
int[] nums = convert(q_nums.poll());
int step = q_step.poll();
if (step == 10) {
System.out.println(q_nums.size() + 1);
break;
}
int[] next = nums.clone();
++step;
for (int i = 1; i < N; i++) {
swap(next, i - 1, i);
int con = convert(next);
if (set.contains(con)) {
swap(next, i - 1, i);
continue;
}
q_nums.add(con);
q_step.add(step);
set.add(con);
swap(next, i - 1, i);
}
}
long e = System.currentTimeMillis();
System.out.println("time:" + (e-s) + "ms");
}
static int convert (int[] nums) {
int ret = 0;
for (int n : nums)
ret = ret * 10 + n;
return ret;
}
static int[] convert (int n) {
char[] c = (n + "").toCharArray();
int[] ret = new int[N];
for (int i = 0; i < N; i++)
ret[i] = c[i] - '0';
return ret;
}
static void swap (int[] ary, int i, int j) {
int tmp = ary[i];
ary[i] = ary[j];
ary[j] = tmp;
}
}