📄문제



✏️풀이
은찬
function solution(maps) { let answer = 1; const visited = maps; const queue = []; const directX = [-1, 1, 0, 0]; const directY = [0, 0, -1, 1]; const n = maps.length; const m = maps[0].length; queue.push([0, 0]); visited[0][0] = 0; while(queue.length > 0){ const size = queue.length; for(let i = 0; i < size; i++){ const [x, y] = queue.shift(); for(let j = 0; j < 4; j++){ const nx = x + directX[j]; const ny = y + directY[j]; if(nx >= 0 && nx < n && ny >= 0 && ny < m && visited[nx][ny] === 1){ if(nx == n - 1 && ny == m - 1){ return ++answer; } queue.push([nx, ny]); visited[nx][ny] = 0; } } } answer++; } return -1; }
재영
const solution = (maps) => { const check = () => { return ( nextX >= 0 && nextY >= 0 && nextX < mapXLength && nextY < mapYLength && maps[nextX][nextY] ); }; let answer = 0; // 답 const mapXLength = maps.length; const mapYLength = maps[0].length; const directions = [ [1, 0], [-1, 0], [0, 1], [0, -1], ]; const queue = []; queue.push([0, 0]); // 큐에 넣어진 것들 각각마다 visited 체크 여부가 달라지지 않을까...? while (queue.length) { answer += 1; const queueSize = queue.length; for (let i = 0; i < queueSize; i += 1) { const [nowX, nowY] = queue.shift(); if(!maps[nowX][nowY]){ continue; } maps[nowX][nowY] = false; if (nowX === mapXLength - 1 && nowY === mapYLength - 1) { return answer; } directions.forEach(([dx, dy]) => { const nextX = nowX + dx; const nextY = nowY + dy; if (check(nextX, nextY, mapXLength, mapYLength, maps)) { queue.push([nextX, nextY]); } }); } } return -1; };
❤🧡💛💚💙💜🤎🖤🤍❣💓💗💖💕💞💘💝💟💌
효성
class MyQueue { constructor() { this.input = []; this.output = []; this.size = 0; } enque(val) { this.input.push(val); this.size++; } deque() { if(this.output.length === 0) { this.output = this.input.reverse(); this.input = []; } this.size--; return this.output.pop(); } } function solution(maps) { const dr = [-1, 1, 0, 0]; const dc = [0, 0, -1, 1]; const R = maps.length; const C = maps[0].length; const visit = Array(R).fill(null).map(() => Array(C).fill(false)); const que = new MyQueue(); que.enque([0, 0, 1]); visit[0][0] = true; while(que.size > 0) { const [r, c, cnt] = que.deque(); if(r === R-1 && c === C-1) { return cnt; } const nextCnt = cnt + 1; for(let i = 0; i < 4; i++) { const nr = r + dr[i]; const nc = c + dc[i]; if(nr < 0 || nr >= R || nc < 0 || nc >= C) continue; if(visit[nr][nc] || maps[nr][nc] === 0) continue; visit[nr][nc] = true; que.enque([nr, nc, nextCnt]); } } return -1; }