-
Notifications
You must be signed in to change notification settings - Fork 3
/
그림.swift
59 lines (50 loc) · 1.76 KB
/
그림.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
import Foundation
struct Node: Hashable {
let row: Int
let col: Int
}
let nm = readLine()!.split(separator: " ").map { Int(String($0))! }
var inputArr: [[Int]] = []
(0..<nm[0]).forEach { _ in
let arr = readLine()!.split(separator: " ").map { Int(String($0))! }
inputArr.append(arr)
}
func solution(matrix: [[Int]]) -> [Int] {
var visited: Set<Node> = []
var queue: [Node] = []
var paints: Int = 0
var maximum: Int = 0
for row in 0..<matrix.count {
for col in 0..<matrix[row].count {
guard matrix[row][col] == 1 else { continue }
let node = Node(row: row, col: col)
if visited.contains(node) { continue }
var loops: Int = 0
paints += 1
visited.update(with: node)
queue.append(node)
while !queue.isEmpty {
loops += 1
let currentNode = queue.removeFirst()
let x = [-1, 0, 1, 0]
let y = [0, 1, 0, -1]
(0..<4).forEach {
if 0..<matrix.count ~= currentNode.row + x[$0] &&
0..<matrix[row].count ~= currentNode.col + y[$0] &&
matrix[currentNode.row + x[$0]][currentNode.col + y[$0]] == 1 {
let newNode = Node(row: currentNode.row + x[$0], col: currentNode.col + y[$0])
if !visited.contains(newNode){
visited.update(with: newNode)
queue.append(newNode)
}
}
}
}
maximum = max(maximum, loops)
}
}
return [paints, maximum]
}
solution(matrix: inputArr).forEach {
print($0)
}