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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 14500๋ฒˆ - ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ (์™„์ „ํƒ์ƒ‰ / DFS๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๋‹ค๋˜๋ฐ?)

๊ฐ์ž ๐Ÿฅ” 2022. 12. 30. 17:26
๋ฐ˜์‘ํ˜•

๋‚˜๋Š” ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€์—ˆ๋‹ค. ๊ฐํžˆ DFS๋Š” ์ƒ๊ฐํ•ด๋‚ด์ง€๋„ ๋ชปํ•จ,,,
์™„์ „ ํƒ์ƒ‰์œผ๋กœ ๋ชจ๋“  ๋ชจ์–‘์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ์ž‘์„ฑํ•˜๊ณ  ํ’€์–ด์„œ ์ฝ”๋“œ๊ฐ€ ๋ฌด๋ ค 287์ค„์ด๋‚˜ ๋‚˜์™”๋‹ค. ใ…‹ใ…‹ใ…‹ใ…‹(์ฃผ์„ํฌํ•จ)
๊ตฌํ˜„์‹ค๋ ฅ๋„ ์ค‘์š”ํ•˜์ง€๋งŒ, ์˜ค๋‹ต๋…ธํŠธ๋Š” ๊ผญ DFS๋กœ ํ•ด๋ณด์ž.๐Ÿ‘Š๐Ÿป

 

๐ŸŸ  ๋ฌธ์ œ

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

 

14500๋ฒˆ: ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ

ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋ž€ ํฌ๊ธฐ๊ฐ€ 1×1์ธ ์ •์‚ฌ๊ฐํ˜•์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ด์–ด์„œ ๋ถ™์ธ ๋„ํ˜•์ด๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค. ์ •์‚ฌ๊ฐํ˜•์€ ์„œ๋กœ ๊ฒน์น˜๋ฉด ์•ˆ ๋œ๋‹ค. ๋„ํ˜•์€ ๋ชจ๋‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ •์‚ฌ๊ฐํ˜•์˜ ๋ณ€

www.acmicpc.net

 

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

์ด๊ฒƒ๋„ ์˜ˆ์ „์— ์Šคํ„ฐ๋””ํ•  ๋•Œ ํ•œ ๋ฒˆ ๋ดค๋˜ ๋ฌธ์ œ๋‹ค. ๊ทธ๋ž˜์„œ, ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค ์‚ดํŽด๋ด์•ผํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ณ  ์žˆ์—ˆ๋‹ค. (์™„์ „ํƒ์ƒ‰(๋ธŒ๋ฃจํŠธํฌ์Šค)๋ฌธ์ œ์ด๊ธฐ๋„ํ•˜๊ณ !) ๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ,, ๋ณด์ž๋งˆ์ž ์ˆจ์ด ํ„ฑ ๋ง‰ํ˜”์ง€๋งŒ, ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

ํ…ŒํŠธ๋ฆฌ์Šค ์กฐ๊ฐ์€ ๋‹ค์„ฏ๊ฐœ๊ฐ€ ์žˆ๋‹ค. ํ•ด๋‹น ๋‹ค์„ฏ๊ฐœ ๋ชจ์–‘์€ ํšŒ์ „๋„ ๊ฐ€๋Šฅํ•˜๊ณ , ๋Œ€์นญ๋„ ๊ฐ€๋Šฅํ–ˆ๋‹ค. ํšŒ์ „์ด์•ผ for ๋ฌธ์œผ๋กœ ๋Œ๋ฆฌ๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ณ  ๋Œ€์นญ์€ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ ์ƒˆ๋กœ์šด๋ชจ์–‘์œผ๋กœ ๋‹ค๋ค„์ค˜์•ผ๊ฒ ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋‹ค.

๋‚ด๊ฐ€ ํ’€์ดํ•œ ๋ฐฉ๋ฒ•์˜ ๊ฐ€์žฅ ํ•ต์‹ฌ์€ ํ•œ์นธ์‹ ์ƒ‰์น ํ•ด์ค€๋‹ค๋Š” ์‹์œผ๋กœ ์ ‘๊ทผํ–ˆ๋‹ค.

