✨ 정답 ✨
function solution(maps){
let count=0;
const dx=[-1, 0, 1, 0];
const dy=[0, -1, 0, 1];
let visited=Array.from({length:maps.length},()=>new Array(maps[0].length).fill(false))
visited[0][0]=1;
let queue=[[0,0]];
while(queue.length){
let [currentY, currentX]=queue.shift();
for (let i=0;i<4;i++){
let nextY=currentY+dy[i];
let nextX=currentX+dx[i];
if ( nextY>=maps.length || nextX>=maps[0].length || nextY<0 || nextX<0 ||maps[nextY][nextX]===0 ){
continue;
}
if (visited[nextY][nextX]){
continue;
}
queue.push([nextY, nextX]);
visited[nextY][nextX]=visited[currentY][currentX]+1;
}
}
count=visited[maps.length-1][maps[0].length-1];
if (count===false){
return -1;
}else{
return count;
}
}
🧵 참고한 정답지 🧵
💡💡 기억해야 할 점 💡💡