Skip to content

Conversation

@YooGyeongMo
Copy link
Collaborator

@YooGyeongMo YooGyeongMo commented Apr 10, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํŒŒ์ดํ”„ ์˜ฎ๊ธฐ๊ธฐ 1

2

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

dfs๋กœ ํ’€์—ˆ๋‹ค๊ฐ€ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋– ์„œ....
dp๋กœ ์ƒ๊ฐํ•ด๋‚ผ ์ˆ˜ ์—†์–ด์„œ ํ’€์ด๋ฅผ ๋ณธ 1์ธ...
์ด๊ฑด ์‚ผ์„ฑ ๊ธฐ์ถœ ์ถ•์—๋„ ๋ชป๋‚€๋‹ค๋Š” .... ใ… ใ… 
ํž~์†์•˜์ง€ใ…‹
ํž~์†์•˜์ง€ใ…‹

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฌธ์ œ๋ฅผ ๋จผ์ € ์ฝ์–ด๋ณด์‹œ๋ฉด-> ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„  ํŒŒ์ดํ”„๋ฅผ ์–ธ์ œ ์–ด๋–ค ํƒ€์ด๋ฐ์— ํ•ด๋‹น ํŒŒ์ดํ”„๋“ค์„ ๋†“์„ ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณผ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ๊ฐ€๋กœ ํŒŒ์ดํ”„๋Š” ์ด์ „ ํŒŒ์ดํ”„๊ฐ€ ๊ฐ€๋กœ ํ˜น์€ ๋Œ€๊ฐ์„  ํŒŒ์ดํ”„์ผ ๋•Œ ๋†“์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ์„ธ๋กœ ํŒŒ์ดํ”„๋Š” ์ด์ „ ํŒŒ์ดํ”„๊ฐ€ ์„ธ๋กœ ํ˜น์€ ๋Œ€๊ฐ์„  ํŒŒ์ดํ”„์ผ ๋•Œ ๋†“์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ท..
  3. ๋Œ€๊ฐ์„  ํŒŒ์ดํ”„๋Š” ์ด์ „ ํŒŒ์ดํ”„๊ฐ€ ์„ธ๋กœ ํ˜น์€ ๊ฐ€๋กœ ํ˜น์€ ๋Œ€๊ฐ์„  ํŒŒ์ดํ”„์ผ ๋•Œ ๋†“์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์œ„ ์กฐ๊ฑด๋“ค์„ ์ฒดํฌํ•˜์…จ๋‹ค๋ฉด, ๋ฒฝ์— ๋Œ€ํ•œ ์กฐ๊ฑด๋„ ๊ณ ๋ คํ•ด์ฃผ์…”์•ผํ•ฉ๋‹ˆ๋‹ค.

  • ํŠนํžˆ ๋Œ€๊ฐ์„  ํŒŒ์ดํ”„๋ฅผ ๋†“์œผ์‹ค ๋•Œ ํŒŒ์ดํ”„๋ฅผ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ์นธ๋ฟ๋งŒ ์•„๋‹Œ ํ•ด๋‹น์นธ ์™ผ์ชฝ, ์œ„์ชฝ๋„ ์ฒดํฌํ•ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ๋Š” ๊ฐ€๋กœ๋กœ ๋†“์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ ๋ฒˆ์งธ ์—ด์—๋Š” ์„ธ๋กœ๋กœ ๋†“์ผ ์ˆ˜๊ฐ€ ์—†์Œ์„ ์•Œ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค !

์ €๋Š” DP๋ฅผ ๊ณ ๋ คํ•  ๋•Œ 2์ฐจ์› ๋ฐฐ์—ด์— ๊ฐ€๋กœํŒŒ์ดํ”„, ๋Œ€๊ฐ์„  ํŒŒ์ดํ”„, ์„ธ๋กœํŒŒ์ดํ”„๊ฐ€ ์ด์ „์— ์ถ”๊ฐ€๋œ ๊ฒƒ๋“ค์„ ๋ˆ„์ ์‹œํ‚ค๋ฉด์„œ ์ฆ๊ฐ€ ์‹œ์ผœ์„œ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์„ ์„ ํƒํ–ˆ๋Š”๋ฐ์š”.

