[백준 11048번] DP(다이나믹 프로그래밍) - 이동하기

김민지·2023년 7월 19일
0

냅다 시작 백준

목록 보기
59/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();


// 두 번째 풀이
input=input.split('\n')
const [N, M]=input.shift().split(' ').map((el)=>+el)

let array=input.map((el)=>el.split(' ').map((el2)=>+el2))
let dp=Array.from({length:N+1},()=> new Array(M+1).fill(0))
let max=0;
for (let i=1;i<=N;i++){
    for (let j=1;j<=M;j++){
        dp[i][j]=array[i-1][j-1]+Math.max(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])
        if (max<dp[i][j]){
            max=dp[i][j]
        }
    }
}
console.log(max)



// 첫 번째 풀이
// input=input.split('\n')
// let [N, M]=input.shift().split(' ').map((el)=>+el)
// input=input.map((el)=>el.split(' ').map((el)=>+el))
// let dp=Array.from({length:N}, ()=>new Array(M).fill(0));
// for (let i=0;i<N;i++){
//     for (let j=0;j<M;j++){
//         let [next1,next2,next3]=[0,0,0]
//         if (i>0){
//             next1=dp[i-1][j]
//         }
//         if (j>0){
//             next2=dp[i][j-1]
//         }
//         if (i>0&&j>0){
//             next3=dp[i-1][j-1]
//         }
//         dp[i][j]=input[i][j]+Math.max(next1, next2, next3)
//     }
// }

// // dp[n][n]=Math.max(dp[n-1][n], dp[n-1][n-1], dp[n][n-1])
// console.log(dp[N-1][M-1]);

🧵 참고한 정답지 🧵

💡💡 기억해야 할 점 💡💡

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

1개의 댓글

comment-user-thumbnail
2023년 7월 19일

이 글은 정말 인상적이었습니다.

답글 달기