Skip to content

Commit 86f343f

Browse files
committed
Feat: 배열, 연결 리스트, 스택, 큐 3문제
1 parent 4abbc90 commit 86f343f

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

Diff for: 1주차/박종운/골드4__뱀.js

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const local_input = `
2+
6
3+
3
4+
3 4
5+
2 5
6+
5 3
7+
3
8+
3 D
9+
15 L
10+
17 D
11+
`;
12+
13+
const input = process.execArgv.includes("--stack-size=65536")
14+
? require("fs").readFileSync("dev/stdin").toString()
15+
: local_input;
16+
17+
const inputs = input.trim().split("\n");
18+
const boardSize = Number(inputs[0]);
19+
const applesCount = Number(inputs[1]);
20+
const ordersCount = Number(inputs[1 + applesCount + 1]);
21+
const apples = inputs
22+
.slice(2, 2 + applesCount)
23+
.map((str) => str.split(" ").map((el) => Number(el)));
24+
const orders = inputs
25+
.slice(1 + applesCount + 1 + 1, 1 + applesCount + 1 + 1 + ordersCount)
26+
.map((str) => str.split(" ").map((el, i) => (i == 0 ? Number(el) : el)));
27+
28+
const orderArr = [];
29+
30+
orders.forEach((arr) => (orderArr[arr[0]] = arr[1]));
31+
32+
const board = Array.from({ length: boardSize }).map((_, i) =>
33+
Array.from({ length: boardSize })
34+
);
35+
36+
// 시작 지점 표시
37+
board[0][0] = "O";
38+
39+
// 사과 위치 표시
40+
apples.forEach((arr) => {
41+
const [y, x] = arr;
42+
board[y - 1][x - 1] = "A";
43+
});
44+
45+
let condi = true;
46+
let time = 0;
47+
let direction = "R";
48+
const queue = [[0, 0]];
49+
50+
while (condi) {
51+
time++;
52+
53+
let last = queue[queue.length - 1];
54+
55+
// 다음칸에 위치 시키기
56+
if (direction === "R") {
57+
queue.push([last[0], last[1] + 1]);
58+
} else if (direction === "L") {
59+
queue.push([last[0], last[1] - 1]);
60+
} else if (direction === "U") {
61+
queue.push([last[0] - 1, last[1]]);
62+
} else if (direction === "D") {
63+
queue.push([last[0] + 1, last[1]]);
64+
}
65+
66+
last = queue[queue.length - 1];
67+
68+
// 벽과 부딛히면 게임 종료
69+
if (
70+
last[0] < 0 ||
71+
last[0] >= boardSize ||
72+
last[1] < 0 ||
73+
last[1] >= boardSize
74+
) {
75+
console.log(time);
76+
return null;
77+
}
78+
79+
// 내 몸에 닫으면 종료
80+
if (
81+
queue.slice(0, -1).find((arr) => arr[0] === last[0] && arr[1] === last[1])
82+
) {
83+
console.log(time);
84+
return null;
85+
}
86+
87+
// 이동한 칸에 사과가 없다면 꼬리 지우기
88+
if (board[last[0]][last[1]] !== "A") {
89+
const current = queue.shift();
90+
board[current[0]][current[1]] = null;
91+
}
92+
93+
// X초가 끝난후 방향 전환
94+
if (typeof orderArr[time] === "string") {
95+
if (orderArr[time] === "L") {
96+
// 왼쪽
97+
if (direction === "R") direction = "U";
98+
else if (direction === "L") direction = "D";
99+
else if (direction === "U") direction = "L";
100+
else if (direction === "D") direction = "R";
101+
} else {
102+
// 오른쪽
103+
if (direction === "R") direction = "D";
104+
else if (direction === "L") direction = "U";
105+
else if (direction === "U") direction = "R";
106+
else if (direction === "D") direction = "L";
107+
}
108+
}
109+
}
110+
111+
console.log(time);

Diff for: 1주차/박종운/레벨1__올바른_괄호.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function solution(s) {
2+
const chars = s.split("");
3+
const stack = [chars.pop()];
4+
5+
while (chars.length > 0) {
6+
if (chars[chars.length - 1] === "(" && stack[stack.length - 1] === ")") {
7+
chars.pop();
8+
stack.pop();
9+
} else {
10+
stack.push(chars.pop());
11+
}
12+
}
13+
14+
return !stack.length;
15+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function solution(arr) {
2+
const result = [];
3+
4+
arr.forEach((num) => {
5+
if (result.length === 0 || result[result.length - 1] !== num) {
6+
result.push(num);
7+
}
8+
});
9+
10+
return result;
11+
}

0 commit comments

Comments
 (0)