๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋†’์ด๋Š” 1~100 ์‚ฌ์ด์ด๊ธฐ ๋•Œ๋ฌธ์—, ์žฅ๋งˆ์ฒ ์— ๋ฌผ์ด ์Œ“์ผ ์ˆ˜ ์žˆ๋Š” ๋†’์ด๋„ 1~100 ์‚ฌ์ด๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์•ˆ์ „ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฒดํฌํ•ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ, ์ด ๋•Œ ์•„๋ฌด ์ง€์—ญ๋„ ์ž ๊ธฐ์ง€ ์•Š์„ ์ˆ˜ ์žˆ์–ด์„œ ๋ฐ˜๋ณต๋ฌธ์„ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒŒ ํ•ด ์คฌ๋‹ค. ์ด ์•„์ด๋””์–ด์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ ์ด ๊ธ€์˜ ๋Œ“๊ธ€์„ ์ฐธ๊ณ ํ–ˆ๋‹ค.

๋ฌธ์ œ

https://www.acmicpc.net/problem/2468

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

let dx = [1, 0, -1, 0]
let dy = [0, 1, 0, -1]
let n = Int(readLine()!)!
var board = [[Int]]()
for _ in 0..<n {
    let area = readLine()!.split(separator: " ").map { Int(String($0))! }
    board.append(area)
}

var max = 0

for i in 0...100 {
    var graph = board

    // ์ž ๊ธฐ๋Š” ์ง€์—ญ ์ฒดํฌ
    for j in 0..<n {
        for k in 0..<n {
            if graph[j][k] <= i {
                graph[j][k] = 0
            }
        }
    }

    // ์ง€์—ญ ๊ฐœ์ˆ˜ ์ฒดํฌ
    var count = 0
    for j in 0..<n {
        for k in 0..<n {
            if graph[j][k] != 0 {
                var visited = [[j, k]]
                var queue = [[j, k]]
                while !queue.isEmpty {
                    let v = queue.removeFirst()
                    for (x, y) in zip(dx, dy) {
                        let nx = v[0] + x
                        let ny = v[1] + y
                        if nx < 0 || nx >= n || ny < 0 || ny >= n || graph[nx][ny] == 0 {
                            continue
                        }
                        if graph[nx][ny] != 0 {
                            visited.append([nx, ny])
                            queue.append([nx, ny])
                            graph[nx][ny] = 0
                        }
                    }
                }
                count += 1
            }
        }
    }


    max = count > max ? count : max
}

print(max)
๋ฐ˜์‘ํ˜•