Skip to content

Commit 681262f

Browse files
authored
Merge pull request #680 from AlgorithmWithGod/lkhyun
[20250817] BOJ / G5 / 중력 큐 / 이강현
2 parents 65f18f6 + 2238315 commit 681262f

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static ArrayDeque<Integer> deq = new ArrayDeque<>();
10+
static int state = 0; //0: 뒤 앞, 1: 앞이 하늘, 2: 앞 뒤, 3: 앞이 아래
11+
static int ballCnt = 0;
12+
static int wallCnt = 0;
13+
14+
public static void main(String[] args) throws IOException {
15+
int Q = Integer.parseInt(br.readLine());
16+
17+
18+
for (int i = 0; i < Q; i++) {
19+
st = new StringTokenizer(br.readLine());
20+
String a = st.nextToken();
21+
22+
if(a.equals("push")){
23+
String b = st.nextToken();
24+
if(b.equals("b")){
25+
deq.offer(1);
26+
ballCnt++;
27+
ballFlush();
28+
}else if(b.equals("w")){
29+
deq.offer(-1);
30+
wallCnt++;
31+
}
32+
33+
}else if(a.equals("pop")){
34+
if(!deq.isEmpty()){
35+
if(deq.poll() == 1){
36+
ballCnt--;
37+
}else{
38+
wallCnt--;
39+
}
40+
}
41+
ballFlush();
42+
}else if(a.equals("rotate")){
43+
String b = st.nextToken();
44+
if(b.equals("l")){
45+
state = (state+1) % 4;
46+
}else if(b.equals("r")){
47+
state = (state+3) % 4;
48+
}
49+
ballFlush();
50+
}else if(a.equals("count")){
51+
String b = st.nextToken();
52+
if(b.equals("b")){
53+
bw.write(ballCnt+"\n");
54+
}else if(b.equals("w")){
55+
bw.write(wallCnt+"\n");
56+
}
57+
}
58+
}
59+
bw.close();
60+
}
61+
static void ballFlush(){
62+
if(state == 1){
63+
while(!deq.isEmpty() && deq.peekLast() == 1){
64+
deq.pollLast();
65+
ballCnt--;
66+
}
67+
}else if(state == 3){
68+
while(!deq.isEmpty() && deq.peek() == 1){
69+
deq.poll();
70+
ballCnt--;
71+
}
72+
}
73+
}
74+
}
75+
```

0 commit comments

Comments
 (0)