728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.ArrayDeque;
import java.util.Scanner;
class Solution {
static class Button { // 버튼 클래스 생
char color; //색 저
int idx; // 인덱스 저
public Button(char color,int idx) {
this.color=color;
this.idx=idx;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int tc=1;tc<=t;tc++) {
int n = sc.nextInt();
ArrayDeque<Button> entire = new ArrayDeque<>(); //전체
ArrayDeque<Button> blue = new ArrayDeque<>(); //블루 큐
ArrayDeque<Button> orange = new ArrayDeque<>(); //오렌지 큐
for(int i=0;i<n;i++) {
char color = sc.next().charAt(0);
int idx = sc.nextInt();
Button btn = new Button(color,idx);
if(color=='B') {
blue.offer(btn);
}
else if(color=='O') {
orange.offer(btn);
}
entire.offer(btn);
}
int time=0;
int bluePosition=1;
int orangePosition=1;
boolean click=false;
while(!entire.isEmpty()) {
time++;
click=false;
if(!blue.isEmpty()) {
if(bluePosition<blue.peekFirst().idx) {
bluePosition++;
}
else if(bluePosition>blue.peekFirst().idx) {
bluePosition--;
}
else {
if(entire.peekFirst().color=='B') { //현재 전체 버튼 큐 맨 앞의 버튼과 색이 동일한지
click=true;
blue.poll();
}
}
}
if(!orange.isEmpty()) {
if(orangePosition<orange.peekFirst().idx) {
orangePosition++;
}
else if(orangePosition>orange.peekFirst().idx) {
orangePosition--;
}
else {
if(entire.peekFirst().color=='O') {
click=true;
orange.poll();
}
}
}
if(click==true) {
entire.poll();
}
}
System.out.println("#"+tc+" "+time);
}
}
}728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] 주혁이의 복권 당첨 (D3) (0) | 2024.09.28 |
|---|---|
| [SW Expert Academy] 구구단 걷기 (D3) (0) | 2024.09.28 |
| [SW Expert Academy] 세상의 모든 팰린드롬 2 (D3) (0) | 2024.09.27 |
| [SW Expert Academy] 유효기간 (D3) (0) | 2024.09.27 |
| [SW Expert Academy] 세가지 합 구하기 (D3) (0) | 2024.09.27 |