From 687929c8b16c30a3d68add4742957dc0ca1675ed Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Fri, 12 Sep 2025 23:03:20 +0900 Subject: [PATCH] 20250912 baekjoon --- code_study/Baekjoon/swift/17070.swift | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 code_study/Baekjoon/swift/17070.swift diff --git a/code_study/Baekjoon/swift/17070.swift b/code_study/Baekjoon/swift/17070.swift new file mode 100644 index 0000000..a923af4 --- /dev/null +++ b/code_study/Baekjoon/swift/17070.swift @@ -0,0 +1,37 @@ +if let N: Int = Int(readLine() ?? "0") +{ + var house: [[Int]] = [] + house.append(Array(repeating: 1, count: N+1)) + + for _ in 1...N { + if let line = readLine(), + let row: [Int] = [1] + line.split(separator: " ").compactMap({Int($0)}) as? [Int], + row.count == N+1 + { + house.append(row) + } + } + + var dp:[[[Int]]] = Array(repeating: Array(repeating: Array(repeating: 0, count: N+1), count: N+1), count: 3) + // dp[dir][y][x], dir: 가로(0) 세로(1) 대각선(2) + dp[0][1][2] = 1 + + for y in 1...N { + for x in 1...N { + if x == 2 && y == 1 { + continue + } + + if house[y][x] == 0 { + dp[0][y][x] = dp[0][y][x-1] + dp[2][y][x-1] // 가로 + dp[1][y][x] = dp[1][y-1][x] + dp[2][y-1][x] // 세로 + + if house[y-1][x] == 0 && house[y][x-1] == 0 { + dp[2][y][x] = dp[0][y-1][x-1] + dp[1][y-1][x-1] + dp[2][y-1][x-1] + } // 대각선 + } + } + } + + print(dp[0][N][N] + dp[1][N][N] + dp[2][N][N]) +}