-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
54 lines (48 loc) · 1.47 KB
/
main.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
//
// main.swift
// SwiftAlgorithm
//
// Created by Lee Myeonghwan on 2023/02/11.
//
import Foundation
func solution(_ rectangle: [[Int]], _ characterX:Int, characterY:Int, itemX: Int, itemY: Int) -> Int {
var answer = 0
var field = Array(repeating: Array(repeating: -1, count: 102), count: 102)
for r in rectangle {
let coordinates = r.map{ $0*2 }
let x1 = coordinates[0]
let y1 = coordinates[1]
let x2 = coordinates[2]
let y2 = coordinates[3]
for i in y1...y2 {
for j in x1...x2 {
if x1 < j && j < x2 && y1 < i && i < y2 {
field[i][j] = 0
} else if field[i][j] != 0 {
field[i][j] = 1
}
}
}
}
let dx: [Int] = [-1, 1, 0, 0]
let dy: [Int] = [0, 0, -1, 1]
var stack: [(Int, Int)] = []
stack.append((characterX * 2, characterY * 2))
var visited = Array(repeating: Array(repeating: 1, count: 102), count: 102)
while !stack.isEmpty {
let (x, y) = stack.removeFirst()
if x == itemX * 2 && y == itemY * 2 {
answer = visited[y][x] / 2
break
}
for k in 0..<4 {
let nx = x + dx[k]
let ny = y + dy[k]
if field[ny][nx] == 1 && visited[ny][nx] == 1 {
stack.append((nx, ny))
visited[ny][nx] = visited[y][x] + 1
}
}
}
return answer
}