๋จผ์ € ๊ทธ๋ฆผ์„ ๋ณด์‹œ๋ฉด, 2์ฐจ์› ๋ฐฐ์—ด์˜ ํ•œ ์ขŒํ‘œ์— ์ด๋Ÿฌํ•œ dp๊ฐ€ ์žˆ๋‹ค๊ณ  ๋ณด์‹œ๋ฉด๋˜๋Š”๋ฐ์š”.
์ขŒํ‘œ

์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ดˆ๊ธฐํ™”๊ฐ€ ๋˜์‹ ๋‹ค๊ณ  ๋ณด๋ฉด๋ฉ๋‹ˆ๋‹ค.
dp์ขŒํ‘œ

์ด๋ ‡๊ฒŒ ๋˜์—ˆ์„๋•Œ 3์ฐจ์› ๋ฐฐ์—ด์„ ํ™œ์šฉํ•ด์„œ [ํŒŒ์ดํ”„ ์ข…๋ฅ˜][ํ–‰][์—ด] ์ด๋Ÿฐ์‹์œผ๋กœ ์ฒดํฌํ•˜๋ฉด์„œ ์ด์ „์— ์Œ“์ธ ๊ฐ’๋“ค์„ ๋ˆ„์ ํ•˜๊ณ  ์ตœ์ข…์ ์ธ [N - 1][N - 1] ์— ๋„๋‹ฌํ•˜๋Š” ํŒŒ์ดํ”„ ์ข…๋ฅ˜๋“ค์˜ ๋ฐฉ๋ฒ•์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค !

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ €๋Š” ์ด ๋ฌธ์ œ๋ฅผ ์ ‘ํ–ˆ์„ ๋•Œ dfs๊ฐ€ ์ƒ๊ฐ๋‚˜์„œ dfs๋กœ ์ ‘๊ทผํ•˜์˜€๋”๋‹ˆ, ๋„ˆ๋ฌด ๋งŽ์€ ์กฐ๊ฑด์ˆ˜๋ฅผ ์ฒดํฌํ•œ ๊ฒฐ๊ณผ ... ํ„ฐ์กŒ์Šต๋‹ˆ๋‹ค.. ์•...

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-04-10 แ„‹แ…ฉแ„’แ…ฎ 10 30 35

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์ œ๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ํ„ฐ์ง„ ์ฒซ ์•„์ด๋””์–ด์—์„œ ๋‚˜์˜จ dfs์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
ใ… ใ… 

def move_pipe(state,r,c):
    global answer

    if r == n-1 and c == n-1:
        answer+=1
        return

    can_move_h = c + 1 < n and board[r][c+1] == 0
    can_move_v = r + 1 < n and board[r+1][c] == 0

    if state != 1 and can_move_h:
        move_pipe(0, r, c + 1)
    if state != 0 and can_move_v:
        move_pipe(1, r+1, c)
    if c + 1 < n and r + 1 < n :
        if board[r][c+1] == 0 and board[r+1][c] == 0 and board[r+1][c+1] == 0:
            move_pipe(2, r + 1, c + 1)

move_pipe(0,0,1)
print(answer)

๊ทธ๋ž˜์„œ ์ €๋Š” dp๋กœ ํ‘ผ ๋ฌธ์ œ๋“ค์„ ๊ฒ€์ƒ‰ํ•˜์˜€๊ณ  ์•„๋ž˜์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ดํ•ดํ•˜๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค !
๋ธ”๋กœ๊ทธ ๋งํฌ

feat: ํŒŒ์ดํ”„ ์˜ฎ๊ธฐ๊ธฐ 1 ๋ฌธ์ œ ํ’€์ด ์ถ”๊ฐ€

