문제 링크
분류 / 레벨 / 언어
DFS,BFS / LV.3 / Javscript
설명
DFS 문제를 재귀로 풀 때는 보통 이런 틀을 갖추고 푼다.
1 2 3 4 5 6
| let count = 0; const dfs = (상태, 누적 정보)=>{ if() dfs(다음 상태, 누적 정보) if() count++ } dfs(초기 상태, 초기 정보)
|
전체 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| function solution(tickets) { const routes = []; const next = (now, remains, route) => { if (remains.length === 0) { routes.push(route.concat(now)); return; } remains.forEach((remain, i) => { if (remain[0] === now) { let remainsCopy = remains.slice(); remainsCopy.splice(i, 1); next(remain[1], remainsCopy, route.concat(now)); } }); };
tickets.forEach((ticket, i) => { if (ticket[0] === "ICN") { let ticketsCopy = tickets.slice(); next(ticketsCopy.splice(i, 1)[0][1], ticketsCopy, ["ICN"]); } });
return routes .map(route => route.join(",")) .sort()[0] .split(","); }
|