[백준 5014번] BFS(너비 우선 탐색) - 스타트링크

김민지·2023년 7월 13일
0

냅다 시작 백준

목록 보기
57/118

✨ 문제 ✨

✨ 정답 ✨

const { count } = require("console");
const fs = require("fs");
const { nextTick } = require("process");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./예제.txt";
let input = fs.readFileSync(filePath).toString().trim();

// const fs = require('fs'); 
// let input = fs.readFileSync("/dev/stdin").toString().trim();

let [F, S, G, U, D] = input.split(' ').map((el) => +el)


// 정답지 보고 푼 답
// const solution = (F, S, G, U, D) => {
//     let visited = new Array(F + 1).fill(false);
//     let queue = [[S, 0]];
//     while (queue.length) {
//         let [current, count] = queue.shift();
//         if (visited[current]) continue;
//         if (current === G) return count;
//         for (let stairs of [current + U, current - D]) {
//             stairs >= 1 &&
//                 stairs <= F &&
//                 !visited[current] &&
//                 queue.push([stairs, count + 1]);
//         }
//         visited[current] = true
//     }
//     return "use the stairs"
// }

// console.log(solution(F, S, G, U, D))



// 두 번째로 푼 답
// const solution = (F, S, G, U, D) => {
//     let visited = new Array(F + 1).fill(false);
//     let queue = [[S, 0]];
//     while (queue.length) {
//         let [current, count] = queue.shift();
//         if (visited[current]) continue;
//         if (current === G) {
//             return count;
//         }
//         if (visited[current] === false) {
//             let next = []
//             if ((current + U <= F )&& (current + U >= 1) && (U !== 0)) {
//                 next.push([current + U, count + 1])

//             }
//             if ((current - D >= 1) && (current - D <= F) &&( D !== 0)) {
//                 next.push([current - D, count + 1])

//             }

//             for (let i = 0; i < next.length; i++) {
//                 queue.push([next[i][0], next[i][1]]);

//             }
//         }
//         visited[current] = true;

//     }
//     return "use the stairs"
// }


//     console.log(solution(F, S, G, U, D))


// 첫 번째로 푼 답
const solution = (F, S, G, U, D) => {
    let visited = new Array(F + 1).fill(false);
    let queue = [[S, 0]];
    while (queue.length) {
           let [current, count] = queue.shift();
            if (current === G) {
                return count;
            }
            let next=[]
            if (current + U <= F && U !== 0) {
                next.push([current + U, count + 1])
            }
            if (current - D >= 1 && D !== 0) {
                next.push([current - D, count + 1])
            }
            for (let i=0;i<next.length;i++){
                if (visited[next[i][0]]) continue;
                visited[next[i][0]]=true;
                queue.push([next[i][0], next[i][1]]);
            }
    }
    return "use the stairs"
}

    console.log(solution(F, S, G, U, D))

🧵 참고한 정답지 🧵

https://mine-it-record.tistory.com/614

💡💡 기억해야 할 점 💡💡

  1. count++와 count+1은 다르다

VSCode는 맞다는데 백준은 틀리다 해서 힘들었다.

profile
이건 대체 어떻게 만든 거지?

0개의 댓글