반응형
소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
class Solution
{
private static BufferedReader br;
private static StringTokenizer st;
private static List<Integer> []magnets;
private static int T, K;
private static int [][]rotates;
public static void main(String[] args) throws NumberFormatException, IOException {
br=new BufferedReader(new InputStreamReader(System.in));
T=Integer.parseInt(br.readLine());
for(int t=1;t<=T;t++) {
magnets=new ArrayList[5];
for(int i=1;i<=4;i++) magnets[i]=new ArrayList<>();
K=Integer.parseInt(br.readLine());
rotates=new int[K][2];
for(int i=1;i<=4;i++) {
st=new StringTokenizer(br.readLine());
for(int j=0;j<8;j++) magnets[i].add(Integer.parseInt(st.nextToken()));
}
for(int i=0;i<K;i++) {
st=new StringTokenizer(br.readLine());
rotates[i][0]=Integer.parseInt(st.nextToken());
rotates[i][1]=Integer.parseInt(st.nextToken());
checkRotateLeft(rotates[i][0], rotates[i][1]);
checkRotateRight(rotates[i][0], rotates[i][1]);
}
int now=1;
int result=0;
for(int i=1;i<=4;i++) {
if(magnets[i].get(0)==1) result+=now;
now*=2;
}
System.out.println("#"+t+" "+result);
}
}
private static void checkRotateLeft(int current, int dir) {
int idx=current-1;
int []direction=new int[5];
while(idx>0) {
dir*=-1;
if(magnets[idx].get(2)==magnets[idx+1].get(6)) break;
direction[idx]=dir;
idx--;
}
doRotate(current, direction);
}
private static void checkRotateRight(int current, int dir) {
int idx=current;
int []direction=new int[5];
direction[current]=dir;
while(idx<4) {
dir*=-1;
if(magnets[idx].get(2)==magnets[idx+1].get(6)) break;
direction[idx+1]=dir;
idx++;
}
doRotate(current, direction);
}
private static void doRotate(int current, int []direction) {
for(int i=1;i<direction.length;i++) {
if(direction[i]==-1) {
int get=magnets[i].get(0);
magnets[i].remove(0);
magnets[i].add(get);
} else if(direction[i]==1) {
int get=magnets[i].get(7);
magnets[i].remove(7);
magnets[i].add(0, get);
}
}
}
}
백준 14891번 톱니바퀴 문제와 거의 동일한 문제였습니다.
설명은 아래 백준 문제로 대체하겠습니다.
https://sujin7837.tistory.com/402
반응형
'코딩테스트 > 구현(Implementation)' 카테고리의 다른 글
[Java] 백준 5373번 : 큐빙 (0) | 2022.04.15 |
---|---|
[Java] 백준 17144번 : 미세먼지 안녕! (0) | 2022.04.14 |
[Java] 백준 14891번 : 톱니바퀴 (0) | 2022.04.12 |
[Java] 백준 14890번 : 경사로 (0) | 2022.04.08 |
[Java] 백준 14500번 : 테트로미노 (0) | 2022.04.08 |