• 문제 링크
3048번: 개미
T초가 지난 후에 개미의 순서를 출력한다. 첫 번째 개미 그룹은 왼쪽에서 오른쪽으로 움직이고, 두 번째 그룹은 반대 방향으로 움직인다.
www.acmicpc.net
• 풀이 과정
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
int n1 = Integer.parseInt(st.nextToken());
int n2 = Integer.parseInt(st.nextToken());
Ant[] grp1 = new Ant[n1 + 1];
Ant[] grp2 = new Ant[n2 + 1];
String input = br.readLine();
for (int i = n1 - 1; i >= 0; i--) {
grp1[i] = new Ant(input.charAt(n1 - i - 1), 1);
}
input = br.readLine();
for (int i = 0; i < n2; i++) {
grp2[i] = new Ant(input.charAt(i), -1);
}
Ant[] start = new Ant[n1 + n2];
for (int i = 0; i < n1; i++)
start[i] = grp1[i];
for (int i = n1; i < n1 + n2; i++)
start[i] = grp2[i - n1];
int t = Integer.parseInt(br.readLine());
Ant[] end = start;
for (int i = 0; i < t; i++) {
Ant[] cur = end.clone();
for (int j = 0; j < n1 + n2 - 1; j++) {
if (cur[j].dir == 1 && cur[j + 1].dir == -1) {
Ant tmp = cur[j];
end[j] = cur[j + 1];
end[j + 1] = tmp;
} else
continue;
}
}
for (int i = 0; i < n1 + n2; i++)
sb.append(end[i].val);
bw.write(sb.toString());
bw.close();
}
private static class Ant {
char val;
int dir;
public Ant(char val, int dir) {
this.val = val;
this.dir = dir;
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 8979 올림픽 - Implementation / Java (0) | 2022.06.05 |
---|---|
[백준] 2563 색종이 - Implementation / Java (0) | 2022.06.03 |
[백준] 10798 세로읽기 - Implementation / Java (0) | 2022.06.01 |
[백준] 8911 거북이 - Implementation / Java (0) | 2022.05.31 |
[백준] 2290 LCD Test - Implementation / Java (0) | 2022.05.30 |
댓글