-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1963번.swift
67 lines (57 loc) · 1.74 KB
/
1963번.swift
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
// 출처 : 백준 소수 경로
// https://www.acmicpc.net/problem/1963
// 풀이 : hogumachu
let tc = Int(readLine()!)!
var isPrime: [Bool] = Array(repeating: true, count: 10_000)
isPrime[0] = false
isPrime[1] = false
for i in 2..<10_000 {
if isPrime[i] {
for j in 2..<5_000 {
if i * j >= 10_000 {
break
} else {
isPrime[i * j] = false
}
}
}
}
for _ in 0..<tc {
var visited: [Bool] = Array(repeating: false, count: 10_000)
var queue: [(Int, Int)] = []
var queueIndex = 0
let primes = readLine()!.split(separator: " ").map{ Int(String($0))! }
var result = -1
queue.append((primes[0], 0))
while queue.count > queueIndex && result == -1 {
let select = queue[queueIndex]
queueIndex += 1
visit(select.0, select.1)
}
print(result == -1 ? "impossible" : result)
func visit(_ now: Int, _ value: Int) {
visited[now] = true
if now == primes[1] {
result = value
return
}
for i in 0...9 {
let one = (now / 10) * 10 + i
let two = (now / 100) * 100 + i * 10 + now % 10
let three = (now / 1000) * 1000 + i * 100 + now % 100
let four = i * 1000 + now % 1000
if isPrime[one] && !visited[one] {
queue.append((one, value + 1))
}
if isPrime[two] && !visited[two] {
queue.append((two, value + 1))
}
if isPrime[three] && !visited[three] {
queue.append((three, value + 1))
}
if i != 0 && isPrime[four] && !visited[four] {
queue.append((four, value + 1))
}
}
}
}