문제 링크 (opens new window)

# 풀이

import Foundation

let n = Int(readLine()!)!

var adj: [[Int]] = .init(repeating: [], count: n)
var visited: [[Bool]] = .init(repeating: .init(repeating: false, count: n), count: n)

var ret = 0, maxRet = 0

let dy = [-1, 0, 1, 0], dx = [0, 1, 0, -1]


for i in 0..<n {
    adj[i] = readLine()!.split(separator: " ").map { Int($0)! }
}

func dfs(_ y: Int, _ x: Int, _ height: Int) {
    visited[y][x] = true

    for i in 0..<4 {
        let ny = dy[i] + y
        let nx = dx[i] + x

        if ny < 0 || nx < 0 || ny >= n || nx >= n {
            continue
        }

        if adj[ny][nx] > height && !visited[ny][nx] {
            dfs(ny, nx, height)
        }
    }
}

for height in 0...100 {
    ret = 0
    visited = .init(repeating: .init(repeating: false, count: n), count: n)

    for row in 0..<n {
        for col in 0..<n {
            if adj[row][col] <= height || visited[row][col] { continue }
            dfs(row, col, height)
            ret += 1
        }
    }
    maxRet = maxRet < ret ? ret : maxRet
}

print(maxRet)