요약
풀이 시간 : 49분 32초
풀이 정리
1. 좌표 방향
2. 이동 가능 여부 확인
다른 사람 풀이
1. 2차원 배열은 [][]로 표현가능
문제 링크
[알고리즘]-공원산책
풀이
function solution(park, routes) {
const parkHeight = park.length, parkWidth = park[0].length
let [currentHeight, currentWidth] = park.flatMap((park, hei) =>
[...park].map((state, wid) => state === "S" ? [hei, wid] : null)).find(Boolean);
const directions = {
"N" : [-1, 0],
"S" : [1, 0],
"W" : [0, -1],
"E" : [0, 1],
};
for(const route of routes){
const [direction, moveStr] = route.split(" ");
const [dy, dx] = directions[direction];
const move = Number(moveStr);
let isUpdate = true;
for(let step = 1; step <= move; step++){
const nextHeight = step * dy + currentHeight;
const nextWidth = step * dx + currentWidth;
const isBlocked = park[nextHeight][nextWidth] === "X";
const outOfPark = nextHeight < 0 || nextHeight >= parkHeight ||
nextWidth < 0 || nextWidth >= parkWidth;
if(outOfPark || isBlocked){
isUpdate = false;
break;
}
}
if(isUpdate){
[currentHeight, currentWidth] = [currentHeight + move * dy, currentWidth + move * dx];
}
}
return [currentHeight, currentWidth];
}