[DFS] 인접행렬

suojae·2023년 12월 14일

문제


해결

import Foundation

func sol(m: Int, n: Int, examples: [[Int]]) {
    
    var matrix = Array(repeating: Array(repeating: 0, count: m), count: n)

    for ex in examples {
        let row = ex[0] - 1
        let column = ex[1] - 1
        let value = ex[2]

        if row >= 0 && row < n && column >= 0 && column < m {
            matrix[row][column] = value
        }
    }

    for row in matrix {
        for value in row {
            print(value, terminator: " ")
        }
        print()
    }
}

// Example usage
let exampleInput = [
    [1, 2, 7],
    [1, 3, 4],
    [2, 1, 2],
    [2, 3, 5],
    [2, 5, 5],
    [3, 4, 5],
    [4, 2, 2],
    [4, 5, 5],
    [6, 4, 5]
]

sol(m: 6, n: 9, examples: exampleInput)
profile
Hi 👋🏻 I'm an iOS Developer who loves to read🤓

0개의 댓글