- python ์–ธ์–ด ์‚ฌ์šฉ ์ด๋ฒˆ์—๋งŒ.
Comment on lines 10 to 26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์—„์ฒญ๋‚˜๊ฒŒ ๊น”๋”ํ•˜๋„ค์š” ์ „ ๋Œ€์ฒด ๋ญ˜ํ•˜๊ฒ ๋‹ค๊ณ  ๊ทธ๋ ‡๊ฒŒ ๋”๋Ÿฝ๊ฒŒ ๋œ๊ฑด์ง€.. ์ง„์งœ ๋ฌธ์ œ์—์„œ ๊ทธ๋ƒฅ ๋งํ•œ๋Œ€๋กœ ํ•ด์•ผํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ์กฐ๊ฑด ์ ์–ด์ค€๊ฒŒ ์ •๋‹ต์ฝ”๋“œ๋„ค์š”
์žฌ๊ท€๋ฅผ ์ œ๊ฐ€ ์ข€ ํ”ผํ•˜๋Š”๊ฒฝํ–ฅ์ด ์žˆ์–ด์„œ ๋”๋”์šฑ ๋”๋Ÿฌ์›Œ์ง„๊ฒƒ๊ฐ™์•„์š” . ์ €์žฅํ•ด๋†“๊ณ  ๋‹ค์Œ์— ํ’€์–ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๊ฑฐ๋Š” ์‚ฌ์‹ค dfs๋กœ ์ œ๊ฐ€ ํ’€๊ณ  ์‹œ๊ฐ„ ํ„ฐ์ง„ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹ file changed๊ฐ€์‹œ๋ฉด dp ์ฝ”๋“œ์žˆ์–ด์—ฌ.... ใ… 

Copy link
Collaborator

@bishoe01 bishoe01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ „ ์• ์ดˆ์— ์ด๊ฒŒ DP๋กœ ํ’€๋ฆด๊ฑฐ๋ผ๋Š” ์ƒ๊ฐ์„ ์ „ํ˜€ ๋ชปํ•ด์„œ, ์ผ๋‹จ ๊ฒฝ๋กœ์ฐพ๊ธฐ๋กœ ํ’€๋‹ค๊ฐ€ ๊ฐ์ด๋‚˜์˜ค๊ธฐ๋ฅผ ๋ฐ”๋žฌ๋Š”๋ฐ, ์˜ˆ์ œ๋Š” ๋‹ค ๋งž๋Š”๋ฐ ์ด๊ฒŒ ์‹œ๊ฐ„์ดˆ๊ณผ ์ด์ „์— ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋œจ๋”๋ผ๊ตฌ์š”..
์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ๋ชฉํ‘œ๋กœ ๊ณ„์† ์กฐ์ •ํ•ด์ฃผ๋‹ค๊ฐ€ ์‹คํŒจํ•˜๊ณ  ๋ณด๋‚ด์คฌ์Šต๋‹ˆ๋‹ค..

import Foundation

enum Direction {
    case down
    case right
    case diagonal
}

func solution(_ N: Int, _ graph: [[Int]]) {
    var map: [[Int]] = Array(repeating: Array(repeating: 0, count: N + 1), count: N + 1)

    var queue: [(d: Direction, x: Int, y: Int)] = [(.right, 1, 2)]
    var pointer = 0
    while queue.count > pointer {
        let (d, x, y) = queue[pointer]
        map[x][y] += 1

//        var nextMoveArr: [(dd: Direction, dx: Int, dy: Int)] = [(.diagonal, 1, 1)] //๋Œ€๊ฐ์„ ๊ฐ€๋ ค๋ฉด ์‚ฌ๋ฐฉ์ด ๋‹ค 0์ด์–ด์•ผํ•œ๋Œ€๋„ค ..
        var nextMoveArr: [(dd: Direction, dx: Int, dy: Int)] = []
        if x < N, y < N,
           graph[x - 1][y] == 0,
           graph[x][y - 1] == 0,
           graph[x][y] == 0
        {
            nextMoveArr.append((.diagonal, 1, 1))
        }
        switch d {
            case .right:
                if y < N, graph[x - 1][y] == 0 {
                    nextMoveArr.append((.right, 0, 1))
                }

            case .down:
                if x < N, graph[x][y - 1] == 0 {
                    nextMoveArr.append((.down, 1, 0))
                }

            case .diagonal:
                if y < N, graph[x - 1][y] == 0 {
                    nextMoveArr.append((.right, 0, 1))
                }
                if x < N, graph[x][y - 1] == 0 {
                    nextMoveArr.append((.down, 1, 0))
                }
        }
//        print(x, y, d, nextMoveArr)
        for item in nextMoveArr {
            let (nd, nx, ny) = (item.dd, x + item.dx, y + item.dy)
            if isRange(nx, ny) {
                queue.append((d: nd, x: nx, y: ny))
            }
        }
        pointer += 1
    }
    print(map[N][N])
//    print(map)
    func isRange(_ x: Int, _ y: Int) -> Bool {
        if x < 1 || x > N || y < 1 || y > N || graph[x - 1][y - 1] == 1 {
            return false
        }
        return true
    }
}

