-
Notifications
You must be signed in to change notification settings - Fork 0
/
DP4.js
66 lines (54 loc) · 1.54 KB
/
DP4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const { off, mainModule } = require('process')
const readline = require('readline')
const { fileURLToPath } = require('url')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
const data = []
rl.on('line', (input) => {
data.push(input)
}).on('close', () => {
let T = data.shift()
for (let i = 0; i < T; i++) {
let [N, M] = data.shift().split(' ').map(Number)
let tmp = data.shift().split(' ').map(Number)
let gold = []
for (let i = 0; i < N; i++) {
gold.push([])
for (let j = 0; j < M; j++) {
gold[i].push(tmp.shift())
}
}//이렇게 해도 되는데 slice를 활용해보자
//gold의 각 열좌표가 한 층을 의미
let DP = new Array(N)
for (let i = 0; i < N; i++) {
DP[i] = new Array(M).fill(0)
}
for (let i = 0; i < N; i++) {
//초깃값
DP[i][0] = gold[i][0]
}
for(let i=1; i<M; i++){//열은 1부터. 열을 기준으로 각 행값들을 비교
for(let j=0; j<N; j++){//행은 0부터
//행의 범위만 찾아주면 됨
if(j===0){
DP[j][i]=Math.max(DP[j][i-1],DP[j+1][i-1])+gold[j][i]
}
else if(j===N-1){//3개를 다 묶어야 하므로 반드시 else if로 써야 한다
DP[j][i]=Math.max(DP[j][i-1],DP[j-1][i-1])+gold[j][i]
}
else{
DP[j][i]=Math.max(DP[j][i-1],DP[j-1][i-1],DP[j+1][i-1])+gold[j][i]
}
}
}
let result = []
for (let i = 0; i < N; i++) {
result.push(DP[i][M - 1])
}
console.log(Math.max(...result))
//console.log(DP)
}
process.exit()
})