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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 2529๋ฒˆ - ๋ถ€๋“ฑํ˜ธ (feat. ๋ฐฑํŠธ๋ž˜ํ‚น, DFS)

๊ฐ์ž ๐Ÿฅ” 2023. 1. 29. 17:30
๋ฐ˜์‘ํ˜•

๐ŸŸ  TIL - Character๋Š” ๋ฐ”๋กœ Int๋กœ ๋ณ€ํ™˜์ด ์•ˆ๋œ๋‹ค.

์•„์˜ค,, index์—๋Ÿฌ๊ฐ€ ์ž๊พธ ๋‚˜๋ฉด์„œ ์• ๋งคํ•˜๊ฒŒ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ธธ๋ž˜ ์ด ์˜ค๋ฅ˜๋ฅผ ์ฐพ๋Š”๋ฐ ํ•œ์ฐธ์ด ๊ฑธ๋ ธ๋‹ค . ์ด๊ฒŒ ๋ญ๋ผ๊ณ !!!!! ๊ฒฐ๋ก ์€,, ์ œ๋ชฉ ๊ทธ๋Œ€๋กœ๋‹ค. Char ๋ฌธ์ž๋Š” ๋ฐ”๋กœ Int๋กœ ๋ณ€ํ™˜์ด ์•ˆ๋œ๋‹ค. ์ฆ‰, "2" ์˜ type์ด char ๋ผ๋ฉด, String์œผ๋กœ ๋ฐ”๊พธ๊ณ  Int๋กœ ๋ฐ”๊ฟ”์•ผ ์ •์ƒ์ ์œผ๋กœ int๋กœ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์—ฌ๊ธฐ์„œ,,, 30๋ถ„? 50๋ถ„? ์ •๋„ ์˜ค๋ฅ˜์ฐพ๋Š”๋‹ค๊ณ  ์ฝ”๋“œ ๋œฏ์–ด๋ณด๊ณ  ์ฝ”๋“œ ์ด๋ฆฌ์ €๋ฆฌ ๋ฐ”๊ฟ”๋ณด๊ณ  ๋‚œ๋ฆฌ๋ฅผ ์ณค๋‹ค ใ…กใ…ก ์ด์ œ ์•Œ์•˜์œผ๋‹ˆ ์ œ๋Œ€๋กœ ๊ธฐ์–ตํ•ด๋‘๊ธธ.

var charNum: Character = "2"

Int(charNum)! // โŒ ์—๋Ÿฌ
Int(String(charNum))! // โœ… ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค.

 

๐ŸŸ  ๋ฌธ์ œ

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

 

2529๋ฒˆ: ๋ถ€๋“ฑํ˜ธ

์—ฌ๋Ÿฌ๋ถ„์€ ์ œ์‹œ๋œ ๋ถ€๋“ฑํ˜ธ ๊ด€๊ณ„๋ฅผ ๋งŒ์กฑํ•˜๋Š” k+1 ์ž๋ฆฌ์˜ ์ตœ๋Œ€, ์ตœ์†Œ ์ •์ˆ˜๋ฅผ ์ฒซ์งธ ์ค„๊ณผ ๋‘˜์งธ ์ค„์— ๊ฐ๊ฐ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ๋‹จ ์•„๋ž˜ ์˜ˆ(1)๊ณผ ๊ฐ™์ด ์ฒซ ์ž๋ฆฌ๊ฐ€ 0์ธ ๊ฒฝ์šฐ๋„ ์ •์ˆ˜์— ํฌํ•จ๋˜์–ด์•ผ ํ•œ๋‹ค. ๋ชจ๋“  ์ž…๋ ฅ

www.acmicpc.net

 

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

์šฐ์„ ,, ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ๋ณด๊ณ  ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผํ•˜๋‚˜,, ์ •๋ง ์–ด๋ ค์› ๋‹ค. ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•ด์•ผํ• ์ง€ ๋ชฐ๋ž๊ณ , ์•ฝ๊ฐ„์˜ ํžŒํŠธ๋ฅผ ์ฐพ์•„๋ณด์•˜๋‹ค. ๊ทธ๋ƒฅ,, ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•˜๋ฉด ์‰ฌ์šด ๋ฌธ์ œ์˜€๊ณ , 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ๋Œ๋ฉด์„œ ์ „์— ์ˆซ์ž์™€ ํ˜„์žฌ ๋ถ€๋“ฑํ˜ธ๋ฅผ ๋น„๊ตํ•ด๋ณด๋Š”๊ฒŒ ๋์ด์—ˆ๋‹ค.

dfs๋ฅผ ๋๋‚ด๋Š” ์กฐ๊ฑด์€ depth == n+1 ๋กœ ๋‘์—ˆ๋‹ค. ๋งŒ์•ฝ 2๊ฐ€ ์ž…๋ ฅ๋ผ์„œ ๋ถ€๋“ฑํ˜ธ๊ฐ€ 2๊ฐœ์ด๋ฉด, 3๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋น„๊ตํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทผ๋ฐ,,, depth๋„ ์‚ฌ์‹ค ํ•„์š”์—†๋‹ค. ๊ทธ๋ƒฅ ๊ตฌํ•˜๋ ค๋Š” answer์˜ ๊ธธ์ด๋ฅผ ๋ณด๋ฉด ๋œ๋‹ค!

