[프로그래머스] 게임 맵 최단거리 (bfs, javascript)
Posted by 해랑쓰 블로그 (Haerang's blog)
dfs로 풀면 효율성 시간 초과가 난다. 최단 거리인 경우 bfs로 모든 경우의 수를 구할 경우에는 dfs 문제로 많이 갈리는 것 같다. function solution(maps) { let xpos=[0,0,1,-1]; let ypos=[1,-1,0,0]; var answer = -1; const q=[[0,0,1]]; while(q.length!=0){ let y=q[0][0]; let x=q[0][1]; let cnt=q[0][2]; q.shift(); if(y===maps.length-1 && x===maps[0].length-1){ answer=cnt; break; } for(let i=0;i