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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 2210๋ฒˆ - ์ˆซ์žํŒ ์ ํ”„ (DFS, ์žฌ๊ท€)

๊ฐ์ž ๐Ÿฅ” 2023. 2. 22. 20:23
๋ฐ˜์‘ํ˜•

โšซ๏ธ ๋ฌธ์ œ

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

 

2210๋ฒˆ: ์ˆซ์žํŒ ์ ํ”„

111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, 212121 ์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋“ค์ด๋‹ค.

www.acmicpc.net

 

โšซ๏ธ ๋‚˜์˜ ํ’€์ด

๋ฌธ์ œ๋ฅผ ๋ณด์ž๋งˆ์ž DFS์ž„์„ ์ง๊ฐํ–ˆ๋‹ค. ์‹œ์ž‘์ ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๊นŠ์€ depth๊นŒ์ง€ ํŒŒ๊ณ ๋“ค๊ณ , ๊ทธ ๋ถ€๋ถ„์—์„œ 6๊ธ€์ž๊ฐ€ ๋˜๋ฉด ๋”ฑ dfsํ•จ์ˆ˜๋ฅผ ๋ฉˆ์ถฐ์ฃผ๋ฉด ๋˜๋Š” ๋ถ€๋ถ„์ด๋ฏ€๋กœ dfs์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๊ตฌ๋‚˜! ์ƒ๊ฐํ–ˆ๋‹ค. (ํ™•์‹คํžˆ ๊ฐœ๋…์„ ํƒ„ํƒ„ํžˆ ํ•˜๊ณ  ํ•˜๋‹ˆ๊นŒ ๋ณด์ด๊ธด ๋ณด์ด๋Š” ๊ณ ๋งŒ, ๋ฟŒ--๋“ฏ)

๊ทธ๋ž˜์„œ ๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ dfs์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

๊ทธ๋ฆฌ๊ณ  dfs๋Š” 6๊ธ€์ž๊ฐ€ ๋˜๋ฉด ๋ฐ”๋กœ return ํ•ด์ฃผ๊ณ  ํƒˆ์ถœํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์‹œ์ž‘์ ์„ ์ „๋ถ€ ๋ฐ”๊ฟ”์„œ ์ „๋ถ€๋‹ค ํƒ์ƒ‰ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค! ๊ทธ๋ž˜์„œ dfs๋ฅผ ์‹ค์ œ๋กœ ์‹คํ–‰ํ•˜๋Š” ๋ถ€๋ถ„์€ for๋ฌธ์œผ๋กœ ์‹œ์ž‘์ ์„ ์ „๋ถ€ ๋„ฃ์–ด์ฃผ์–ด์•ผํ•œ๋‹ค. ์ด๊ฒƒ์„ ์ฝ”๋“œ๋กœ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

โšซ๏ธ ์ •๋‹ต์ฝ”๋“œ

ํ•œ ๋ฒˆ ํ‹€๋ฆฐ ์ด์œ ๋Š”,,, count๋ฅผ ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š”๋ฐ array ๊ทธ๋Œ€๋กœ ์ „๋ถ€ ์ถœ๋ ฅํ•ด์คฌ๋‹ค ,, ๋ฌธ์ œ๋ฅผ ์ž˜๋ณด์ž ํ›„ํ›„ 

import Foundation

var matrix = [[Int]]()
for _ in 0..<5 {
    matrix.append(readLine()!.split(separator: " ").map{ Int($0)! })
}

let dx = [-1, 1, 0, 0]
let dy = [0, 0, -1, 1]

var answerList = [String]()

private func dfs(x: Int, y: Int, number: String) {
    var number = number
    number += String(matrix[x][y])

    if number.count == 6 {
        answerList.append(number)
        return
    }

    for i in 0..<4 {
        let nx = x + dx[i]
        let ny = y + dy[i]

        if nx < 0 || nx >= 5 || ny < 0 || ny >= 5 {
            continue
        }

        dfs(x: nx, y: ny, number: number)
    }
}

for i in 0..<5 {
    for j in 0..<5 {
        dfs(x: i, y: j, number: "")
    }
}

print(Set(answerList).count)

 


 

๐Ÿ’ฌ ๊ทธ๋ƒฅ,, ์ฃผ์ €๋ฆฌ์ฃผ์ €๋ฆฌ

