๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ Algorithm/์•Œ๊ณ ๋ฆฌ์ฆ˜-Swift

[์•Œ๊ณ ๋ฆฌ์ฆ˜/Swift] BOJ 1743 ์Œ์‹๋ฌผ ํ”ผํ•˜๊ธฐ :: ๊ทธ๋ž˜ํ”„, DFS/BFS

by Danna 2021. 5. 27.
728x90
728x90

Swift ๋กœ ํ’€์–ด๋ณด๋Š” ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค! ์ด๋ฒˆ์ฃผ์— Swift ์Šคํ„ฐ๋””์—์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ๋‚˜๊ฐ€๋Š”๋ฐ, ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋””์—์„œ๋„ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œํ’€์ด๊ฐ€ ์ฃผ์ œ์ด๋„ค์š” ๐Ÿ˜€ ๊ฐœ๋…๊ณผ ๋ฌธ์ œ๋ฅผ ๋™์‹œ์— ์ ‘ํ•˜๋‹ˆ๊นŒ ์ข‹์•„์š”!

 

1743๋ฒˆ: ์Œ์‹๋ฌผ ํ”ผํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— ํ†ต๋กœ์˜ ์„ธ๋กœ ๊ธธ์ด N(1 ≤ N ≤ 100)๊ณผ ๊ฐ€๋กœ ๊ธธ์ด M(1 ≤ M ≤ 100) ๊ทธ๋ฆฌ๊ณ  ์Œ์‹๋ฌผ ์“ฐ๋ ˆ๊ธฐ์˜ ๊ฐœ์ˆ˜ K(1 ≤ K ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค.  ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ K๊ฐœ์˜ ์ค„์— ์Œ์‹๋ฌผ์ด ๋–จ์–ด์ง„ ์ขŒํ‘œ (r, c)๊ฐ€ ์ฃผ์–ด์ง„

www.acmicpc.net

 

๐Ÿ‘€ ํ’€์ด ๋ฐฉ๋ฒ•

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ์ขŒํ‘œ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ทธ๋ž˜ํ”„(2์ฐจ์› ๋ฐฐ์—ด์˜ ๋งต)์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด์š”! ์ขŒํ‘œ (r, c) ๋Š” 2์ฐจ์› ๋ฐฐ์—ด์—์„œ arr[r-1][c-1] ๊ณผ ๊ฐ™์ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

(๋ฌธ์ œ๊ฐ€ ์ข€ ํŠน์ดํ•œ๋ฐ..) ํ†ต๋กœ์— ๋–จ์–ด์ง„ ์Œ์‹๋ฌผ ์ค‘ ๊ฐ€์žฅ ํฐ ์Œ์‹๋ฌผ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ฃผ๋ณ€์— ์žˆ๋Š” ์Œ์‹๋ฌผ๋ผ๋ฆฌ๋Š” ๋ญ‰์น˜๊ฒŒ๋˜์–ด ํฌ๊ธฐ๊ฐ€ ์ปค์ง„๋‹ค๊ณ  ํ•ด์š”. ์—ฌ๊ธฐ์„œ ๊ทธ๋ž˜ํ”„์˜ ๊ตฌ์—ญ์„ ๋‚˜๋ˆ„๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ƒ๊ฐํ•ด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! ์ €๋Š” ์ด ๊ณผ์ •์—์„œ DFS ๋ฅผ ์ด์šฉํ•ด ๊ตฌ์—ญ๋งˆ๋‹ค ๋ผ๋ฒจ๋ง์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

BOJ 1743 

โœ”๏ธŽ Swift ์ฝ”๋“œ

๋จผ์ €, readLine() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›๊ณ  ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

0 - ๋นˆ ํ†ต๋กœ, 1 - ์Œ์‹๋ฌผ์ด ์žˆ๋Š” ํ†ต๋กœ

 

2์ฐจ์› ๊ทธ๋ž˜ํ”„๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉฐ DFS ๋ฅผ ์ด์šฉํ•ด์„œ ๊ทธ๋ž˜ํ”„์—์„œ ์ธ์ ‘๋œ ์Œ์‹๋ฌผ๋ผ๋ฆฌ ๊ฐ™์€ ๋ผ๋ฒจ์„ ์ฃผ๋Š” ๋ผ๋ฒจ๋ง ์ž‘์—…์„ ์ง„ํ–‰ํ–ˆ์–ด์š”. ๋ผ๋ฒจ๋ง ์ž‘์—…์„ ํ•˜๋ฉด์„œ, ์ธ์ ‘ํ•œ ์ขŒํ‘œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ณต๋ฌธ์—์„œ๋Š” ๋ฐ˜ํ™˜๋œ ๊ฐ’์„ ํ†ตํ•ด ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

์˜ˆ์ œ ๋ผ๋ฒจ๋ง ์ „ / ํ›„
1 0 0 0             2 0 0 0
0 1 1 0     ->     0 3 3 0
1 1 0 0              3 3 0 0

 

์ œ์ถœํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

import Foundation

// BOJ-1743 ์Œ์‹๋ฌผํ”ผํ•˜๊ธฐ
func dfs(graph: inout [[Int]], y: Int, x : Int, label: Int) -> Int {
    // ๊ทธ๋ž˜ํ”„ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฒฝ์šฐ ์ข…๋ฃŒ
    if y < 0 || y >= graph.count || x < 0 || x >= graph[0].count {
        return 0
    }
    
    // ์ด๋ฏธ ๋ผ๋ฒจ๋ง์ด ๋˜์–ด์žˆ๊ฑฐ๋‚˜ 0์ธ ๊ฒฝ์šฐ ์ข…๋ฃŒ
    if graph[y][x] != 1 {
        return 0
    }
    
    graph[y][x] = label // ๋ผ๋ฒจ๋ง
    
    // ์ƒํ•˜์ขŒ์šฐ๋กœ ์—ฐ๊ฒฐ๋œ ์˜์—ญ์„ ๋ฐฉ๋ฌธํ•˜๋ฉฐ ๊ฐœ์ˆ˜๋ฅผ ์„ผ๋‹ค.
    var count = 1
    count += dfs(graph: &graph, y: y-1, x: x, label: label)
    count += dfs(graph: &graph, y: y+1, x: x, label: label)
    count += dfs(graph: &graph, y: y, x: x-1, label: label)
    count += dfs(graph: &graph, y: y, x: x+1, label: label)
    return count
}

// Input
let line = readLine()!.split(separator: " ").map{Int($0)!}
var graph: [[Int]] = Array(repeating: Array(repeating: 0, count: line[1]), count: line[0])
for _ in 0..<line[2] {
    let coord = readLine()!.split(separator: " ").map{Int($0)!}
    graph[coord[0]-1][coord[1]-1] = 1
}

// Output
var label = 2
var max_size = 0
for y in 0..<line[0] {
    for x in 0..<line[1] {
        if graph[y][x] != 1 {
            continue
        }
        let size = dfs(graph: &graph, y: y, x: x, label: label)
        max_size = max(size, max_size)
        if size > 0 {
            label += 1
        }
    }
}
print(max_size)

Swift ๋ฅผ ํ†ตํ•ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ‘ธ๋Š”๊ฒŒ ๋งˆ๋ƒฅ ์–ด๋ ต๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ํ’€์–ด๋ณด๋‹ˆ Swift ๋ฅผ ์ž์ฃผ ์“ธ ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋” ์ต์ˆ™ํ•ด์ง€๊ณ  ์ข‹๋„ค์š” ๐Ÿคฉ

728x90
728x90