https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWIeV9sKkcoDFAVH
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Solution {
public static int T, K, ans = 0;
public static int[][] wheel = new int[4][8];
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
T = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= T; tc++) {
// 1. 입력받기
K = Integer.parseInt(br.readLine());
// 1-1. 톱니바퀴 입력받기
for (int i = 0; i < 4; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < 8; j++) {
wheel[i][j] = Integer.parseInt(st.nextToken());
}
}
// 2. 톱니바퀴 회전
for (int i = 0; i < K; i++) {
st = new StringTokenizer(br.readLine());
int num = Integer.parseInt(st.nextToken()) - 1;
int dir = Integer.parseInt(st.nextToken());
if(dir == -1) dir = 0;
Queue<int[]> q = new LinkedList<>();
q.offer(new int[] { num, dir });
int[] visited = new int[4];
visited[num] = 1;
while (!q.isEmpty()) {
int cur[] = q.poll();
int curN = cur[0];
int curD = cur[1];
// 2-1. 다음 돌릴 톱니바퀴 선택
int left = curN - 1; // 왼쪽
if (left >= 0 && visited[left] == 0 && wheel[curN][6] + wheel[left][2] == 1) {
q.offer(new int[] { left, (curD + 1) % 2 });
visited[left] = 1;
}
int right = curN + 1; // 오른쪽
if (right < 4 && visited[right] == 0 && wheel[curN][2] + wheel[right][6] == 1) {
q.offer(new int[] { right, (curD + 1) % 2 });
visited[right] = 1;
}
// 2-2. 돌리기
if (curD == 1) { // 시계방향 회전
int tmp = wheel[curN][7];
for (int j = 7; j >= 1; j--) {
wheel[curN][j] = wheel[curN][j - 1];
}
wheel[curN][0] = tmp;
} else { // 반시계방향 회전
int tmp = wheel[curN][0];
for (int j = 1; j < 8; j++) {
wheel[curN][j - 1] = wheel[curN][j];
}
wheel[curN][7] = tmp;
}
}
}
// 3. 결과 출력
ans = 0;
for (int i = 0; i <= 3; i++) {
if (wheel[i][0] == 1) {
ans += Math.pow(2, i);
}
}
System.out.println("#" + tc + " " + ans);
}
}
}
단순 구현문제였지만 돌리는 부분에서 시간을 조금 많이 할애하였다.
'코딩테스트 > JAVA' 카테고리의 다른 글
[백준 JAVA] 14503번: 로봇 청소기 (1) | 2024.10.10 |
---|---|
[백준 JAVA] 22866번: 탑 보기 (2) | 2024.10.02 |
[백준 JAVA] 10942번: 팰린드롬? (0) | 2024.10.02 |
[백준 JAVA] 13913번: 숨바꼭질 4 (0) | 2024.09.30 |
[SWEA JAVA] 3282. 0/1 Knapsack (0) | 2024.09.30 |