💻 Algorithm
-
[Swift] 백준 1697 숨바꼭질 (Graph Traversal)💻 Algorithm/Swift 2023. 2. 12. 17:18
📌 풀이 💬 Code import Foundation let input = readLine()!.split(separator: " ").map { Int(String($0))! } let n = input[0] let m = input[1] var isUsed = [Bool].init(repeating: false, count: n+1) backTracking(0, []) func backTracking(_ cnt: Int, _ arr: [String]) { if cnt == m { print(arr.joined(separator: " ")) return } for i in 1...n where !isUsed[i] { isUsed[i] = true backTracking(cnt+1, arr+[String..
-
[Swift] 백준 15649 N과 M (1) (Back Tracking)💻 Algorithm/Swift 2023. 1. 31. 22:50
📌 풀이 💬 Code import Foundation let input = readLine()!.split(separator: " ").map { Int(String($0))! } let n = input[0] let m = input[1] var isUsed = [Bool].init(repeating: false, count: n+1) backTracking(0, []) func backTracking(_ cnt: Int, _ arr: [String]) { if cnt == m { print(arr.joined(separator: " ")) return } for i in 1...n where !isUsed[i] { isUsed[i] = true backTracking(cnt+1, arr+[String..
-
[Swift] 백준 2178 미로 탐색 (Graph Traversal)💻 Algorithm/Swift 2023. 1. 31. 22:48
📌 풀이 💬 Code import Foundation let input = readLine()!.split(separator: " ").map{ Int(String($0))! } let n = input[0] let m = input[1] var graph = [[Int]]() var visited = [[Bool]]() for _ in 0.. Int { var queue = [[Int]]() var minLen = 10000 let dx = [-1, 1, 0, 0] let dy = [0, 0, -1, 1] queue.append([x, y, 1]) visited[x][y] = true while !queue.isEmpty { let now = queue.removeFirst() let len = now..