[자바스크립트, 백준] 2178 미로 탐색
알고리즘/백준2023. 3. 17. 12:03
난이도 : 실버 1
링크 : https://www.acmicpc.net/problem/2178
문제리뷰
bfs
소스코드
const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');
const [N, M] = input.shift().split(' ').map(Number);
const map = input.map((row) => row.split('').map(Number));
function bfs(x,y) {
const queue = [[x,y]];
const result = [];
const visisted = { };
visisted[[x,y]] = 1;
let dx = [0, 0, -1, 1];
let dy = [-1, 1, 0, 0];
while(queue.length) {
for(let i=0; i<queue.length; i++) {
let now = queue.shift();
result.push(now);
for(let j=0; j<4; j++) {
let nx = now[0] + dx[j];
let ny = now[1] + dy[j];
if( nx >= 0 && ny >= 0 && nx < N && ny < M && !visisted[[nx,ny]] && map[nx][ny] === 1) {
visisted[[nx,ny]] = visisted[now]+1;
queue.push([nx,ny]);
}
}
}
}
return visisted[[N-1,M-1]];
}
console.log(bfs(0,0));
'알고리즘 > 백준' 카테고리의 다른 글
[자바스크립트, 백준] 10798 세로읽기 (0) | 2023.03.15 |
---|---|
[자바스크립트, 백준] 2567 색종이 - 2 (1) | 2023.03.14 |
[자바스크립트, 백준] 9498 시험성적 (0) | 2023.03.13 |
[자바스크립트, 백준] 백준에서 자바스크립트로 알고리즘 푸는 방법 (0) | 2023.03.13 |
파이썬, 백준 18352, 특정 거리의 도시 찾기 (0) | 2022.11.11 |
댓글()