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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1759๋ฒˆ - ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ (๋‘๋ฒˆ์งธํ’€์ด) (feat. DFS, ๋ฐฑํŠธ๋ž˜ํ‚น, ๊ณจ๋“œ5)

๊ฐ์ž ๐Ÿฅ” 2023. 1. 18. 15:11
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ

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

 

1759๋ฒˆ: ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ

์ฒซ์งธ ์ค„์— ๋‘ ์ •์ˆ˜ L, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (3 ≤ L ≤ C ≤ 15) ๋‹ค์Œ ์ค„์—๋Š” C๊ฐœ์˜ ๋ฌธ์ž๋“ค์ด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์ด๋ฉฐ, ์ค‘๋ณต๋˜๋Š” ๊ฒƒ์€ ์—†๋‹ค.

www.acmicpc.net

 

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

๋ฌธ์ œ๋ฅผ ๊ฐ™์€ ๋ถ€๋ฅ˜๋ผ๋ฆฌ ๋ชจ์•„ ํ’€์–ด์„œ ๊ทธ๋Ÿฐ๊ฐ€, ํ™•์‹คํžˆ ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์–ด๋–ป๊ฒŒ ํ’€์ง€ ๋”ฑ ๋– ์˜ฌ๋ž๋‹ค. ์˜ˆ์ „์—๋Š” ์•ˆ๊ทธ๋žซ๋Š”๋ฐ,,, ์—ญ์‹œ ํ•œ ๋ถ„์•ผ๋งŒ ์ฃฝ์–ด๋ผ ํŒŒ๊ณ , ๊ทธ ๋ถ„์•ผ์— ์ต์ˆ™ํ•ด์งˆ๋•Œ๊นŒ์ง€ ๋ฌธ์ œ๋“ค์„ ๋ˆˆ์— ๋„ฃ๋Š”๊ฒŒ ์ค‘์š”ํ•œ๊ฒƒ ๊ฐ™๋‹ค.

์ผ๋‹จ, ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅ๋˜์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์˜ค๋ฆ„ ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ์ด์šฉํ•ด์•ผํ•˜๊ณ , ์•ž์— ์ง€๋‚˜์™”๋˜ ๋ฐฐ์—ด์š”์†Œ๋“ค์€ ๋ณด์ง€ ์•Š๋„๋ก ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผํ•œ๋‹ค. ์ด ๋ถ€๋ถ„์€, ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ index ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋ฉด์„œ, ์ง€๊ธˆ ๋ณด๋‹ค๋Š” ํ•˜๋‚˜ ๋’ค์— ์žˆ๋Š” ์š”์†Œ๋งŒ ๋ฐ”๋ผ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ค„ ๊ฒƒ์ด๋‹ค.
  2. ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— visited ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ํ•˜๋‚˜์”ฉ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.
  3. dfs๋ฅผ ์žฌ๊ท€๋กœ ๋Œ๋ฆฌ๋ฉด์„œ ์กฐ๊ฑด์œผ๋กœ ํ™•์ธํ•ด์•ผํ•˜๋Š” ๊ฒƒ์ด 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ๋ชจ์Œ์ด ํ•œ๊ฐœ ์ด์ƒ ์กด์žฌํ•˜๋Š”์ง€, ์ž์Œ์ด ๋‘๊ฐœ ์ด์ƒ ์กด์žฌํ•˜๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ์ž…๋ ฅ๋ฐ›์€ L๊ณผ ๊ธธ์ด๊ฐ€ ๊ฐ™์€์ง€! ์ด๋ ‡๊ฒŒ ์„ธ๊ฐ€์ง€๋ฅผ ํŒ๋‹จํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

์ด ํ’€์ด์™€ ๋น„์Šทํ•œ, ๋” ์ž์„ธํ•œ ํ•ด์„์„ ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜ ๋ฌธ์ œ๋ฅผ ์ฐธ๊ณ ํ•ด๋ณด์ž.

https://didu-story.tistory.com/381

 

[๋ฐฑ์ค€] (Swift) 18290๋ฒˆ - NM๊ณผ K(1) (feat. ๋ธŒ๋ฃจํŠธํฌ์Šค, DFS, ๋ฐฑํŠธ๋ž˜ํ‚น)

๐ŸŸ  ๋ฌธ์ œ https://www.acmicpc.net/problem/18290 18290๋ฒˆ: NM๊ณผ K (1) ํฌ๊ธฐ๊ฐ€ N×M์ธ ๊ฒฉ์žํŒ์˜ ๊ฐ ์นธ์— ์ •์ˆ˜๊ฐ€ ํ•˜๋‚˜์”ฉ ๋“ค์–ด์žˆ๋‹ค. ์ด ๊ฒฉ์žํŒ์—์„œ ์นธ K๊ฐœ๋ฅผ ์„ ํƒํ•  ๊ฒƒ์ด๊ณ , ์„ ํƒํ•œ ์นธ์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’

didu-story.tistory.com

 

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

import Foundation

var lc = readLine()!.split(separator: " ").map{ Int($0)! }
var l = lc[0]
var c = lc[1]
var aeiou = ["a", "e", "i", "o", "u"]
var arr = readLine()!.split(separator: " ").map{ String($0) }.sorted(by: <)
var visited = Array(repeating: false, count: c)
var password = [String]()

private func dfs(index: Int) {
    // ์ตœ์†Œ ๋ชจ์Œ ํ•œ๊ฐœ ์ด์ƒ, ์ตœ์†Œ ์ž์Œ ๋‘๊ฐœ ์ด์ƒ
    if password.count == l {
        var containAeiou = false
        var consonantCnt = 0

        for i in password {
            if aeiou.contains(i) {
                containAeiou = true
            } else {
                consonantCnt += 1
            }
        }

        if consonantCnt >= 2, containAeiou {
            print(password.joined(separator: ""))
        }
        return
    }

    for k in index..<c {
        if !visited[k] {
            visited[k] = true
            password.append(arr[k])
            dfs(index: k)
            visited[k] = false
            password.removeLast()
        }
    }
}

dfs(index: 0)

 

๋ฐ˜์‘ํ˜•