2025-09-12 23:03:20 +09:00

38 lines
1.1 KiB
Swift

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])
}