Skip to content

Commit 02846ee

Browse files
committed
Feat: 배열, 연결 리스트, 스택, 큐 뱀(3190번)
1 parent abbb28a commit 02846ee

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

1주차/채수혁/골드4__뱀.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
const local_input = `
2+
10
3+
5
4+
1 5
5+
1 3
6+
1 2
7+
1 6
8+
1 7
9+
4
10+
8 D
11+
10 D
12+
11 D
13+
13 L
14+
`;
15+
16+
const input = process.execArgv.includes("--stack-size=65536")
17+
? require("fs").readFileSync("dev/stdin").toString()
18+
: local_input;
19+
20+
const inputs = input.trim().trim().split("\n");
21+
const boardSize = Number(inputs[0]);
22+
const applesCount = Number(inputs[1]);
23+
const ordersCount = Number(inputs[1 + applesCount + 1]);
24+
25+
let apples = inputs
26+
.slice(2, 2 + applesCount)
27+
.map((str) => str.split(" ").map((v) => Number(v)));
28+
29+
const orders = inputs
30+
.slice(2 + applesCount + 1, 2 + applesCount + 1 + ordersCount)
31+
.map((str) => str.split(" ").map((v, i) => (i === 0 ? Number(v) : v)));
32+
33+
// 뱀 머리
34+
const que = [[1, -1]];
35+
let cnt = 0;
36+
37+
let currentDirection = [1, 0];
38+
39+
while (true) {
40+
cnt++;
41+
42+
const headIndex = que.length - 1;
43+
const headPosition = que[que.length - 1];
44+
45+
const nextPosition = [
46+
headPosition[0] + currentDirection[0],
47+
headPosition[1] + currentDirection[1],
48+
];
49+
50+
// 벽에 부딛힌경우
51+
if (
52+
nextPosition[0] < 1 ||
53+
nextPosition[0] > boardSize ||
54+
-nextPosition[1] < 1 ||
55+
-nextPosition[1] > boardSize
56+
) {
57+
break;
58+
}
59+
// 몸에 부딛힌 경우
60+
if (
61+
que.reduce(
62+
(a, v) => a || (v[0] === nextPosition[0] && v[1] === nextPosition[1]),
63+
false
64+
)
65+
) {
66+
break;
67+
}
68+
// 머리가 다음 위치로 움직임
69+
que.push(nextPosition);
70+
71+
// 사과를 먹지 않은 경우
72+
if (
73+
!apples.reduce(
74+
(a, v) => a || (v[1] === nextPosition[0] && -v[0] === nextPosition[1]),
75+
false
76+
)
77+
) {
78+
// 꼬리를 움직인 만큼 제거
79+
que.shift();
80+
} else {
81+
apples = apples.filter(
82+
(v) => !(v[1] === nextPosition[0] && -v[0] === nextPosition[1])
83+
);
84+
}
85+
86+
// 방향 회전 을 할 시간이라면
87+
if (orders.length > 0 && orders[0][0] === cnt) {
88+
const ordersDirection = orders[0][1];
89+
orders.shift();
90+
91+
// 오른쪽으로 돌기
92+
if (ordersDirection === "D") {
93+
if (currentDirection[0] === 1 && currentDirection[1] === 0) {
94+
currentDirection = [0, -1];
95+
} else if (currentDirection[0] === 0 && currentDirection[1] === -1) {
96+
currentDirection = [-1, 0];
97+
} else if (currentDirection[0] === -1 && currentDirection[1] === 0) {
98+
currentDirection = [0, 1];
99+
} else {
100+
currentDirection = [1, 0];
101+
}
102+
} else if (ordersDirection === "L") {
103+
// 왼쪽으로 돌기
104+
if (currentDirection[0] === 1 && currentDirection[1] === 0) {
105+
currentDirection = [0, 1];
106+
} else if (currentDirection[0] === 0 && currentDirection[1] === 1) {
107+
currentDirection = [-1, 0];
108+
} else if (currentDirection[0] === -1 && currentDirection[1] === 0) {
109+
currentDirection = [0, -1];
110+
} else {
111+
currentDirection = [1, 0];
112+
}
113+
}
114+
}
115+
}
116+
console.log(cnt);

0 commit comments

Comments
 (0)