Skip to content

Commit 420da51

Browse files
committed
공원 산책 / 중급
1 parent d0819a1 commit 420da51

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function solution(park, routes) {
2+
const directions = { N: [-1, 0], S: [1, 0], W: [0, -1], E: [0, 1] };
3+
let x, y;
4+
5+
for (let i = 0; i < park.length; i++) {
6+
const j = park[i].indexOf("S");
7+
if (j !== -1) {
8+
x = i;
9+
y = j;
10+
break;
11+
}
12+
}
13+
14+
for (const route of routes) {
15+
const [direction, distanceStr] = route.split(" ");
16+
const distance = parseInt(distanceStr);
17+
const [dx, dy] = directions[direction];
18+
19+
let canMove = true;
20+
21+
for (let step = 1; step <= distance; step++) {
22+
const newX = x + dx * step;
23+
const newY = y + dy * step;
24+
25+
if (
26+
newX < 0 ||
27+
newX >= park.length ||
28+
newY < 0 ||
29+
newY >= park[0].length ||
30+
park[newX][newY] === "X"
31+
) {
32+
canMove = false;
33+
break;
34+
}
35+
}
36+
37+
if (canMove) {
38+
x += dx * distance;
39+
y += dy * distance;
40+
}
41+
}
42+
43+
return [x, y];
44+
}

0 commit comments

Comments
 (0)