์•ž์„œ bfs๋ฌธ์ œ๋ฅผ ํ’€๋“ฏ dx, dy ๋ฐฉํ–ฅํ‚ค๋ฅผ ์„ค์ •ํ•ด๋‘์ž. ใ…ก ๋ชจ์–‘์„ ์˜ˆ์‹œ๋กœ ๋“ค๋ฉด,  for๋ฌธ์„ 4๋ฒˆ ๋Œ๋ ค์„œ ์˜ค๋ฅธ์ชฝ์นธ์„ ํ•˜๋‚˜์”ฉ ํ•ด๋‹น ์œ„์น˜์— ์žˆ๋Š” ๋‚ด๋ถ€์˜ ์ˆ˜๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๊ทธ๋Ÿผ L ๋ชจ์–‘์ฒ˜๋Ÿผ ๊บพ์ธ๊ฑด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ?

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋œ๋‹ค. ์ฆ‰, ๋ฐฉํ–ฅ์ „ํ™˜์ด ํ•œ๋ฒˆ ํ•„์š”ํ•˜๋‹ค. ์ง€๊ธˆ ํ•ด๋‹น L์„ ๋ณด๋ฉด, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ƒ‰์น ์ด ๋˜๋‹ค๊ฐ€, ๋ฐฉํ–์ž‰ '์œ„'๋กœ ๋ฐ”๋€Œ์–ด์•ผํ•œ๋‹ค. ์ด๊ฒƒ์„ ๋‚˜๋Š” direction์ด๋ผ๊ณ  ๋”ฐ๋กœ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค. ๋‚ด๊ฐ€ ์ง€์ •ํ•ด์ค€ ๋ฐฉํ–ฅํ‚ค๋กœ ๋ณด๋ฉด, '์˜ค๋ฅธ์ชฝ'์€ dx,dy์ธ๋ฑ์Šค 3์ด๋‹ค. 3๋ฐฉํ–ฅ์œผ๋กœ ์ง„ํ–‰ํ•˜๋‹ค๊ฐ€, 0 ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค. 

๋„ํ˜•์˜ ํšŒ์ „๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. ๋„ํ˜•์„ ํšŒ์ „ํ•˜๋Š” ๊ฒƒ๋„, ์ƒ‰์น ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ƒ๊ฐํ•˜๋ฉด๋œ๋‹ค. ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด์„œ ์ƒ‰์น ํ•˜๋‹ค๊ฐ€, ๋ฐฉํ–ฅ์„ ๋ฐ”๊ฟ”์„œ ์œ„๋กœ ๊ฐ€๋ฉด์„œ ์ƒ‰์น ํ•˜๊ณ , ๋˜ ๋ฐฉํ–ฅ์„ ๋ฐ”๊ฟ”์„œ ์™ผ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด์„œ ์ƒ‰์น ํ•˜๊ณ ,, ์ด๋Ÿฐ์‹์œผ๋กœ ์ƒ๊ฐํ•˜๋ฉด๋œ๋‹ค!

๋จธ๋ฆฟ์†์—์„œ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฐ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ  ํšŒ์ „ํ•˜๋ฉด์„œ ,, ๊ทธ๋ฆฌ๋ฉด์„œ,, ํ’€์—ˆ๋‹ค. ๐Ÿคฏ๐Ÿคฏ๐Ÿคฏ๐Ÿคฏ

๐Ÿ”– ์ถ”๊ฐ€๋กœ, ๋Œ€์นญ์ด ํ•„์š”ํ•œ ๋„ํ˜•์€ ๋ฌด์—‡์ด ์žˆ์„๊นŒ?

