baekjoon 20251203
This commit is contained in:
parent
947cd4a91c
commit
bebf5a9cae
29
code_study/Baekjoon/ts/2342.ts
Normal file
29
code_study/Baekjoon/ts/2342.ts
Normal file
@ -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));
|
||||
Loading…
x
Reference in New Issue
Block a user