Skip to content

Commit 421be20

Browse files
committed
[Simulation] baekjoon-3954
1 parent 3a1ccd1 commit 421be20

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
| 11 | | [Baekjoon-14503 로봇 청소기](./src/Simulation/P14503) | 삼성 SW 역량 테스트 기출 |
4141
| 12 | | [Baekjoon-17135 캐슬 디펜스](./src/Simulation/P17135) | 삼성 A형 기출 |
4242
| 13 | | [Baekjoon-17281 ⚾](./src/Simulation/P17281) | 삼성 A형 기출 |
43+
| 14 | | [Baekjoon-3954 Brainf**k 인터프리터](./src/Simulation/P3954) | 삼성 A형 기출 |
4344

4445
## String
4546

src/Simulation/P3954/Main.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package Simulation.P3954;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.Stack;
7+
import java.util.StringTokenizer;
8+
9+
public class Main {
10+
11+
final static int MOD = 256;
12+
final static int END = 50000000;
13+
14+
static int T, M, C, I;
15+
static String program, input;
16+
static int[] brackets;
17+
18+
public static void main(String[] args) throws Exception {
19+
System.setIn(new FileInputStream("src/Simulation/P3954/input.txt"));
20+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21+
22+
T = Integer.parseInt(br.readLine());
23+
while (T-- > 0) {
24+
StringTokenizer st = new StringTokenizer(br.readLine());
25+
M = Integer.parseInt(st.nextToken());
26+
C = Integer.parseInt(st.nextToken());
27+
I = Integer.parseInt(st.nextToken());
28+
program = br.readLine();
29+
input = br.readLine();
30+
play();
31+
}
32+
}
33+
34+
static void makeBrackets() {
35+
brackets = new int[C];
36+
Stack<Integer> s = new Stack<>();
37+
for (int i = 0; i < C; i++) {
38+
char c = program.charAt(i);
39+
if (c == '[') s.push(i);
40+
else if (c == ']') {
41+
int prev = s.pop();
42+
brackets[i] = prev;
43+
brackets[prev] = i;
44+
}
45+
}
46+
}
47+
48+
static void play() {
49+
makeBrackets();
50+
int m = 0, i = 0, l = 0, c = 0, cnt = 0;
51+
int[] arr = new int[M];
52+
53+
while (cnt++ <= END*2) {
54+
if (c == C) { System.out.println("Terminates"); return; }
55+
char p = program.charAt(c);
56+
switch (p) {
57+
case '-': arr[m] = (arr[m]-1) % MOD; break;
58+
case '+': arr[m] = (arr[m]+1) % MOD; break;
59+
case '<': if (--m < 0) m += M; break;
60+
case '>': if (++m == M) m = 0; break;
61+
case '[':
62+
if (arr[m] == 0) c = brackets[c];
63+
break;
64+
case ']':
65+
if (arr[m] != 0) {
66+
if (cnt > END) l = Math.max(l, c);
67+
c = brackets[c];
68+
}
69+
break;
70+
case '.': break;
71+
case ',':
72+
if (i == I) arr[m] = 255;
73+
else arr[m] = input.charAt(i++);
74+
break;
75+
}
76+
c++;
77+
}
78+
79+
System.out.println("Loops " + brackets[l] + " " + l);
80+
}
81+
}

src/Simulation/P3954/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## [baekjoon-3954] Brainf**k 인터프리터
2+
3+
![image](https://user-images.githubusercontent.com/22045163/108668812-35ae8d80-751f-11eb-92cd-622d6e632f92.png)
4+
![image](https://user-images.githubusercontent.com/22045163/108668853-4828c700-751f-11eb-8e83-c628ac6e5fd6.png)
5+
6+
### 문제 풀이 Point
7+
8+
풀면서 좀 너무한 것 같다 !! 라는 생각이 드는 문제였다. 휴... 😭😭😭
9+
10+
- 문제의 핵심 : Loop에서 괄호 짝을 구하는 것, 이 과정에서 발생할 수 있는 다양한 테스트케이스들을 생각해봐야 한다.
11+
- 문제 속에 알고리즘에 대한 모든 힌트를 담고 있다.
12+
- 해당 프로그램이 무한 루프라고 판단되었을 때, 정확한 괄호의 짝을 찾아내는 과정에 대한 고찰이 필요하다.
13+
14+
![image](https://user-images.githubusercontent.com/22045163/108668881-5545b600-751f-11eb-941a-7a00b5101544.png)

src/Simulation/P3954/input.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
11
2+
10 4 3
3+
+-.,
4+
qwe
5+
1000 5 1
6+
+[+-]
7+
a
8+
100 74 4
9+
+++++[->++<]>[-<+++++++>]<[->+>+>+>+<<<<]>+++>--->++++++++++>---<<<.>.>.>.
10+
xxyz
11+
9999 52 14
12+
+++++[>+++++++++<-],+[-[>--.++>+<<-]>+.->[<.>-]<<,+]
13+
this_is_a_test
14+
10 5 1
15+
+[[]]
16+
a
17+
1 9 1
18+
++[[++]+]
19+
a
20+
65359 16 3
21+
+[>+],...[[-],+]
22+
999
23+
65359 16 3
24+
+[>+],[[-],+]+[]
25+
999
26+
3 209 1
27+
+[>+[>----------------------------------[+++]<+]++++++++++++++++++++++++++[+]<+],[-].+[-[[>+[>----------------------------------[+++]<+]++++++++++++++++++++++++++[+]<+]----------------------------------[+]]++]
28+
1
29+
3 124 1
30+
+[-[[>+[>----------------------------------[+++]<+]++++++++++++++++++++++++++[+]<+]----------------------------------[+]]++]
31+
.
32+
1 8 1
33+
+[-[]++]
34+
.
35+
// output
36+
Terminates
37+
Loops 1 4
38+
Terminates
39+
Terminates
40+
Loops 2 3
41+
Loops 3 6
42+
Terminates
43+
Loops 14 15
44+
Loops 86 208
45+
Loops 1 123
46+
Loops 3 4

0 commit comments

Comments
 (0)