// 蜜蜂采蜜 ABCDE五点 从原点来回走完的最短距离
// 暴力:全排列 + 模拟距离
function distance (a, b) {
const [a1, a2] = a.split(',').map(Number), [b1, b2] = b.split(',').map(Number)
return Math.sqrt((a1-b1)**2 + (a2-b2)**2)
}
function getTotalDistance (list, res=0) {
const todoList = ['0,0'].concat(list, '0,0')
for(let i=1; i<7; i++) {
res += distance(todoList[i-1], todoList[i])
}
return res
}
// 全排列模板
function getList (list, res=Infinity) {
const dfs = (stack) => {
if(stack.length === 5) {
res = Math.min(res, getTotalDistance(stack)) // 这里修改
return
}
for(let i=0; i<5; i++) {
const cur = list[i].join(',')
if(stack.includes(cur)) continue
stack.push(cur)
dfs(stack)
stack.pop()
}
}
dfs([])
return res
}
console.log(getList([[200,0], [200,10], [200,50], [200,30], [200,25]]).toFixed(0)) // [2,3,4,1]