function solution(maps) {
const xLen = maps.length
const yLen = maps(0).length
const vistedMap = Array.from(Array(xLen),()=>Array(yLen).fill(false))
const offset = (
(-1,0), //좌
(1,0), //우
(0,-1), //상
(0,1), //하
)
const answer = ()
function sumCountArea(x,y){
const stack = ((x,y))
let count = maps(x)(y)*1
while(stack.length > 0){
const (xP,yP) = stack.pop()
offset.forEach(((moveX,moveY))=>{
const nX = xP + moveX
const nY = yP + moveY
if(nX >= 0 && nX < xLen && nY >= 0 && nY < yLen && !vistedMap(nX)(nY) && maps(nX)(nY) != "X"){
vistedMap(nX)(nY) = true
count += maps(nX)(nY)*1
stack.push((nX,nY))
}
})
}
answer.push(count)
}
for(let i = 0; i < xLen; i++){
for(let j = 0; j < yLen; j++){
if(vistedMap(i)(j) || maps(i)(j) == "X"){
continue
}
vistedMap(i)(j) = true
sumCountArea(i,j)
}
}
console.log(answer)
answer.sort((a,b)=>a-b)
return answer.length > 0 ? answer: (-1)
}
총, 최단 거리
대부분 비슷합니다.
2차 배열이 주어질 때
보조 어레이 생성 및 방문 여부 확인
stack이나 que로 합계를 계산하는 것과 비슷합니다.
중요한 것은 각 루프 문을 만드는 것입니다.