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

[Swift Algorithm] ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ BOJ #2667

by seolhee2750 2021. 10. 10.
๋ฌธ์ œ ์„ค๋ช…

<๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ์ง€๋„๊ฐ€ ์žˆ๋‹ค. 1์€ ์ง‘์ด ์žˆ๋Š” ๊ณณ์„, 0์€ ์ง‘์ด ์—†๋Š” ๊ณณ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ฒ ์ˆ˜๋Š” ์ด ์ง€๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ๊ฒฐ๋œ ์ง‘์˜ ๋ชจ์ž„์ธ ๋‹จ์ง€๋ฅผ ์ •์˜ํ•˜๊ณ , ๋‹จ์ง€์— ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ด๋ ค ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์—ฐ๊ฒฐ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์€ ์–ด๋–ค ์ง‘์ด ์ขŒ์šฐ, ํ˜น์€ ์•„๋ž˜์œ„๋กœ ๋‹ค๋ฅธ ์ง‘์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋งํ•œ๋‹ค. ๋Œ€๊ฐ์„ ์ƒ์— ์ง‘์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์—ฐ๊ฒฐ๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. <๊ทธ๋ฆผ 2>๋Š” <๊ทธ๋ฆผ 1>์„ ๋‹จ์ง€๋ณ„๋กœ ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ธ ๊ฒƒ์ด๋‹ค. ์ง€๋„๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ๋‹จ์ง€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๊ฐ ๋‹จ์ง€์— ์†ํ•˜๋Š” ์ง‘์˜ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ง€๋„์˜ ํฌ๊ธฐ N(์ •์‚ฌ๊ฐํ˜•์ด๋ฏ€๋กœ ๊ฐ€๋กœ์™€ ์„ธ๋กœ์˜ ํฌ๊ธฐ๋Š” ๊ฐ™์œผ๋ฉฐ 5≤N≤25)์ด ์ž…๋ ฅ๋˜๊ณ , ๊ทธ ๋‹ค์Œ N์ค„์—๋Š” ๊ฐ๊ฐ N๊ฐœ์˜ ์ž๋ฃŒ(0ํ˜น์€ 1)๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ด ๋‹จ์ง€์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ๋‹จ์ง€๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

 

๋‚ด ๋ฌธ์ œ ํ’€์ด

์ž…๋ ฅ

7
0110100
0110101
1110101
0000111
0100000
0111110
0111000

์ถœ๋ ฅ

3
7
8
9

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
let n = Int(readLine()!)! // ์ž…๋ ฅ๋˜๋Š” ํ•œ ๋ณ€์˜ ํฌ๊ธฐ
var map = [[Int]]()
var count = 0
var block = [Int]()
let dx = [-1, 1, 0, 0], dy = [0, 0, -1, 1]

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

// dfs ํ•จ์ˆ˜
func dfs(_ x: Int, _ y: Int) {
    if x < 0 || y < 0 || x >= n || y >= n || map[x][y] == 0 { return } // ์˜ˆ์™ธ
    
    count += 1 // ๋‹จ์ง€ ๋„“์ด ๋ˆ„์ 
    map[x][y] = 0 // ์ด๋ฏธ ์ฒดํฌํ•œ ์ž๋ฆฌ๋Š” 0์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์ค‘๋ณต ๊ฒ€์‚ฌ ํ”ผํ•ด์ฃผ๊ธฐ
    
    // ์žฌ๊ท€ ํ˜ธ์ถœ
    dfs(x+1, y)
    dfs(x-1, y)
    dfs(x, y+1)
    dfs(x, y-1)
}

// ๋ฉ”์ธ ์‹คํ–‰
for i in 0..<n {
    for j in 0..<n {
        if map[i][j] == 1 {
            count = 0 // count ์ดˆ๊ธฐํ™”
            dfs(i, j) // dfs ํ˜ธ์ถœ
            block.append(count) // block ๋ฐฐ์—ด์— ๋ฐฉ๊ธˆ ๊ตฌํ•œ ๋‹จ์ง€ ํฌ๊ธฐ ์ถ”๊ฐ€
        }
    }
}

print(block.count)
print(block.sorted().map({String($0)}).joined(separator: "\n"))

