https://www.acmicpc.net/problem/14891
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
static int[][] wheal;
static int N;
static int K,D;
static int ans;
public static void main(String[] args) throws IOException {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
wheal = new int[4][8];
for(int i=0;i<4;i++){
String st = br.readLine();
String[] temp = st.split("");
for(int j=0;j<8;j++){
wheal[i][j] = Integer.parseInt(temp[j]);
}
}
StringTokenizer st= new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
for(int i=0;i<N;i++){
st =new StringTokenizer(br.readLine());
K=Integer.parseInt(st.nextToken()) -1 ;
D=Integer.parseInt(st.nextToken());
boolean[] rotate = new boolean[4];
int[] rotateDirection = new int[4];
rotate[K] = true;
rotateDirection[K] = D;
// 오른쪽 톱니바퀴 체크
for(int s=K;s<3;s++){
// s는 Wheal 번호
if(wheal[s][2]== wheal[s+1][6]) break;
else if( wheal[s][2] != wheal[s+1][6]){
rotate[s+1]=true;
rotateDirection[s+1] = -rotateDirection[s];
}
}
// 왼쪽 톱니바퀴 체크
for(int s=K;s>0;s--) {
// s는 Wheal 번호
if(wheal[s][6] == wheal[s-1][2]) break;
else if (wheal[s][6] != wheal[s-1][2]) {
rotate[s-1]=true;
rotateDirection[s-1] = -rotateDirection[s];
}
}
for(int j=0;j<4;j++){
if(rotate[j]){
if(rotateDirection[j]==1) clockwise(j);
else AntiClockwise(j);
}
}
}
ans=0;
if( wheal[0][0] == 1) ans+=1;
if( wheal[1][0] == 1) ans+=2;
if( wheal[2][0] == 1) ans+=4;
if( wheal[3][0] == 1) ans+=8;
System.out.println(ans);
}
// 시계 방향 회전 => 모든 배열을 오른쪽으로 1칸씩 이동
static void clockwise(int whealNum){
int last = wheal[whealNum][7];
for(int i=7;i>=1;i--){
wheal[whealNum][i]=wheal[whealNum][i-1];
}
wheal[whealNum][0]=last;
}
// 반시계 방향 => 모든 배열을 왼쪽으로 1칸씩 이동
static void AntiClockwise(int whealNum){
int first = wheal[whealNum][0];
for(int i=1;i<=7;i++){
wheal[whealNum][i-1] = wheal[whealNum][i];
}
wheal[whealNum][7]=first;
}
static int execute(int nextD,int s){
if (nextD == 1) {
clockwise(s); // 시계방향 회전
return -1;
} else {
AntiClockwise(s); // 반시계방향 회전
return 1;
}
}
static int setNextD(int D){
if(D==1) return -1;
else return 1;
}
}
'코딩테스트' 카테고리의 다른 글
[알고리즘 문제풀이] 백준 11479 서로 다른 부분 문자열의 개수 (문자열 응용) (1) | 2024.11.15 |
---|---|
[알고리즘 문제 풀이] 백준 1920 수 찾기 ( 해시 응용 ) (0) | 2024.11.14 |
[알고리즘 문제 풀이] 백준 117244 연결 요소의 개수 (BFS/DFS) (0) | 2024.07.14 |
[알고리즘 문제 풀이] 백준 2667 단지번호붙이기 (BFS/DFS) (0) | 2024.07.10 |
[알고리즘 문제 풀이] 백준 13549 숨바꼭질3 (BFS/DFS) (학습용) (0) | 2024.07.06 |