개발자 톡
1번 테스트 케이스만 틀리는데 어떤 점이 잘못되었는지 모르겠습니다.
- 등록일
- 2024-11-04 18:55:09
- 조회수
- 48
- 작성자
- ga1754
```javascript
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const inputData = [];
rl.on('line', (line) => {
inputData.push(line.split(" ").map((e) => Number(e)));
}).on('close', () => {
const n = inputData[0][0];
const isValid = (nr, nc) => {
return 0 <= nr && nr < n && 0 <= nc && nc < n;
}
const dr = [1, 0];
const dc = [0, 1];
const field = inputData.slice(1);
const dp = Array.from({length: n}, () => Array.from({length: n}, () => [0, 0]));
dp[0][0][0] = field[0][0] * 2;
dp[0][0][1] = field[0][0];
const dq = [[0, 0]];
while (dq.length) {
const [r, c] = dq.shift();
for (let d = 0; d < 2; d++) {
const nr = r + dr[d];
const nc = c + dc[d];
if (isValid(nr, nc)) {
const maxV = Math.max(dp[r][c][1], field[nr][nc]);
const newScore = dp[r][c][0] - dp[r][c][1] + maxV + field[nr][nc];
if (dp[nr][nc][0] === 0 || dp[nr][nc][0] < newScore) {
dp[nr][nc][0] = newScore;
dp[nr][nc][1] = maxV;
dq.push([nr, nc]);
}
}
}
}
console.log(dp[n-1][n-1][0]);
process.exit(0);
});
```