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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 10026๋ฒˆ - ์ ๋ก์ƒ‰์•ฝ (๊ณจ๋“œ5) (DFS)

๊ฐ์ž ๐Ÿฅ” 2023. 3. 3. 01:41
๋ฐ˜์‘ํ˜•

๊ณจ๋“œ์—ฌ์„œ ๊ฒ๋จน์—ˆ๋Š”๋ฐ ์กฐ๊ฑด์ฒ˜๋ฆฌ๋งŒ ์ž˜ ํ•ด์ฃผ๋ฉด ์•„์ฃผ ์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹น!

 

โšซ๏ธ ๋ฌธ์ œ

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

 

10026๋ฒˆ: ์ ๋ก์ƒ‰์•ฝ

์ ๋ก์ƒ‰์•ฝ์€ ๋นจ๊ฐ„์ƒ‰๊ณผ ์ดˆ๋ก์ƒ‰์˜ ์ฐจ์ด๋ฅผ ๊ฑฐ์˜ ๋Š๋ผ์ง€ ๋ชปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์ด ๋ณด๋Š” ๊ทธ๋ฆผ์€ ์•„๋‹Œ ์‚ฌ๋žŒ์ด ๋ณด๋Š” ๊ทธ๋ฆผ๊ณผ๋Š” ์ข€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ํฌ๊ธฐ๊ฐ€ N×N์ธ ๊ทธ๋ฆฌ๋“œ์˜ ๊ฐ ์นธ์— R(๋นจ๊ฐ•), G(์ดˆ๋ก)

www.acmicpc.net

 

โšซ๏ธ ๋‚˜์˜ ๊ณ ๋ฏผ,,, ํ’€์ด

๋ฌธ์ œ๋ฅผ ์ฝ์ž๋งˆ์ž ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผํ• ์ง€ ํŒŒ์•…ํ–ˆ์ง€๋งŒ,, , ๋™์‹œ์— ๊ณ ๋ฏผ์ด ๋“ค์—ˆ๋˜ ๊ฒƒ์ด ์žˆ๋‹ค. ์ ๋ก์ƒ‰์•ฝ์ธ๊ฒฝ์šฐ์™€, ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ๊ฒฝ์šฐ๋ฅผ ๋”ฐ๋กœ dfs๋ฅผ ๋Œ๋ ค์ฃผ์–ด์•ผํ• ๊นŒ? ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด,,, ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜๊ฑธ๋ฆฌ์ง€ ์•Š์„๊นŒ? ์‹ถ์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ๊น”๋”ํ•˜๊ฒŒ ์งค ์ˆ˜ ์žˆ์„๊นŒ, DFS๋ฅผ ํ•œ ๋ฒˆ๋งŒ ๋Œ๋ฆฌ๋ฉด์„œ ๋‘๋งˆ๋ฆฌ ํ† ๋ผ๋ฅผ ๋ชจ๋‘ ์žก์„ ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผํ–ˆ๋‹ค. 

๐Ÿ”– ์‹œ๊ฐ„๋ณต์žก๋„ ๋จผ์ € ๋ตํฌํ•ด๋ณด์ž