@YooGyeongMo
Copy link
Collaborator Author

YooGyeongMo commented Apr 15, 2025

@bishoe01 ์˜ค dp์ฒ˜๋Ÿผ ์ ‘๊ทผ์„ ์—„์ฒญ ์ž˜ํ•˜์…จ๋Š”๋ฐ์š” ??? ์ €๋„ dp๋Š” ๋ฌด์Ÿˆ๊ฒŒ ํž˜๋“ค์–ด์„œ ํšŒํ”ผ์ค‘์ธ๋ฐ ์ด๊ฒŒ ๊ฑธ๋ฆด ์ค„์ด์•ผ...
ํ•€์Œค ์ œ๊ฐ€ ์ˆ˜์ •์„ ์•ˆํ–ˆ๋„ค์˜ˆ ํ•ด๋‹น ์ฝ”๋“œ๋Š” dfs๊ณ  ๋ฉ”๋ชจ์ด์ œ์ด์…˜์œผ๋กœ ํ‘ผ๊ฑฐ ๋‹ค์‹œ ์ˆ˜์ •ํ•ด๋†จ์œผ์˜ˆ
file changed ์ฝ”๋“œ๋ณด์‹œ๊ณ  ํ•œ๋ฒˆ ๋””ํ”ผ๋กœ ํ’€์–ด๋ณด์‹œ์ง€์˜ˆ

@sep037 sep037 removed their request for review April 22, 2025 03:15
Copy link
Member

@alstjr7437 alstjr7437 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„๋‹ˆ ์ €๋„ ๋ณด๋“œ๋ฌธ์ œ๊ธธ๋ž˜ ์–ด!? BFS, DFS ์ด๋ ‡๊ฒŒ ์ƒ๊ฐํ•˜๊ณ  ํ’€์—ˆ๋Š”๋ฐ
์‹œ๊ฐ„์ดˆ๊ณผ์˜€๋„ค์š”,,, ๋ณด๋“œ๋ผ๊ณ  ๋ฌด์กฐ๊ฑด BFS๋ผ๋Š” ์Šต๊ด€์„ ๋ฒ„๋ ค์•ผํ• ํ…๋ฐ..

๋”ฐ๋กœ ์‹œ๊ฐ„์„ ๋„ˆ๋ฌด ๋งŽ์ด์จ์„œ ๊ฒฐ๊ตญ ์ธํ„ฐ๋„ท๊ณผ ํ•€, ๋ฐ๋ฏธ์•ˆ์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด๋ฒ„๋ ธ๋„ค์š” ใ… 

  • ์ธํ„ฐ๋„ท์—์„œ ์ฐธ๊ณ ํ–ˆ๋Š”๋ฐ ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•œ ๊ฒƒ ๊ฐ™์•„์„œ ์ฒจ๋ถ€ํ•ด์š”!
import Foundation

struct Queue<T> {
    var enq = [T]()
    var pointer = -1
    var counter = 0
    
    var isEmpty: Bool {
        counter == 0
    }
    
    mutating func append(_ element: T) {
        enq.append(element)
        counter += 1
    }
    mutating func popFirst() -> T {
        pointer += 1
        counter -= 1
        return enq[pointer]
    }
}

enum Direction {
    case horizontal
    case vertical
    case diagonal
}

let N = Int(readLine()!)!
var arr = [[Int]]()
var q = Queue<(Direction, Int, Int)>()
var answer = 0

for _ in 0..<N {
    arr.append(readLine()!.split(separator: " ").map{Int($0)!})
}