๋Œ€์นญ์ด ํ•„์š”ํ•œ ๋„ํ˜•์€, L๋ชจ์–‘๊ณผ Z ๋ชจ์–‘ ๋‘๊ฐœ ๋ฟ์ด์—ˆ๋‹ค. l ๋ชจ์–‘, ใ…๋ชจ์–‘, ใ…๋ชจ์–‘์€ ํšŒ์ „์œผ๋กœ ๋ชจ๋“  ๋ชจ์–‘์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ใ…๋Š” ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋Œ€์นญํ•˜๋ฉด ใ…“๋ชจ์–‘์ด์ง€๋งŒ, ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ใ…๋ฅผ ๋‘๋ฒˆ ํšŒ์ „ํ•˜๋ฉด ใ…“๊ฐ€ ๋‚˜์˜จ๋‹ค. ๊ทธ๋ž˜์„œ ๋”ฐ๋กœ ๋Œ€์นญ์— ๋Œ€ํ•œ func์€ ์ƒ๊ฐํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

์ด ๋ชจ๋“ ๊ฒƒ๋“ค์„ ๊ตฌํ˜„ํ•ด๋ณด์ž...

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

์‹œ๊ฐ„์ดˆ๋Š” 2์ดˆ๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค. ์ด 2์–ต๋ฒˆ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€์–ด๋„ ์‹œ๊ฐ„์€ ์ถฉ๋ถ„ํ•˜๋‹ค

import Foundation

// ํ•„์š”ํ•œ ๋ณ€์ˆ˜ ์„ ์–ธ
let nm = readLine()!.split(separator: " ").map{ Int($0)! }
var tetroMap = [[Int]]()
for _ in 0..<nm[0] {
    let temp = readLine()!.split(separator: " ").map { Int($0)! }
    tetroMap.append(temp)
}

// ์œ„ ์•„๋ž˜ ์™ผ ์˜ค
let dx = [-1, 1, 0, 0]
let dy = [0, 0, -1, 1]

// ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ๋ชจ์–‘๋ณ„ ํ•ฉ์„ ๊ตฌํ•ด์ฃผ๋Š” func
// ๋Œ€์นญ์ด ํ•„์š”ํ•œ ๋…€์„์€ L ๊ณผ Z
// 1. ใ…ก
private func tetro_1(direction: Int, nx: Int, ny: Int) -> Int {
    var nx = nx
    var ny = ny
    var total = 0
    for _ in 0..<4 {
        if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
            return 0
        }
        total += tetroMap[nx][ny]
        nx += dx[direction]
        ny += dy[direction]
    }
    return total
}

// 2. ใ…
private func tetro_2(direction: Int, nx: Int, ny: Int) -> Int {
    var total = 0
    if nx < 0 || ny < 0 || nx+1 >= nm[0] || ny+1 >= nm[1] {
        return 0
    }
    total += tetroMap[nx][ny]
    total += tetroMap[nx+1][ny]
    total += tetroMap[nx][ny+1]
    total += tetroMap[nx+1][ny+1]
    return total
}

// 3. L์˜ ๋Œ€์นญ
private func tetro_3(direction: Int, nx: Int, ny: Int) -> Int {
    var direction = direction
    var nx = nx
    var ny = ny
    var total = 0

    for _ in 0..<3 {
        if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
            return 0
        }
        total += tetroMap[nx][ny]
        nx += dx[direction]
        ny += dy[direction]
    }

    nx -= dx[direction]
    ny -= dy[direction]

    if direction == 3 {
        direction = 1
    } else if direction == 0 {
        direction = 3
    } else if direction == 2 {
        direction = 0
    } else {
        direction = 2
    }

    nx += dx[direction]
    ny += dy[direction]
    if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
        return 0
    }
    total += tetroMap[nx][ny]
    return total
}

// 3.1 L
private func tetro_3_symmetry(direction: Int, nx: Int, ny: Int) -> Int {
    var direction = direction
    var nx = nx
    var ny = ny
    var total = 0

    for _ in 0..<3 {
        if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
            return 0
        }
        total += tetroMap[nx][ny]
        nx += dx[direction]
        ny += dy[direction]
    }
    nx -= dx[direction]
    ny -= dy[direction]

    if direction == 3 {
        direction = 0
    } else if direction == 0 {
        direction = 2
    } else if direction == 2 {
        direction = 1
    } else {
        direction = 3
    }

    nx += dx[direction]
    ny += dy[direction]
    if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
        return 0
    }
    total += tetroMap[nx][ny]
    return total
}

