-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
76 lines (64 loc) Β· 2.29 KB
/
Main.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
package BFS.P1525;
import java.io.*;
import java.util.*;
public class Main {
final static int TARGET = 123456789;
static int[] move = {-1, 1, -3, 3};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/BFS/P1525/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = 0;
for (int i = 0; i < 3; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < 3; j++) {
int n = Integer.parseInt(st.nextToken());
if (n == 0) n = 9;
num = num * 10 + n;
}
}
Set<Integer> set = new HashSet<>();
Queue<Integer> q = new LinkedList<>();
q.offer(num);
int count = 0;
boolean flag = false;
loop: while (!q.isEmpty()) {
int curSize = q.size();
for (int i = 0; i < curSize; i++) {
int c = q.poll();
if (c == TARGET) {
flag = true;
break loop;
}
int hole = findHole(c);
for (int j = 0; j < 4; j++) {
int to = hole + move[j];
if ((0 <= to && to < 9) &&
!(hole == 2 && to == 3 || hole == 3 && to == 2) &&
!(hole == 5 && to == 6 || hole == 6 && to == 5)) {
int s = swap(c, hole, to);
if (!set.contains(s)) {
q.offer(s);
set.add(s);
}
}
}
}
count ++;
}
if (flag) System.out.println(count);
else System.out.println(-1);
}
static int findHole(int num) {
StringBuilder sb = new StringBuilder(num + "");
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '9') return i;
}
return 0;
}
static int swap(int num, int hole, int to) {
StringBuilder sb = new StringBuilder(num + "");
sb.replace(hole, hole+1, String.valueOf(sb.charAt(to)));
sb.replace(to, to+1, "9");
return Integer.parseInt(sb.toString());
}
}