func myfunc(_ dir: Direction, _ x: Int, _ y: Int){
    if x == N-1 && y == N-1 {
        answer += 1
        return
    }
    
    var hubo = [(Direction, Int, Int)]()
    switch dir {
        case .horizontal:
            hubo = [(Direction.horizontal, x, y+1), (Direction.diagonal, x+1, y+1)]
        case .vertical:
            hubo = [(Direction.vertical, x+1, y), (Direction.diagonal, x+1, y+1)]
        case .diagonal:
            hubo = [(Direction.horizontal, x, y+1), (Direction.vertical, x+1, y), (Direction.diagonal, x+1, y+1)]
    }
    for (dire, X, Y) in hubo {
        if X >= N || Y >= N {
            continue
        }

        switch dire {
            case .horizontal, .vertical:
                if !(arr[X][Y] == 0) {
                    continue
                }
            case .diagonal:
                if !(arr[X][Y] == 0 && arr[X-1][Y] == 0 && arr[X][Y-1] == 0) {
                    continue
                }
        }
        myfunc(dire, X, Y)
    }
}

myfunc(Direction.horizontal, 0, 1)
print(answer)

Comment on lines +4 to +6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜คํ˜ธ ์ด๋ ‡๊ฒŒ ๋Œ€๊ฐ์„ ๊นŒ์ง€ ์ €์žฅํ•˜์…จ๊ตฐ์š”

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ์™€ Enum์˜ ์กฐํ™”,