// 4. Z
private func tetro_4(direction: Int, nx: Int, ny: Int) -> Int {
    var direction = direction
    var nx = nx
    var ny = ny
    var total = 0

    for _ in 0..<2 {
        if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
            return 0
        }
        total += tetroMap[nx][ny]
        nx += dx[direction]
        ny += dy[direction]
    }
    nx -= dx[direction]
    ny -= dy[direction]

    if direction == 3 {
        direction = 1
    } else if direction == 0 {
        direction = 3
    } else if direction == 2 {
        direction = 0
    } else {
        direction = 2
    }
    nx += dx[direction]
    ny += dy[direction]
    if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
        return 0
    }
    total += tetroMap[nx][ny]

    if direction == 1 {
        direction = 3
    } else if direction == 3 {
        direction = 0
    } else if direction == 0 {
        direction = 2
    } else {
        direction = 1
    }
    nx += dx[direction]
    ny += dy[direction]
    if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
        return 0
    }
    total += tetroMap[nx][ny]
    return total
}

// 4.1 Z์˜ ๋Œ€์นญ
private func tetro_4_symmetry(direction: Int, nx: Int, ny: Int) -> Int {
    var direction = direction
    var nx = nx
    var ny = ny
    var total = 0

    for _ in 0..<2 {
        if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
            return 0
        }
        total += tetroMap[nx][ny]
        nx += dx[direction]
        ny += dy[direction]
    }
    nx -= dx[direction]
    ny -= dy[direction]

    if direction == 3 {
        direction = 0
    } else if direction == 0 {
        direction = 2
    } else if direction == 2 {
        direction = 1
    } else {
        direction = 3
    }
    nx += dx[direction]
    ny += dy[direction]
    if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
        return 0
    }
    total += tetroMap[nx][ny]

    if direction == 0 {
        direction = 3
    } else if direction == 2 {
        direction = 0
    } else if direction == 1 {
        direction = 2
    } else {
        direction = 3
    }
    nx += dx[direction]
    ny += dy[direction]
    if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
        return 0
    }
    total += tetroMap[nx][ny]
    return total
}

// 5. ใ…— ใ…œ ใ… ใ…“
private func tetro_5(direction: Int, nx: Int, ny: Int) -> Int {
    var direction = direction
    var nx = nx
    var ny = ny
    var total = 0

    for _ in 0..<3 {
        if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
            return 0
        }
        total += tetroMap[nx][ny]
        nx += dx[direction]
        ny += dy[direction]
    }
    nx = nx - dx[direction] - dx[direction]
    ny = ny - dy[direction] - dy[direction]

    if direction == 3 {
        direction = 1
    } else if direction == 0 {
        direction = 3
    } else if direction == 2 {
        direction = 0
    } else {
        direction = 2
    }
    nx += dx[direction]
    ny += dy[direction]
    if nx < 0 || ny < 0 || nx >= nm[0] || ny >= nm[1] {
        return 0
    }
    total += tetroMap[nx][ny]
    return total
}

// main
var direction = 0
var answer = [Int]()

for i in 0..<nm[0] {
    for j in 0..<nm[1] {
        // 4๋ฐฉํ–ฅ ๋Œ๋ฆฌ๊ธฐ
        for k in 0..<4 {
            answer.append(tetro_1(direction: k, nx: i, ny: j))
            answer.append(tetro_2(direction: k, nx: i, ny: j))
            answer.append(tetro_3(direction: k, nx: i, ny: j))
            answer.append(tetro_3_symmetry(direction: k, nx: i, ny: j))
            answer.append(tetro_4(direction: k, nx: i, ny: j))
            answer.append(tetro_4_symmetry(direction: k, nx: i, ny: j))
            answer.append(tetro_5(direction: k, nx: i, ny: j))
        }
    }
}

