개발자 톡

연습문제 톡 나무 조경

[스포] JS 풀이 (안풀릴때 보세요)

등록일
2025-02-06 17:18:47
조회수
74
작성자
saungin1228
const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const input = []

rl.on("line", (line) => {
    input.push(line.trim())
}).on("close" , () => {
    input.shift()

    const numbers = input.map(v => v.split(" ").map(Number))



    const NUMBER_map = []

    for (let i=0; i< numbers.length -1; i++){
        for(let j=0; j< numbers[i].length; j++){
            const up = numbers[i][j]
            const down = numbers[i+1][j]
            NUMBER_map.push({
                value : up + down,
                location : [`${i},${j}`, `${i+1},${j}`]
            })
        }
    }

    for (let i=0; i< numbers.length; i++){
        for(let j=0; j< numbers[i].length -1; j++){
            const left = numbers[i][j]
            const right = numbers[i][j+1]
            NUMBER_map.push({
                value : left + right,
                location : [`${i},${j}`, `${i},${j+1}`]
            })
        }
    }
    
    NUMBER_map.sort((a,b) => b.value - a.value)
    
    let answer = -1;
    const dfs = (cur_value , size, history ) => {
        if (size >8)
            return;

        answer = Math.max(cur_value , answer)
        

        for ( const { value , location } of NUMBER_map){
            const [ front , end ] = location
            
            if (history.includes(front) || history.includes(end)){
                continue;
            }
            
            dfs(cur_value + value , size + 2 , [...history , front, end])
        }
    }

    
    dfs(0 , 0 , [])

    console.log(answer)
    
})


#나무_조경

이 카테고리의 톡 더보기