Skip to content

Commit 209106d

Browse files
committed
공원 산책 / 중급
1 parent f62f3f2 commit 209106d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

yulrang/03_Queue/06_Park_game.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function solution(park, routes) {
2+
let pathArr = [];
3+
const WIDTH = park[0].length;
4+
const HEIGHT = park.length;
5+
6+
for(let rowIdx = 0; rowIdx < park.length; rowIdx++){
7+
const row = park[rowIdx].split("");
8+
if(row.includes("S")){
9+
const columnIdx = park[rowIdx].indexOf("S");
10+
pathArr.push([columnIdx, rowIdx]);
11+
break;
12+
}
13+
}
14+
15+
16+
for(let i = 0; i<routes.length; i++) {
17+
const DIRECTION = routes[i].split(' ')[0];
18+
const AMOUNT = Number(routes[i].split(' ')[1]);
19+
let blockPos = [-1, -1];
20+
let x = pathArr[pathArr.length-1][0];
21+
let y = pathArr[pathArr.length-1][1];
22+
23+
if (DIRECTION === "E") {
24+
blockPos = [park[y].indexOf("X", x), y];
25+
x = pathArr[pathArr.length-1][0] + AMOUNT;
26+
y = pathArr[pathArr.length-1][1];
27+
} else if (DIRECTION === "W"){
28+
blockPos = [park[y].lastIndexOf("X", x), y];
29+
x = pathArr[pathArr.length-1][0] - AMOUNT;
30+
y = pathArr[pathArr.length-1][1];
31+
} else if (DIRECTION === "S"){
32+
blockPos = [x, park.findIndex((row, idx) => idx >= y && row[x] === "X")];
33+
x = pathArr[pathArr.length-1][0];
34+
y = pathArr[pathArr.length-1][1] + AMOUNT;
35+
} else if (DIRECTION === "N"){
36+
blockPos = [x, park.slice(0, y).reverse().findIndex((row, idx) => row[x] === "X")];
37+
if (blockPos[1] !== -1) blockPos[1] = y - blockPos[1] - 1;
38+
x = pathArr[pathArr.length-1][0];
39+
y = pathArr[pathArr.length-1][1] - AMOUNT;
40+
}
41+
42+
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT){
43+
continue;
44+
}
45+
if(!blockPos.includes(-1)){
46+
if((y === blockPos[1] && x >= blockPos[0] && pathArr[pathArr.length > 1 ? pathArr.length-2 : 0][0] < blockPos[0]) ||
47+
(y === blockPos[1] && x <= blockPos[0] && pathArr[pathArr.length > 1 ? pathArr.length-2 : 0][0] > blockPos[0]) ||
48+
(x === blockPos[0] && y >= blockPos[1] && pathArr[pathArr.length > 1 ? pathArr.length-2 : 0][1] < blockPos[1]) ||
49+
(x === blockPos[0] && y <= blockPos[1] && pathArr[pathArr.length > 1 ? pathArr.length-2 : 0][1] > blockPos[1]) ) {
50+
continue;
51+
}
52+
}
53+
54+
pathArr.push([x, y]);
55+
}
56+
57+
return pathArr[pathArr.length-1].reverse();
58+
}
59+

0 commit comments

Comments
 (0)