From bebf5a9cae5b62782e7e658c24214683845601e1 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Wed, 3 Dec 2025 21:54:26 +0900 Subject: [PATCH] baekjoon 20251203 --- code_study/Baekjoon/ts/2342.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 code_study/Baekjoon/ts/2342.ts diff --git a/code_study/Baekjoon/ts/2342.ts b/code_study/Baekjoon/ts/2342.ts new file mode 100644 index 0000000..0b5159b --- /dev/null +++ b/code_study/Baekjoon/ts/2342.ts @@ -0,0 +1,29 @@ +export {}; +const inst: number[] = require("fs").readFileSync(0).toString().trim().split(" ").map(Number); +const steps: number = inst.length - 1; + +const force: number[][] = [ + [1,2,2,2,2], + [0,1,3,4,3], + [0,3,1,3,4], + [0,4,3,1,3], + [0,3,4,3,1] +]; + +let dp: number[][][] = Array.from({length: inst.length-1}, () => Array.from({length: 5}, () => new Array(5).fill(0))); + +const solve = (step: number, left: number, right: number): number => { + if (step === steps) return 0; + + if (dp[step][left][right] === 0) { + const next: number = inst[step]; + dp[step][left][right] = Math.min( + solve(step+1, next, right) + force[left][next], + solve(step+1, left, next) + force[right][next] + ); + }; + + return dp[step][left][right]; +}; + +console.log(solve(0,0,0)); \ No newline at end of file