๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ์ ๋ก์ƒ‰์•ฝ BOJ #10026

by seolhee2750 2022. 1. 23.
๋ฌธ์ œ

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

 

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

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

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
let n = Int(readLine()!)!
var picture = [[Character]]()
var count = 0
var count2 = 0

for _ in 0..<n { picture.append(Array(readLine()!)) }

func dfs(_ x: Int, _ y: Int, _ mark: Character, _ color: Character) {
    if x < 0 || y < 0 || x >= n || y >= n || picture[x][y] == mark || picture[x][y] != color { return } // ์˜ˆ์™ธ
    
    picture[x][y] = mark // ์ด๋ฏธ ์ฒดํฌํ•œ ์ž๋ฆฌ ํ‘œ์‹œํ•ด์„œ ์ค‘๋ณต ํ”ผํ•˜๊ธฐ
    
    // ์žฌ๊ท€ ํ˜ธ์ถœ
    dfs(x+1, y, mark, color)
    dfs(x-1, y, mark, color)
    dfs(x, y+1, mark, color)
    dfs(x, y-1, mark, color)
}

// ์ ๋ก์ƒ‰์•ฝ ์•„๋‹Œ ์‚ฌ๋žŒ (1์ฐจ)
for i in 0..<n {
    for j in 0..<n {
        if picture[i][j] == "c" || picture[i][j] == "s" { continue }
        else {
            if picture[i][j] == "R" { dfs(i, j, "s", "R") }
            else if picture[i][j] == "G" { dfs(i, j, "s", "G") }
            else if picture[i][j] == "B" { dfs(i, j, "c", "B") }
            count += 1
        }
    }
}

// ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ (2์ฐจ)
for i in 0..<n {
    for j in 0..<n {
        if picture[i][j] == "f" { continue }
        else {
            if picture[i][j] == "s" { dfs(i, j, "f", "s") }
            else if picture[i][j] == "c" { dfs(i, j, "f", "c") }
            count2 += 1
        }
    }
}

print(String(count) + " " + String(count2))

๐Ÿ‘‰ dfs๋กœ ํ’€์–ด์ฃผ์—ˆ๊ณ , ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ์‹œ์„ ์„ ๋จผ์ € ์ฒดํฌํ•œ ํ›„ ์ ๋ก์ƒ‰์•ฝ์ผ ๋•Œ๋ฅผ ์ฒดํฌํ•ด์ฃผ๋ฉด ์‰ฝ๋‹ค.

  • 1์ฐจ๋กœ ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ์‚ฌ๋žŒ์˜ ์‹œ์„ ์œผ๋กœ dfs๋ฅผ ๋Œ๋ ค์„œ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ ,
    2์ฐจ๋กœ ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์˜ ์‹œ์„ ์œผ๋กœ dfs๋ฅผ ๋Œ๋ ค์„œ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด์ฃผ์—ˆ๋‹ค.
  • picture๋ผ๋Š” ๋ฐฐ์—ด์€ ์ฃผ์–ด์ง„ ๊ทธ๋ฆผ์˜ ์ƒ‰๊น”๋“ค์„ ์ €์žฅํ–ˆ๋Š”๋ฐ, picture์„ var๋กœ ๋‘๊ณ 
    dfs๋ฅผ ๋Œ๋ฆฌ๋ฉด์„œ ์ฒดํฌ๋ฅผ ์™„๋ฃŒํ•œ ์ž๋ฆฌ๋Š” ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์ค‘๋ณต์„ ํ”ผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ–ˆ๋‹ค.
  • ๋จผ์ € 1์ฐจ์—์„œ๋Š” ์˜์—ญ์„ R, G, B๋กœ ๋‚˜๋ˆ ์„œ ์ฒดํฌํ•˜๋Š”๋ฐ,
    ์ฒดํฌํ•˜๋ฉด์„œ R๊ณผ G์˜ ๊ฒฝ์šฐ์—๋Š” ์ฒดํฌ ์™„๋ฃŒํ•œ ์ž๋ฆฌ๋ฅผ "s"๋กœ, B๋Š” "c"๋กœ ๋ฐ”๊ฟจ๋‹ค.
    2์ฐจ๋กœ ์ฒดํฌํ•  ๋•Œ๋Š” ์ ๋ก์ƒ‰์•ฝ์˜ ์ž…์žฅ์—์„œ ์ฒดํฌํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ๊ตฌ๋ถ„ํ–ˆ๋‹ค.
  • 2์ฐจ๋กœ ์ฒดํฌํ•  ๋•Œ๋Š” picture์˜ ๋ชจ๋“  ์ž๋ฆฌ์— "s"ํ˜น์€ "c"๋งŒ ์žˆ์„ ๊ฒƒ์ด๋‹ค.
    ๋”ฐ๋ผ์„œ s์™€ c๋กœ ์˜์—ญ์„ ๊ตฌ๋ถ„ํ•ด์„œ ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • dfs๋ฅผ ๋Œ๋ฆฌ๋Š” for๋ฌธ์„ ๋‘ ๋ฒˆ์ด๋‚˜ ๋Œ๋ ค์•ผ ํ•ด์„œ 1์ดˆ ์•ˆ์— ๋ฌธ์ œ๊ฐ€ ๋Œ์•„๊ฐˆ์ง€..?๊ฐ€ ์˜๋ฌธ์ด๋ผ
    ํ’€๋ฉด์„œ ์กฐ๊ธˆ ๊ฑฑ์ •๋์ง€๋งŒ ์ž˜ ๋Œ์•„๊ฐ”๋‹ค.
  • ํ‰์†Œ dfs๋ฅผ ์ž˜ ์“ธ ์ค„ ์•„๋Š” ์‚ฌ๋žŒ์ด๋ผ๋ฉด ๋ชจ๋‘ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค.

๋Œ“๊ธ€