๋ฐฐ์—ด์€ ๋ฌด์กฐ๊ฑด NxN ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ , N์˜ ์ตœ๋Œ€ ๊ฐ’์€ 100์ด๋‹ค. DFS ํ•˜๋‚˜๋‹น N๋ฒˆ์˜ loop๋ฅผ ๋Œ๊ธฐ ๋•Œ๋ฌธ์— O(N)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ–์ง€๋งŒ, ์ „๋ถ€ ๋ฐฉ๋ฌธํ•˜๋ฉด์„œ ํƒ์ƒ‰ํ•ด์ฃผ์–ด์•ผํ•˜๋‹ˆ๊นŒ O(N^2)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค. ์ตœ๋Œ€ ๊ทธ๋Ÿผ 10000๋ฒˆ์˜ loop๋ฅผ ๋Œ๊ฒŒ ๋˜๋Š”๋ฐ, ์ด์ •๋„๋Š” ๋ฌด๋ฆฌ ์—†์ด ๋Œ์•„๊ฐˆ ๊ฒƒ ๊ฐ™์•˜๊ณ , DFS๋ฅผ ์ฑ„ํƒํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ๋‹ค. (DFS๋ฅผ ๋‘๋ฒˆ ๋Œ๋ ค๋„ ๊ฒฐ๊ตญ ๋น…์˜คํ‘œ๊ธฐ๋ฒ•์—์„  O(N^2)์ด๋ฏ€๋กœ ๋‘๋ฒˆ๋Œ๋ ค๋„ ๋ฌด๋ฆฌ์—†์ด ๋Œ์•„๊ฐˆ๊ฒƒ ๊ฐ™์•˜๋‹ค.)

๐Ÿ”– ์ฒ˜์Œ ์ƒ๊ฐํ•œ ๋ฐฉ์‹

์Œ,, ์šฐ์„  ์ฒ˜์Œ์— ์ƒ๊ฐํ•œ ๋ฐฉ์‹์€ ์ฒ˜์Œ์— ์ž…๋ ฅ๋ฐ›๋Š” picture์„ ๋‘๊ฐ€์ง€๋กœ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ด์—ˆ๋‹ค. G๊ณผ R๋ฅผ ํ•˜๋‚˜๋กœ ํ†ต์ผ์‹œํ‚จ ์ ๋ก์ƒ‰์•ฝ ์ „์šฉ picture ๋ฐฐ์—ด๊ณผ, ๊ทธ๋ƒฅ ์ž…๋ ฅ๋ฐ›์€ picture ๋ฐฐ์—ด ์ด๋ ‡๊ฒŒ ๋‘๊ฐ€์ง€๋ฅผ ๋งŒ๋“ค์–ด์„œ, DFS๋ฅผ ๋‘๋ฒˆ ๋Œ๋ฆฌ๋Š” ๋ฐฉ์‹์„ ์ฒ˜์Œ์— ์ƒ๊ฐํ–ˆ๋‹ค. ๊ทผ๋ฐ.. ๋ญ”๊ฐ€ ๋ง˜์— ์•ˆ๋“ค์–ด! ๋” ์ž˜์งœ๊ณ  ์‹ถ์€ ๋งˆ์Œ์ด ๋“ค์—ˆ๋‹ค.

๐Ÿ”– ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ์ฑ„ํƒํ•œ ๋ฐฉ์‹

๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ dfs๋ฅผ ๋‘๋ฒˆ ๋Œ๋ฆฌ๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ๋‹ค. ์™œ๋ƒ๋ฉด, pictrue๋ฐฐ์—ด์„ ๋‘๊ฐœ ๋งŒ๋“ค์—ˆ๋”๋ผ๋„, ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ๋”ฐ๋กœ ํ•ด์„œ ์žฌ๊ท€๋ฅผ ๋Œ๋ ค์ค˜์•ผํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋งˆ๋•…ํžˆ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์ƒ๊ฐ๋‚˜์ง€ ์•Š์•„์„œ, dfs๋ฅผ ๋‘๋ฒˆ ๋Œ๋ฆฌ๊ธฐ๋กœ ํ–ˆ๋‹ค... ๊ทธ๋ž˜,, dfs๋‘๋ฒˆ ๋Œ๋ ค๋„ ๋ฌด๋ฆฌ๊ฐ€ ์—†๋„๋ก ์กฐ๊ฑด์„ ์ €๋ ‡๊ฒŒ ์ฃผ์–ด์คฌ๊ฒ ์ง€,, ๊ทธ๋ฆฌ๊ณ  ํ•ญ์ƒ ์กฐ๊ฑด์„ ์ €๋ ‡๊ฒŒ ์ค€ ์ด์œ ๊ฐ€ ์žˆ๊ฒ ์ง€! ํ•˜๊ณ  ์ƒ๊ฐํ•˜๋ฉด ??? ๋‘๋ฒˆ ๋Œ๋ฆฌ๋Š”๊ฒŒ ๊ทธ๋ž˜ ๋งž๊ฒ ๋‹ค!!

