Muscardinus
빛의 경로 사이클 본문
728x90
https://programmers.co.kr/learn/courses/30/lessons/86052
function solution(grid) {
const answer = [];
const R = grid.length;
const C = grid[0].length;
const visit = Array.from(new Array(R), () => Array.from(new Array(C), () => new Array(4).fill(0)));
const dirMap = {S: 0, R: 1, L: 2};
const map = grid.map((r) => Array.from(r).map((c) => dirMap[c]));
// 북 동 남 서
const dy = [-1, 0, 1, 0];
const dx = [0, 1, 0, -1];
// 방향 전환
const transDir = [[0, 1, 3], [1, 2, 0], [2, 3, 1], [3, 0, 2]];
for (let i = 0; i < R; i++) {
for (let j = 0; j < C; j++) {
for (let d = 0; d < 4; d++) {
if (visit[i][j][d]) continue;
let curDir = d;
let r = i; // 현재 r좌표
let c = j; // 현재 c좌표
let count = 0;
while(!visit[r][c][curDir]) {
count++;
visit[r][c][curDir] = 1;
curDir = transDir[curDir][map[r][c]];
r += dy[curDir];
c += dx[curDir];
r = r >= R ? 0 : r < 0 ? R - 1 : r;
c = c >= C ? 0 : c < 0 ? C - 1 : c;
}
answer.push(count);
}
}
}
return answer.sort((a, b) => a - b);
}
728x90
Comments