Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 16234๋ฒˆ - ์ธ๊ตฌ์ด๋™ (BFS ํ’€์ด)

๊ฐ์ž ๐Ÿฅ” 2022. 7. 6. 19:39
๋ฐ˜์‘ํ˜•

โœ… ๋ฌธ์ œ ๋งํฌ

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

 

16234๋ฒˆ: ์ธ๊ตฌ ์ด๋™

N×Nํฌ๊ธฐ์˜ ๋•…์ด ์žˆ๊ณ , ๋•…์€ 1×1๊ฐœ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋•…์—๋Š” ๋‚˜๋ผ๊ฐ€ ํ•˜๋‚˜์”ฉ ์กด์žฌํ•˜๋ฉฐ, rํ–‰ c์—ด์— ์žˆ๋Š” ๋‚˜๋ผ์—๋Š” A[r][c]๋ช…์ด ์‚ด๊ณ  ์žˆ๋‹ค. ์ธ์ ‘ํ•œ ๋‚˜๋ผ ์‚ฌ์ด์—๋Š” ๊ตญ๊ฒฝ์„ ์ด ์กด์žฌํ•œ๋‹ค. ๋ชจ

www.acmicpc.net

 

โœ… ๋ฌธ์ œ ํ’€์ด ์•„์ด๋””์–ด

๊ธฐ๋ณธ์ ์œผ๋กœ ์—ฐํ•ฉ๊ตญ๊ฐ€์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋‹จํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— BFS๋กœ ํ’€์–ด์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. ์œ„์˜ ์ถœ๋ ฅ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด, ์ ์„ ์ด ์ƒ๊ธฐ๋Š” ์œ„์น˜๊ฐ€ ๋‹ค๋ฅด๋‹ค. 

30๊ณผ 40์€ ์ง์ ‘์ ์ธ ์—ฐ๊ฒฐ์€ ์•„๋‹ˆ์ง€๋งŒ, ์–ด์จ‹๋“  ์—ฐํ•ฉ๊ตญ๊ฐ€์ด๊ธดํ•˜๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๋ฐ˜์— ์–ด๋ ต๊ฒŒ๋งŒ ์ƒ๊ฐ๋˜์—ˆ๋˜ "์–ด๋””์— ์ ์„ ์ด์ƒ๊ธฐ๋Š”์ง€ ๊ณ ๋ คํ•ด์•ผํ•˜๋Š”๊ฐ€"๋ฅผ ์ „ํ˜€ ์ƒ๊ฐํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์—ฐํ•ฉ๊ตญ๊ฐ€์— ๊ฐ™์ด ๋‹ด์•„์ฃผ๋ฉด๋œ๋‹ค. ์ฆ‰, ์ ์„ ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์ด์•ผ๊ธฐ๋‹ค!

๊ธฐ๋ณธ BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์ฒ˜๋Ÿผ, ๊ทธ๋ž˜ํ”„ ๋ชจ๋‘๋ฅผ ํƒ์ƒ‰ํ•ด์ฃผ๋ฉด์„œ, ์—ฐํ•ฉ๊ตญ๊ฐ€์˜ ์ž๊ฒฉ์ด ์ถฉ์กฑ๋˜๋ฉด visited์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค.

 

1. BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋‚ด๋ถ€์—์„œ๋Š” L์ด์ƒ R ์ดํ•˜์˜ ์ฐจ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด union์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค.
2. ๊ทธ๋ฆฌ๊ณ  union์•ˆ์— ๋“ค์–ด๊ฐ€์žˆ๋‹ค๋ฉด, ์ธ๊ตฌ์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•ด์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, unionPopulation์„ ๊ตฌํ•ด์„œ ๊ฐ’์„ ๋ฐ”๊ฟ”์ค€๋‹ค.
3. main์ฝ”๋“œ!! BFS์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ return ๊ฐ’์€ union.count์ธ๋ฐ, union ์˜ count๊ฐ€ 2๊ฐœ ์ด์ƒ์ด๋ฉด, ์ธ๊ตฌ์˜ ์ด๋™์ด ๋ฐœ์ƒํ–ˆ๋‹ค๋Š” ๋œป์ด๊ธฐ ๋•Œ๋ฌธ์— union.count๊ฐ€ 2์ด์ƒ์ด ์•„๋‹๋•Œ๊นŒ์ง€ while ๋ฌธ์„ ๋Œ๋ ค์ค€๋‹ค.

 