switch dir {
        case .horizontal:
            hubo = [(Direction.horizontal, x, y+1), (Direction.diagonal, x+1, y+1)]
        case .vertical:
            hubo = [(Direction.vertical, x+1, y), (Direction.diagonal, x+1, y+1)]
        case .diagonal:
            hubo = [(Direction.horizontal, x, y+1), (Direction.vertical, x+1, y), (Direction.diagonal, x+1, y+1)]
    }
    
    ``` ํ•ด๋‹น ๋ฐฉ์‹ ๋„ˆ๋ฌด ์ข‹์€๊ฑฐ๊ฐ™์•„์š” 
    
๋Œ€๊ฐ์„  ์ €์žฅ ๋ฐฉ์‹์€ DP์—์„œ ๋Œ€๊ฐ์„ ์„ ๋‹ด์„๋• ์–ด๋–ค ๋ฐฉ์‹์ด ์žˆ์„์ง€ ๊ณ ๋ฏผ์„ ๋ชปํ–ˆ์–ด์„œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ–ˆ์—ˆ๋„ค์š” ใ…œใ…œ
    

Copy link
Collaborator

@MuchanKim MuchanKim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฐ๋ฏธ์•ˆ ์ฝ”๋“œ ํ•œ๋ฒˆ ํ›‘๊ณ  ํ’€์–ด์„œ ๊ทธ๋Ÿฐ์ง€ ์ด๋™ ๋กœ์ง์€ ๊ฑฐ์˜ ๋˜‘๊ฐ™๋„ค์š”! ์ „ DFS๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
๋…ผ๋ฆฌ์ ์ธ ๊ธฐ๋Šฅ? ๋‹จ์œ„๋กœ ๋‚˜๋ˆ ์„œ ํ’€์–ด๋ดค๋Š”๋ฐ ๋‚˜๋ฆ„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ž€ ์ƒ๊ฐ๋„ ๋“œ๋„ค์š”. ๊ธฐ์กด์—” ํ’€์–ด๋†“๊ณ  ๋ถ„๋ฆฌ!
+ ๊ธธ์ด typealias ํ™œ์šฉํ•˜๋Š” ๊ฑฐ ๋ณด๊ณ  ๊ฐ€๋…์„ฑ ์ข‹์•„์ง€๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์ €๋„ ํ•จ ์จ๋ด„์š”

๊ธ€๊ตฌ ์ „ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ํŒŒ์ดํ”„๋ฅผ tail๊ณผ head ๋‘ ์ ์œผ๋กœ ์ง๊ด€์ ์œผ๋กœ ํ‘œํ˜„ํ–ˆ๋Š”๋ฐ, ๋ฐ๋ฏธ์•ˆ์€ head ์œ„์น˜์™€ ๋ฐฉํ–ฅ state๋งŒ์œผ๋กœ ํŒŒ์ดํ”„๋ฅผ ํ‘œํ˜„ํ•˜์…จ๊ตฐ์š”.. ๊ณจ๋“œ ๋ฌธ์ œ ๋งค์šฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ํ’€์ด์‹œ๊ฐ„: 2์‹œ๊ฐ„+@

import Foundation

typealias Board = [[Int]]
typealias Position = (tail: (Int, Int), head: (Int, Int))

let boardSize: Int = Int(readLine()!)!

func getBoard(_ size: Int) -> Board {
    var newBoard: Board = []
    for _ in 0..<size {
        let row = readLine()!.split(separator: " ").map { Int($0)! }
        newBoard.append(row)
    }
    return newBoard
}

// ํŒŒ์ดํ”„์˜ ํ˜„์žฌ ๋ฐฉํ–ฅ ํ™•์ธ
func getDirection(_ position: Position) -> Int {
    let (tailRow, tailCol) = position.tail
    let (headRow, headCol) = position.head
    
    // ๊ฐ€๋กœ ๋ฐฉํ–ฅ (0)
    if tailRow == headRow && tailCol + 1 == headCol {
        return 0
    }
    // ์„ธ๋กœ ๋ฐฉํ–ฅ (1)
    else if tailRow + 1 == headRow && tailCol == headCol {
        return 1
    }
    // ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ (2)
    else {
        return 2
    }
}

// ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธ
func canMove(_ position: Position, _ direction: Int, _ board: Board, _ N: Int) -> Bool {
    let (headRow, headCol) = position.head
    
    // ๊ฐ€๋กœ ์ด๋™
    if direction == 0 {
        let newCol = headCol + 1
        return newCol < N && board[headRow][newCol] == 0
    }
    // ์„ธ๋กœ ์ด๋™
    else if direction == 1 {
        let newRow = headRow + 1
        return newRow < N && board[newRow][headCol] == 0
    }
    // ๋Œ€๊ฐ์„  ์ด๋™
    else {
        let newRow = headRow + 1
        let newCol = headCol + 1
        return newRow < N && newCol < N && 
               board[headRow][newCol] == 0 && 
               board[newRow][headCol] == 0 && 
               board[newRow][newCol] == 0
    }
}

// ํŒŒ์ดํ”„ ์ด๋™
func movePipe(_ position: Position, _ direction: Int) -> Position {
    let (headRow, headCol) = position.head
    
    let tail = position.head
    var head: (Int, Int) = (0, 0)
    
    // ๊ฐ€๋กœ ์ด๋™
    if direction == 0 {
        head = (headRow, headCol + 1)
    }
    // ์„ธ๋กœ ์ด๋™
    else if direction == 1 {
        head = (headRow + 1, headCol)
    }
    // ๋Œ€๊ฐ์„  ์ด๋™
    else {
        head = (headRow + 1, headCol + 1)
    }
    
    return (tail, head)
}

func solution(_ N: Int, _ board: Board) -> Int {
    // ์‹œ์ž‘ ํŒŒ์ดํ”„ ์œ„์น˜ - ์ฒ˜์Œ์—๋Š” (0,0)์™€ (0,1) ์œ„์น˜์— ํŒŒ์ดํ”„๊ฐ€ ๋†“์—ฌ ์žˆ์Œ!
    let startPosition: Position = ((0, 0), (0, 1))
    var count = 0
    
    func dfs(_ position: Position) {
        // ํŒŒ์ดํ”„ ๋์ด (N-1, N-1)์— ๋„๋‹ฌํ•˜๋ฉด ์นด์šดํŠธ ์ฆ๊ฐ€
        let (headRow, headCol) = position.head
        if headRow == N-1 && headCol == N-1 {
            count += 1
            return
        }
        
        let currentDirection = getDirection(position)
        
        // ํ˜„์žฌ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๊ฐ€๋Šฅํ•œ ์ด๋™ ๋ฐฉํ–ฅ ํ™•์ธ
        if currentDirection == 0 { // ๊ฐ€๋กœ
            // ๊ฐ€๋กœ๋กœ ์ด๋™
            if canMove(position, 0, board, N) {
                dfs(movePipe(position, 0))
            }
            // ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๋™
            if canMove(position, 2, board, N) {
                dfs(movePipe(position, 2))
            }
        }
        else if currentDirection == 1 { // ์„ธ๋กœ
            // ์„ธ๋กœ๋กœ ์ด๋™
            if canMove(position, 1, board, N) {
                dfs(movePipe(position, 1))
            }
            // ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๋™
            if canMove(position, 2, board, N) {
                dfs(movePipe(position, 2))
            }
        }
        else { // ๋Œ€๊ฐ์„ 
            // ๊ฐ€๋กœ๋กœ ์ด๋™
            if canMove(position, 0, board, N) {
                dfs(movePipe(position, 0))
            }
            // ์„ธ๋กœ๋กœ ์ด๋™
            if canMove(position, 1, board, N) {
                dfs(movePipe(position, 1))
            }
            // ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๋™
            if canMove(position, 2, board, N) {
                dfs(movePipe(position, 2))
            }
        }
    }
    
    dfs(startPosition)
    return count
}

let board = getBoard(boardSize)
let result = solution(boardSize, board)
print(result)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ž…๋ ฅ ๊ฐ„๊ฒฐํ•˜๋„ค์˜ˆ

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์™€ ์‹œ๊ฐ„ ์•ˆํ„ฐ์ง€๋˜๊ฐ€์š” ? DFSํ–ˆ์„๋•Œ ์ €๋Š” ํ„ฐ์ ธ์„œ dp์ธ๊ฑธ์•Œ์•˜๋Š”๋ฐ..ใ… ใ… ใ… 

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌด ์ฝ”๋“œ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค !

struct Pipe {
    var tail: (Int, Int)
    var head: (Int, Int)
    
    var direction: Int {
        if tail.0 == head.0 && tail.1 + 1 == head.1 {
            return 0
        } else if tail.0 + 1 == head.0 && tail.1 == head.1 {
            return 1
        } else {
            return 2
        }
    }
    
    func moved(to newDirection: Int) -> Pipe {
        let (r, c) = head
        let newHead: (Int, Int)
        switch newDirection {
        case 0: newHead = (r, c + 1)
        case 1: newHead = (r + 1, c)
        default: newHead = (r + 1, c + 1)
        }
        return Pipe(tail: head, head: newHead)
    }
}
``` ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ํŒŒ์ดํ”„๋ฅผ ๊ตฌ์กฐ์ฒด๋กœ ๋‘์‹œ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?