์Šคํ„ฐ๋””์นœ๊ตฌ๋“ค๊ณผ DFS/BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด๋ฅผ ์ผ์ฃผ์ผ๋™์•ˆ ์‹œ์ž‘ํ–ˆ๋‹ค. ๊ทธ๋ƒฅ ๋ฌดํ„ฑ๋Œ€๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€๋ ค๊ณ  ํ–ˆ์ง€๋งŒ, ์ด๋ฒˆ์— ํ’€ ๋ฌธ์ œ๋“ค์€ ์ „๋ถ€ ๋ฐฑ์ค€ ์‹ค๋ฒ„1 ์ด์ƒ ์ˆ˜์ค€์ด์—ˆ๊ณ , ๊ทธ๋Œ€๋กœ ํ’€๋ฉด ๋˜ ์ธํ„ฐ๋„ท์„ ๋งˆ๊ตฌ ์ฐธ๊ณ ํ•˜๋ฉด์„œ ํ’€๊ฒŒ ๋ป”ํ–ˆ๋‹ค... dfs/bfs๊ฐ€ ๋ฌธ์ œ ์œ ํ˜•๋งŒ ์ตํžˆ๋ฉด ์™„์ „ ์•”๊ธฐ๋ฌธ์ œ๋ผ๊ณ  ๊ทธ๋Ÿฌ๋Š”๋ฐ, ๋‚œ ์™ค์ผ€ ์–ด๋ ต์ง€? ์•„์ง ๊ฐœ๋…์„ ํŒŒ์•…ํ•˜์ง€ ๋ชปํ•ด์„œ ์ธ๊ฒƒ ๊ฐ™์•„์„œ, ๋‹ค์‹œ ๊ณต๋ถ€๋ฅผ ํ–ˆ๋‹ค. ๊ทธ ๊ณต๋ถ€ ๋‚ด์šฉ์€ ์•„๋ž˜ ์ฐธ๊ณ ! 

https://github.com/HotCodeBreakers/CodingTest/discussions/25

 

[๊ฐœ๋…์ •๋ฆฌ] DFS+BFS (ํ˜น์‹œ,,, ๋ฌธ์ œํ’€์ด ์ „์— ์ด์ฝ”ํ…Œ ์ฑ…์„ ๋ณด์‹œ๋‚˜์š”!?) · Discussion #25 · HotCodeBreakers/Codi

์•ˆ๋…•ํ•˜์„ธ์š” ํ•ซ์ฝ”๋”๋ถ„๋“ค, ์งˆ๋ฌธ์ด ํ•˜๋‚˜ ์žˆ๋Š”๋ฐ์š”! ๋ชจ๋‘ ๋ฌธ์ œํ’€๊ธฐ ์ „์— ์ด์ฝ”ํ…Œ์ฑ…์— ์žˆ๋Š” ์˜ˆ์ œ๋ฅผ ๋ณด์‹œ๋‚˜์š”? ์ €๋Š” ๊ทธ๋ƒฅ ๋ฌดํ„ฑ๋Œ€๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์‹œ์ž‘ํ•˜๋Š”๋ฐ์š” ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ ์ด๋ฒˆ์— ์˜ฌ๋ผ์˜จ ๋ฌธ์ œ๋ฅผ ๋ณด๋‹ˆ

github.com

์ด๊ฒƒ์ด ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹ค ๋ผ๋Š” ์ฑ…์„ ๊ธฐ์ค€์œผ๋กœ ๊ณต๋ถ€๋ฅผ ํ•˜๊ธฐ๋กœ ํ–ˆ๋Š”๋ฐ, ์šฐ๋ฆฌ๋Š” ๊ทธ๋ƒฅ ๋ฒ”์œ„๋งŒ ๋งž์ถฐํ’€๊ณ  ์ฑ…์„ ์œ„์ฃผ๋กœ ๋ฌธ์ œ๋ฅผ ๊ฐ€์ ธ๊ฐ€์ง€ ์•Š๋Š”๋‹ค! ํŒŒ์ด์ฌ์ด ์•„๋‹ˆ๊ธฐ๋„ํ•˜๊ณ , ์ฑ…์— ์žˆ์ง€๋งŒ ์ธํ„ฐ๋„ท์—๋Š” ์—†๋Š” ๋ฌธ์ œ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ...

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

๋ฐ˜์‘ํ˜•