๐Ÿ‘‰ dfs ๋ฌธ์ œ๋กœ, 1๋กœ ์ด์–ด์ง„ ๊ตฌ์—ญ๋“ค๋กœ ๋‚˜๋ˆ ์„œ ํƒ์ƒ‰ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ํฌ์ธํŠธ!

์•„์ฃผ ์ „ํ˜•์ ์ธ ๋ฌธ์ œ์ด๋ฏ€๋กœ ๋‚ด ํ’€์ด์˜ ์ฃผ์š” ํŠน์ง•๋งŒ ์–˜๊ธฐํ•˜์ž๋ฉด,

์ƒํ•˜์ขŒ์šฐ๋ฅผ ๊ฒ€์‚ฌํ•˜์—ฌ 1์ด ์žˆ๋Š”์ง€๋งŒ ๊ฒ€์‚ฌํ•ด์ฃผ๋ฉด ๋˜๋Š” ์•„์ฃผ ๊ฐ„๋‹จํ•œ ๋™์ž‘๋“ค์˜ ๋ฐ˜๋ณต์ด๋ฏ€๋กœ

dfs ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๊ตณ์ด ๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์ง€ ์•Š๊ณ  ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ ๊ตฌ์„ฑํ•ด์ฃผ์—ˆ๋‹ค.

 

๋ฐ”๋กœ ์ด์ „์— ํ’€์—ˆ๋˜ #2583 ์˜์—ญ๊ตฌํ•˜๊ธฐ ๋ฌธ์ œ์™€ ์ •๋ง 99.9ํผ์„ผํŠธ ๋™์ผํ•˜๋ฏ€๋กœ..!

์ž์„ธํ•œ ํ’€์ด ๊ณผ์ •์€ ์ด์ „ ๊ฒŒ์‹œ๊ธ€์„ ์ฐธ๊ณ ์„ธํ•ด์ฃผ์„ธ์šฉ 

2021.10.10 - [๐Ÿ“ Problem Solving with Swift/๐Ÿท BOJ] - [Swift Algorithm] ์˜์—ญ ๊ตฌํ•˜๊ธฐ BOJ #2583

 

[Swift Algorithm] ์˜์—ญ ๊ตฌํ•˜๊ธฐ BOJ #2583

๋ฌธ์ œ ์„ค๋ช… ๋ˆˆ๊ธˆ์˜ ๊ฐ„๊ฒฉ์ด 1์ธ M×N(M,N≤100)ํฌ๊ธฐ์˜ ๋ชจ๋ˆˆ์ข…์ด๊ฐ€ ์žˆ๋‹ค. ์ด ๋ชจ๋ˆˆ์ข…์ด ์œ„์— ๋ˆˆ๊ธˆ์— ๋งž์ถ”์–ด K๊ฐœ์˜ ์ง์‚ฌ๊ฐํ˜•์„ ๊ทธ๋ฆด ๋•Œ, ์ด๋“ค K๊ฐœ์˜ ์ง์‚ฌ๊ฐํ˜•์˜ ๋‚ด๋ถ€๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์ด ๋ช‡ ๊ฐœ์˜ ๋ถ„๋ฆฌ

seolhee2750.tistory.com

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์˜์—ญ๊ตฌํ•˜๊ธฐ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋งŽ์ด ํ’€์–ด๋ณธ ์•„์ฃผ ์ „ํ˜•์ ์ธ dfs ๋ฌธ์ œ์˜€๊ณ ,,!
    ๋ฌธ์ œ ์ฝ๋Š”๊ฒƒ๋ถ€ํ„ฐ ์ œ์ถœ๊นŒ์ง€ 10๋ถ„๋„ ์•ˆ๊ฑธ๋ ธ๋‹ค. ์ด์ œ ์ข€ dfs๊ฐ€ ์ต์ˆ™ํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
  • ์ด๋Ÿฐ ๋ฅ˜์˜ ๋ฌธ์ œ๊ฐ€ ์–ด์ƒ‰ํ•˜์‹  ๋ถ„๋“ค์€ #2916 ๊ทธ๋ฆผ ๋ฌธ์ œ๋ฅผ ๋จผ์ € ํ’€์–ด๋ณด์‹œ๋ฉด ์กฐ์„๋“ฏํ•ฉ๋‹ˆ๋‹ค.

 


๋ฌธ์ œ

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

๋Œ“๊ธ€