Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags more
Archives
Today
Total
관리 메뉴

개발의변화

프로그래머스 LV3 미로 탈출 명령어 본문

카테고리 없음

프로그래머스 LV3 미로 탈출 명령어

refindmySapporo 2024. 2. 14. 20:35
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/150365

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

function solution(n, m, x, y, r, c, k) {
    let answer = "z".repeat(k)
    //미로의 세로길이 n, 미로의 가로길이 m,
    //출발위치 x,y
    //탈출위치 r,c
    //탈출이동 거리 k
    x = x - 1
    y = y - 1
    r = r - 1
    c = c - 1
    if (Math.abs(Math.abs(x-r)+Math.abs(y-c)-k) % 2 == 1 ||Math.abs(x-r)+Math.abs(y-c)>k ) return "impossible"
    const arr=Array.from({length:n},() =>Array.from({length:m},()=>'.'));
    const q = [[x,y,0,""]]
    const directions = [[1,0,"d"],[0,-1,"l"],[0,1,"r"],[-1,0,"u"],]
    const keyword = ""
    function letsgetit(x,y,count,keyword) {
        if (count == k){
            if(x==r && y==c) answer = keyword
            return
        }
        if(answer!=="z".repeat(k)) return
        if(Math.abs(x-r)+Math.abs(y-c)+count>+k) return
        for (const direction of directions) {
            const dx = x+direction[0]
            const dy = y+direction[1]
            if (0<=dx && dx < arr.length && 0<=dy && dy<arr.length) {
                letsgetit(dx,dy,count+1,keyword+direction[2])
            }
        }
        return
     }
    letsgetit(x,y,0,"")
    return answer
}

 

k번째 재귀에서 어떻게 처리할 수 있을까 

좋은 문제였다.

반응형