๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

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

 

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

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

www.acmicpc.net

๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒํ•˜์ขŒ์šฐ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๊ณ  ๊ทธ ์“ฐ๋ ˆ๊ธฐ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

DFS๋กœ ํ’€์–ด๋ดค๋‹ค. 2์ฐจ์›๋ฐฐ์—ด๋กœ map๊ณผ visited ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ์“ฐ๋ ˆ๊ธฐ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0์œผ๋กœ ํ‘œ์‹œํ–ˆ๋‹ค.

 

1000
0110
1100

์œ„์˜ ์˜ˆ์ œ์—์„œ ์ด๋ ‡๊ฒŒ..


๊ทธ๋ฆฌ๊ณ  for๋ฌธ์„ ๋Œ๋ฉด์„œ ์“ฐ๋ ˆ๊ธฐ์ด๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์— DFS๋ฅผ ๋Œ๋ ค์ฃผ๋ฉฐ count๋ฅผ +1 ํ•ด์ฃผ์—ˆ๋‹ค.

DFS๊ฐ€ ๋๋‚˜๊ณ  ๋‚˜์˜ฌ ๋•Œ๋Š” ๋‹ค์‹œ count๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ  count๊ฐ€ ์ œ์ผ ํฐ ๊ฐ’์„ ์ €์žฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

์ฝ”๋“œ

import Foundation

let input = readLine()!.split(separator: " ").map { Int(String($0))! }
let height = input[0], width = input[1], k = input[2]
var map = Array(repeating: Array(repeating: 0, count: width), count: height) // ์“ฐ๋ ˆ๊ธฐ ์žˆ์œผ๋ฉด 1 ์—†์œผ๋ฉด 0
var visited = Array(repeating: Array(repeating: false, count: width), count: height)
let dy = [0, 0, -1, 1] // ์ƒํ•˜์ขŒ์šฐ
let dx = [-1, 1, 0, 0]
var answer = Int.min
var count = 0

for _ in 0..<k {
    let input = readLine()!.split(separator: " ").map { Int(String($0))! }
    let h = input[0]-1, w = input[1]-1
    map[h][w] = 1 // ์“ฐ๋ ˆ๊ธฐ๋Š” 1
}

func DFS(_ x: Int, _ y: Int) {
    visited[y][x] = true // ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
    count += 1 // ์“ฐ๋ ˆ๊ธฐ ๊ฐœ์ˆ˜ ์ถ”๊ฐ€
    
    for i in 0..<4 {
        let nx = dx[i] + x
        let ny = dy[i] + y
        // map์˜ ๋ฒ”์œ„์— ์žˆ๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ  ์“ฐ๋ ˆ๊ธฐ์ธ ๊ฒฝ์šฐ์—๋งŒ DFS ๋Œ๋ฆผ
        if nx >= 0 && nx < width && ny >= 0 && ny < height && !visited[ny][nx] && map[ny][nx] == 1 {
            DFS(nx, ny)
        }
    }
    
}

for y in 0..<height {
    for x in 0..<width {
        if map[y][x] == 1 && !visited[y][x] { // ์“ฐ๋ ˆ๊ธฐ์ด๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ๋งŒ DFS ๋Œ๋ฆผ
            count = 0
            DFS(x, y)
            answer = max(answer, count) // DFS ๋‹ค ๋Œ๋ฆฌ๊ณ  ๋‚˜์˜จ ์“ฐ๋ ˆ๊ธฐ ๊ฐœ์ˆ˜์˜ max๊ฐ’ ์ €์žฅ
        }
        
    }
}

print(answer)

 

ํ›„๊ธฐ

์‚ฌ์‹ค ๋Œ€ํ‘œ์ ์ธ ํƒ์ƒ‰ ๋ฌธ์ œ์ธ๋ฐ.. ์˜ค๋žœ๋งŒ์— DFS ๋ฌธ์ œ๋ฅผ ํ’€์–ด์„œ ๋งŽ์ด ํ—ค๋งธ๋‹ค..ใ… ใ… 

๋ฌธ์ œ๋ฅผ ์ž˜๋ชป ์ƒ๊ฐํ•ด์„œ depth๋กœ ํ’€๋‹ค๊ฐ€ ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ์™„์ „ ์ž˜๋ชป๋œ ํ’€์ด์—ˆ๋‹ค....ใ…Žใ…Ž

func DFS(_ x: Int, _ y: Int, _ depth: Int) {
    answer = max(answer, depth) // DFS์˜ ๊นŠ์ด ๊ณ„์‚ฐ
    visited[y][x] = true // ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ

    for i in 0..<4 {
        let nx = dx[i] + x
        let ny = dy[i] + y
        // map์˜ ๋ฒ”์œ„์— ์žˆ๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ  ์“ฐ๋ ˆ๊ธฐ์ธ ๊ฒฝ์šฐ์—๋งŒ DFS ๋Œ๋ฆผ
        if nx >= 0 && nx < width && ny >= 0 && ny < height && !visited[ny][nx] && map[ny][nx] == 1 {
            DFS(nx, ny, depth+1)
        }
    }

}

์ด๋Ÿฐ ์‹์œผ๋กœ DFS์˜ ๊นŠ์ด๋ฅผ ๋”ํ•ด๊ฐ”๋Š”๋ฐ ๋ฌธ์ œ๋Š” ๊นŠ์ด๊ฐ€ ์•„๋‹ˆ๋ผ DFS๋ฅผ ๋„๋Š” ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค,,๐Ÿ˜…
count๋กœ ๊ฐœ์ˆ˜๋ฅผ ๋”ํ•ด๊ฐ€๋Š” ๊ฑธ๋กœ ์ˆ˜์ •ํ–ˆ๋”๋‹ˆ ๋ฐ”๋กœ ํ’€๋ฆผ

 

 

profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...