print(answer.max()!)

 

 

์•„์˜ค,, ๊ธธ๊ณ  ๊ธธ๋‹ค.. ์ƒ๊ฐํ•œ๊ฒƒ์ฒ˜๋Ÿผ ์ฝ”๋“œ๋งŒ ์ž˜~~~~ ์งœ์—ฌ์ง€๋ฉด ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋‹ค. ๊ทผ๋ฐ,,, ์ด๋ฐฉํ–ฅ ์ €๋ฐฉํ–ฅ ํ—ท๊ฐˆ๋ ค์„œ ํ’€๊ธฐ ์‰ฝ์ง€ ์•Š์•˜๋‹ค. ํ•˜์ง€๋งŒ, ์ฝ”๋“œ๋ฅผ๋ณด๋ฉด ์ดˆ๋ฐ˜์— ๊ฐ์„ ์žก์œผ๋ฉด ๋’ค์— ํ•จ์ˆ˜๋“ค์€ ๊ฑฐ์˜๋‹ค ๋˜‘๊ฐ™์ด ํ˜๋Ÿฌ๊ฐ„๋‹ค. ์ดˆ๋ฐ˜์— ๊ฐ์žก๋Š”๋ฐ ์กฐ๊ธˆ ์˜ค๋ž˜๊ฑธ๋ ธ๊ณ , ๋’ค์— ํ•จ์ˆ˜๋“ค์€ ๋ณต๋ถ™ํ•˜๋ฉด์„œ ๋จธ๋ฆฌ๋กœ ์Š‰์Š‰ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋ฉด์„œ ํ’€์—ˆ๋‹ค. 

์ƒ๊ฐ๋ณด๋‹ค ์ฝ”ํ…Œ์—์„œ ๋งˆ์ฃผ์ณค์„๋•Œ, ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋“ค์„ ํ‘ธ๋Š”๊ฒƒ๋ณด๋‹ค ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๊ฐ€์žฅ ํž˜๋“ค๋‹ค. ๋จธ๋ฆฌ๋กœ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋‚˜๊ฐ€๋Š”๊ฒŒ ์ƒ๊ฐ๋ณด๋‹ค ํž˜์ด๋“ค๊ณ , ์ด๊ฒƒ์„ ์ฝ”๋“œ๋กœ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋Š”๊ฒƒ๋„ ์ƒ๋‹นํ•œ ์ƒ๊ฐ์ด ํ•„์š”ํ•˜๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์€ ์ƒ๋‹นํžˆ ๊ท€์ฐฎ์€ ์ผ์ด๋ผ๊ณ  ์ƒ๊ฐํ–‡์ง€๋งŒ ์ด๋Ÿฐ๋ฅ˜์˜ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ๋งˆ์ฃผ์น˜๋ฉด, ํ™•์‹คํžˆ ์ฝ”ํ…Œ ๋Œ€๋น„๊ฐ€ ์ž˜ ๋  ๊ฒƒ ๊ฐ™๋‹ค. (์ด ๋ฌธ์ œ๋ž‘ ๋น„์Šทํ•œ ๋ฌธ์ œ๋ฅผ LG CNS ์ฝ”ํ…Œ์—์„œ ๋ณธ์ ์ด ์ž‡๋Š”๊ฒƒ๊ฐ™๋‹ค. ๊ทธ ๋ฌธ์ œ๋„ ๊ฒฐ๊ตญ ๊ตฌํ˜„๋ฌธ์ œ์˜€๊ธดํ–ˆ์—ˆ๋Š”๋ฐ,,, ) 

ํ™”์ดํŒ…,,๊ทธ๋ฆฌ๊ณ  ๋ชจ๋‘ ํ•ดํ”ผ๋‰ด์ด์–ด์–ดใ…“

 

๋ฐ˜์‘ํ˜•