โœ… ์ „์ฒด ์†Œ์Šค์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Boj/16234_%EC%9D%B8%EA%B5%AC%EC%9D%B4%EB%8F%99/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

//
//  main.swift
//  Algorithm
//
//  Created by LeeJiSoo on 2022/07/06.
//

import Foundation

let nlr = readLine()!.split(separator: " ").map { Int(String($0))! }
var ground = [[Int]]()
let n = nlr[0]
let l = nlr[1]
let r = nlr[2]
let dx = [-1, 1, 0, 0]
let dy = [0, 0, -1, 1] // ์ƒํ•˜์ขŒ์šฐ
var visited = Array(repeating: Array(repeating: false, count: n), count: n)
for _ in 0..<n {
    ground.append(readLine()!.split(separator: " ").map { Int(String($0))! })
}

func bfs(_ i: Int, _ j: Int) -> Int {
    var queue = [(Int, Int)]()
    queue.append((i, j))
    visited[i][j] = true  // ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
    var union = [(Int, Int)]()  // ์—ฐํ•ฉ๊ตญ๊ฐ€ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , ๋‹ค์‹œ ๋„ฃ์–ด์ฃผ๋Š” ๊ณผ์ •
    union.append((i, j))
    var count = 0  // ์—ฐํ•ฉ๊ตญ๊ฐ€์˜ ์ธ๊ตฌ์ˆ˜๋ฅผ ๋”ํ•ด์ฃผ๋Š” ๋ณ€์ˆ˜
    count += ground[i][j]
    
    while !queue.isEmpty {
        let xy = queue.removeFirst()
        let x = xy.0
        let y = xy.1
        
        for k in 0..<4 {
            let nx = x + dx[k]
            let ny = y + dy[k]
            
            if nx < 0 || nx >= n || ny < 0 || ny >= n {
                continue
            }
            
            if visited[nx][ny] {
                continue
            }
            
            // ์กฐ๊ฑด ์ถฉ์กฑํ•˜๋ฉด union ์— ๋„ฃ์–ด์ฃผ์–ด์„œ ์ˆ˜ ์„ธ์–ด์ฃผ๊ธฐ
            if l <= abs(ground[nx][ny] - ground[x][y]), abs(ground[nx][ny] - ground[x][y]) <= r {
                union.append((nx, ny))
                visited[nx][ny] = true
                count += ground[nx][ny]
                queue.append((nx, ny))
            }
        }
    }
    // ์—ฐํ•ฉ๊ตญ๊ฐ€๋“ค์˜ ์ธ๊ตฌ ์ˆ˜
    let unionPopulation = Int(floor(Double(count) / Double(union.count)))
    for country in union {
        ground[country.0][country.1] = unionPopulation
    }
    
    return union.count
}

// 2. ์—ฐํ•ฉ๊ตญ๊ฐ€๊ฐ€ 2๊ฐœ ์ด์ƒ์ด๋ฉด bfs ํ†ตํ•ด์„œ ๊ณ„์† ์ธ๊ตฌ์ด๋™์„ ํ™•์ธํ•ด์•ผํ•จ
var day = 0
while true {
    visited = Array(repeating: Array(repeating: false, count: n), count: n) // visited ์ดˆ๊ธฐํ™”
    var check = false // ์ธ๊ตฌ ์ด๋™ ์กด์žฌ ์œ ๋ฌด ์ฒดํฌ
    
    for i in 0..<n {
        for j in 0..<n {
            if !visited[i][j] {
                if bfs(i, j) >= 2 {
                    check = true
                }
            }
        }
    }
    
    if !check { // ๋”์ด์ƒ union ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ check๊ฐ€ false ์ž„
        break
    }
    
    day += 1
}

print(day)

 

๋ฐ˜์‘ํ˜•