answer์— ์กฐ๊ฑด์—๋ถ€ํ•ฉํ•˜๋Š” ์ˆซ์ž๋“ค์„ ํ•˜๋‚˜์”ฉ ๋ถ™์—ฌ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€๋ฉด๋œ๋‹ค. ํ˜„์žฌ answer์ด "0"์ด๊ณ , ๋น„๊ตํ•˜๋ ค๊ณ  ํ•˜๋Š” i ๊ฐ€ "2", ์ž…๋ ฅ๋ฐ›์€ ๋ถ€๋“ฑํ˜ธ๊ฐ€ "<"๋ผ๋ฉด "0<2" ๋ฅผ ๋น„๊ตํ•ด์„œ true ๋ฉด visited ์ฒ˜๋ฆฌํ•ด์ฃผ๊ณ , ๋‹ค์‹œ ์ˆœํšŒ๋Œ๊ธฐ์œ„ํ•ด์„œ dfs ์žฌ๊ท€๋ฅผ ๋Œ๋ ค์ฃผ๋ฉด ๋œ๋‹ค! ์ด ๊ณผ์ •์„ ๊ทธ๋ฆผ์œผ๋กœ ๊ทธ๋ ค๋ดค๋‹ค. (๋‚˜๋งŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๊ทธ๋ฆผ์ผ์ˆ˜๋„...?)

(๊ทธ๋ฆผ์—๋Š” depth๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ ์ฝ”๋“œ๋ฅผ ์งœ๋‹ค ๋ณด๋‹ˆ๊นŒ depth๊ฐ€ ๊ณง answer์˜ ๊ธธ์ด์™€ ๊ฐ™์•„์„œ depth ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ญ์ œํ•œ์ฑ„๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.)

์ •๋ฆฌํ•ด๋ณด์ž.

  1. 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ „๋ถ€ ์ˆœํšŒ (์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— 9๋ถ€ํ„ฐ 0๊นŒ์ง€ ๋Œ๋ฆด๊ฑฐ์ž„)
  2. visited[i] ํ™•์ธ, false์ด๋ฉด answer์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์„ ๊ฐ–์ถ˜๊ฒƒ!
  3. i๋ฅผ answer์— ํ•ฉ์ณ์ฃผ๊ธฐ ์ „์— ์ž…๋ ฅ๋ฐ›์€ ๋ถ€๋“ฑํ˜ธ, answer ๋งจ ๋งˆ์ง€๋ง‰์— ์กด์žฌํ•˜๋Š” ์ˆซ์ž์™€ ๋น„๊ตํ•ด์„œ ์ ๋‹นํ•œ์ง€ ํŒ๋ณ„
  4. ์•„๋‹ˆ๋ผ๋ฉด ์•„๋ฌด์ผ๋„ ๋ฒŒ์–ด์ง€์ง€ ์•Š๊ณ  ๋‹ค์Œ i๋กœ ๋„˜๊ฒจ์ฃผ๊ณ , ๋งŒ์•ฝ ์กฐ๊ฑด์ด ๋ถ€ํ•ฉํ•˜๋‹ค๋ฉด answer์— ํ•ฉ์ณ์ฃผ๊ธฐ
  5. ๋งŒ์•ฝ answer์˜ ๊ธธ์ด๊ฐ€ n+1๊ณผ ๊ฐ™๋‹ค๋ฉด answer์ด ๋  ์ˆ˜ ์žˆ๋Š” ํ›„๋ณด๋‹ˆ๊นŒ, answerList์— ๋„ฃ์–ด์ฃผ๊ณ  return ํ•ด์ค˜์„œ dfs๋ฅผ ๋๋‚ด์ฃผ๊ธฐ
  6. 9๋ถ€ํ„ฐ 0๊ฐ€์ง€ ๋Œ๋ฆฐ ๊ฒฐ๊ณผ๋ฌผ์ด answerList์ด๊ธฐ ๋•Œ๋ฌธ์— ์–ด์ฉ”์ˆ˜์—†์ด ๊ฐ€์žฅ ํฐ ์ˆ˜๊ฐ€ answerList์˜ ๋งจ์•ž์—, ๊ฐ€์žฅ ์ž‘์€์ˆ˜๊ฐ€ answerList์˜ ๋งจ๋’ค์— ์กด์žฌํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, ๋งจ์•ž๊ณผ ๋งจ๋’ค์—์žˆ๋Š” ๊ฒƒ๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•ด์ฃผ์ž.

 

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

import Foundation

let n = Int(readLine()!)!
let inequality = readLine()!.split(separator: " ").map{ String($0) }
var answer = ""
var answerList = [String]()
var visited = Array(repeating: false, count: 10)

private func check(num1: Int, num2: Int, inequal: String) -> Bool {
    if inequal == "<" {
        if num1 < num2 {
            return true
        } else {
            return false
        }
    } else {
        if num1 > num2 {
            return true
        } else {
            return false
        }
    }
}

private func dfs(answer: String) {
    if answer.count == n+1 {
        answerList.append(answer)
        return
    }

    for i in stride(from: 9, through: 0, by: -1) {
        if !visited[i] {
            if answer == "" || check(num1: Int(String(Array(answer)[answer.count-1]))!,
                                     num2: i,
                                     inequal: inequality[answer.count-1]) {
                visited[i] = true
                dfs(answer: answer + String(i))
                visited[i] = false
            }
        }
    }

}

dfs(answer: answer)

for i in 0..<2 {
    if i == 0 {
        print(answerList[i])
    } else {
        print(answerList[answerList.count-1])
    }
}

 

๋ฐ˜์‘ํ˜•