import Foundation let T = Int(readLine()!)! for _ in 1...T { let MNK = readLine()!.components(separatedBy: " ").map { Int($0)! } let M = MNK[0] // ๋ฐฐ์ถ๋ฐญ ๊ฐ๋ก๊ธธ์ด let N = MNK[1] // ๋ฐฐ์ถ๋ฐญ ์๋ก๊ธธ์ด let K = MNK[2] // ๋ฐฐ์ถ๊ฐ ์ฌ์ด์ ธ ์๋ ์์น์ ๊ฐ์ // M * N 2์ฐจ์ ๋ฐฐ์ด ์์ฑ var graph = [[Int]](repeating: [Int](repeating: 0, count: M), count: N) for _ in 1...K { let XY = readLine()!.components(separatedBy: " ").map { Int($0)! } let ..
[Swift] ๋ฐฑ์ค1260 DFS์ BFS
ยท
๐ ์ฝํ /BOJ
import Foundation let NMV = readLine()!.components(separatedBy: " ").map { Int($0)! } let N = NMV[0] let M = NMV[1] let V = NMV[2] var graph = [String: [String]]() initializeGraph() for _ in 1...M { let AB = readLine()!.components(separatedBy: " ") let A = AB[0] let B = AB[1] graph[A]?.append(B) graph[B]?.append(A) } printArray(DFS(start: String(V))) printArray(BFS(start: String(V))) func DFS(st..
import Foundation let N = Int(readLine()!)! var tuples = [(x: Int, y: Int)]() for _ in 1...N { let xy = readLine()!.components(separatedBy: " ").map { Int($0)! } let x = xy[0] let y = xy[1] tuples.append((x, y)) } tuples = tuples.sorted(by: { $0.y < $1.y }).sorted(by: { $0.x < $1.x }) for tuple in tuples { print(tuple.x, tuple.y) } ํํ๋ก ํ์๋ค. ์ ๋ ฌํ ๋๋ ์ ๋ ฌํ๋ ์์๋ฅผ ์ ์ํ๋๊ฒ ์ข์ ๊ฒ ๊ฐ๋ค.
[Swift] ๋ฐฑ์ค 10866 ๋ฑ
ยท
๐ ์ฝํ /BOJ
import Foundation let N = Int(readLine()!)! var deque = [Int]() for _ in 1...N { let line = readLine()! printNum(command: line) } func printNum(command: String) { let arr = command.components(separatedBy: " ") switch arr[0] { case "push_back": return deque.append(Int(arr[1])!) case "push_front": if deque.isEmpty { deque.append(Int(arr[1])!) } else { deque.insert(Int(arr[1])!, at: 0) } return () ..