์ดํ›„ 

```swift
let currentDirection = pipe.direction 

์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋…์„ฑ๋„ ๋†’์•„์งˆ๊บผ๊ฐ™์Šต๋‹ˆ๋‹ค !

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ถ”๊ฐ€๋กœ

enum Direction: Int {
    case horizontal = 0, vertical = 1, diagonal = 2
}

``` ์ด๋Ÿฐ์‹์œผ๋กœ๋„ ๊ฐ€๋Šฅํ• ๊ฑฐ๊ฐ™์•„์š” !

Copy link
Member

@giljihun giljihun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C2์ด์Šˆ๋กœ ์•„์ง ์ €๋งŒ์˜ ์ •๋‹ต์ฝ”๋“œ๋Š” ์ œ์ถœํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค ใ… ใ… 
+
ํŒŒ์ด์ฌ ์ฝ”๋“œ ์ง„์งœ ๋ณด๊ธฐ ์ข‹๋„ค์š” ใ…‹ใ…‹ใ…‹
dfs๋„ ๊ฒฐํ•ฉ๋œ ํƒ‘๋‹ค์šด dp?๊ฐ™์•„์š”.
๋Œ€๊ฐ์„  ์ˆœํšŒํ•˜๋ฉด์„œ ์ธ์ ‘ํ•œ ์„ธ ์นธ์ด ๋‹ค empty ์กฐ๊ฑด ์ฒ˜๋ฆฌ๋„ ์•ผ๋ฌด์ง€๋„ค์š”!!!

@YooGyeongMo YooGyeongMo merged commit 788140c into main Apr 24, 2025
2 checks passed
@alstjr7437 alstjr7437 deleted the 1-YooGyeongMo branch May 23, 2025 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants