알고리즘/백준
[BaekJoon] 14892 백준 톱니바퀴
suhaha
2020. 5. 31. 20:58
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
ArrayList<Integer>[] list = new ArrayList[4];
for(int i = 0; i<4; i++) {
String s = scan.nextLine();
String[] str = s.split("");
list[i] = new ArrayList<Integer>();
for(int j = 0; j<8; j++) {
list[i].add(Integer.parseInt(str[j]));
}
}
int k = scan.nextInt();
for(int i = 0; i<k; i++) {
int n = scan.nextInt();
int d = scan.nextInt();
rotate(d, list, n-1);
}
scan.close();
int count = 0;
for(int i = 0; i<4; i++) {
if(list[i].get(0) == 1) {
count += Math.pow(2, i);
}
}
System.out.println(count);
}
public static void rotate(int d, ArrayList<Integer>[] list, int n) {
int pre = list[n].get(2);
int pre2 = list[n].get(6);
int tmp = d;
for(int i = n+1; i<4; i++) {
if(pre == list[i].get(6)) {
break;
}
else {
pre = list[i].get(2); //새로운 톱니
if(d == -1) {
int t = list[i].get(7); //마지막 제거
list[i].remove(7);
list[i].add(0, t); //0에 부착
d = 1;
}
else {
int t = list[i].get(0); //0제거
list[i].remove(0);
list[i].add(t); //마지막에 부착
d = -1;
}
}
}
d = tmp;
for(int i = n-1; i>-1; i--) {
if(pre2 == list[i].get(2)) {
break;
}
else {
pre2 = list[i].get(6);
if( d == -1 ) {
int t = list[i].get(7); //마지막 제거
list[i].remove(7);
list[i].add(0, t); //0에 부착
d = 1;
}
else {
int t = list[i].get(0); //0제거
list[i].remove(0);
list[i].add(t); //마지막에 부착
d = -1;
}
}
}
if(tmp == -1) {
int t = list[n].get(0);
list[n].remove(0);
list[n].add(t);
}
else {
int t = list[n].get(7);
list[n].remove(7);
list[n].add(0, t);
}
}
}
톱니바퀴 돌리는 것을 list화 해서 첫번째와 마지막 요소만 바꿔주면 된다.