๐Ÿ”– ์ฝ”๋“œ ๊ตฌํ˜„

main์œผ๋กœ ๋Œ์•„๊ฐ€๋Š” ๋ถ€๋ถ„์—์„œ 2์ค‘ for loop๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , ๊ทธ ๋‚ด๋ถ€์—์„œ dfs๋ฅผ ๋‘๋ฒˆ ๋Œ๋ ธ๋‹ค! ๊ทธ๋ฆฌ๊ณ , dfs ์ฝ”๋“œ๋Š” ๋‘๋ฒˆ ์ž‘์„ฑํ•˜๋ฉด ๋น„ํšจ์œจ์ ์ผ ๊ฒƒ ๊ฐ™์•„์„œ, enum์œผ๋กœ type์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์ค€ ๋’ค, ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋กœ type๊นŒ์ง€ ํ•จ๊ป˜ ๋„˜๊ฒจ์ฃผ์—ˆ๋‹ค.

dfs ๋‚ด๋ถ€ ์ฝ”๋“œ๋„ ์•ฝ๊ฐ„ ๋น„์Šทํ•œ ํ˜•ํƒœ๋กœ ๋ฐ˜๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ๋” ๊น”๋”ํ•˜๊ฒŒ ์งค ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผํ–ˆ์ง€๋งŒ,, ์ผ๋‹จ,, ๊ทธ๋ž˜ ์ผ๋‹จ ์—ฌ๊ธฐ์„œ ๋...

โšซ๏ธ ์ •๋‹ต์ฝ”๋“œ

import Foundation

let n = Int(readLine()!)!
var picture = [[String]]()

for _ in 0..<n {
    picture.append(readLine()!.map{ String($0) })
}

var redGreenDiseaseVisited = Array(repeating: Array(repeating: false, count: n), count: n)
var nonDiseaseVisited = Array(repeating: Array(repeating: false, count: n), count: n)
var answer = [0, 0]

// ์ƒ ํ•˜ ์ขŒ ์šฐ
let dx = [-1, 1, 0, 0]
let dy = [0, 0, -1, 1]

enum type {
    case redGreenDisease
    case nonDisease
}

private func dfs(x: Int, y: Int, type: type) {
    switch type {
    case .redGreenDisease:
        redGreenDiseaseVisited[x][y] = true
        let now = picture[x][y]

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

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

            if now == "G" || now == "R" {
                if picture[nx][ny] == "G" || picture[nx][ny] == "R", !redGreenDiseaseVisited[nx][ny] {
                    dfs(x: nx, y: ny, type: .redGreenDisease)
                }
            } else {
                if picture[nx][ny] == now, !redGreenDiseaseVisited[nx][ny] {
                    dfs(x: nx, y: ny, type: .redGreenDisease)
                }
            }
        }

    default:
        nonDiseaseVisited[x][y] = true
        let now = picture[x][y]

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

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

            if now == picture[nx][ny], !nonDiseaseVisited[nx][ny] {
                dfs(x: nx, y: ny, type: .nonDisease)
            }
        }
    }

}

// main : ๋ชจ๋“  ๋…ธ๋“œ ํƒ์ƒ‰
for i in 0..<n {
    for j in 0..<n {
        if !nonDiseaseVisited[i][j] {
            answer[0] += 1
            dfs(x: i, y: j, type: type.nonDisease)
        }

        if !redGreenDiseaseVisited[i][j] {
            answer[1] += 1
            dfs(x: i, y: j, type: type.redGreenDisease)
        }
    }
}

print("\(answer[0]) \(answer[1])")

 

๋ฐ˜์‘ํ˜•