728x90
package Samsung;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class GearWheel {
// n = Integer.parseInt(st.nextToken());
// l = Integer.parseInt(st.nextToken());
static LinkedList<LinkedList<Integer>>gear = new LinkedList<LinkedList<Integer>>();
static int count=0;
static void move( int n,int dir){
if(dir ==1){
LinkedList<Integer>ngear = new LinkedList<Integer>();
ngear = gear.get(n);
int num = ngear.peekLast();
ngear.pollLast();
ngear.add(0, num);
}
else if(dir==-1){
LinkedList<Integer>ngear = new LinkedList<Integer>();
ngear= gear.get(n);
int num = ngear.peekFirst();
ngear.pollFirst();
ngear.add(7, num);
}
}
static void simulateprev(int gearnum, int dir){
int prev = gearnum-1;
if(prev<1){return;}
else{
if(gear.get(prev-1).get(2)!=gear.get(gearnum-1).get(6)){
simulateprev(prev,-dir);
move(prev-1,-dir);
}
}
return;
}
static void simulatenext(int gearnum, int dir){
int next = gearnum+1;
if(next>4){return;}
else{
if(gear.get(next-1).get(6)!=gear.get(gearnum-1).get(2)){
simulatenext(next,-dir);
move(next-1,-dir);
}
}
return;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int k;
for (int i = 0; i < 4; i++) {
gear.add(i, new LinkedList<Integer>());
String str = br.readLine();
for(int j=0; j<str.length(); j++){
if(i==0){
gear.get(0).add(str.charAt(j)-'0');
}else if(i==1){
gear.get(1).add(str.charAt(j)-'0');
}else if(i==2){
gear.get(2).add(str.charAt(j)-'0');
}else if(i==3){
gear.get(3).add(str.charAt(j)-'0');
}
}
}
k = Integer.parseInt(br.readLine());
for(int j=0; j<k; j++){
StringTokenizer st = new StringTokenizer(br.readLine());
int gearnum = Integer.parseInt(st.nextToken());
int dir = Integer.parseInt(st.nextToken());
simulateprev(gearnum,dir);
simulatenext(gearnum,dir);
move(gearnum-1,dir);
}
for(int i=0; i<4; i++){
if(gear.get(i).peekFirst()==1){
if(i==0){count+=1;}
else if(i==1){count+=2;}
else if(i==2){count+=4;}
else{count+=8;}
}
}
System.out.println(count);
return;
}
}
'백준 > 삼성기출' 카테고리의 다른 글
사다리조작 / 브루트포스 (0) | 2021.04.05 |
---|---|
감시/JAVA (0) | 2021.04.05 |
경사로- Java, 구현 (0) | 2021.04.02 |
스타트와 링크 - Java, 백트래킹 (0) | 2021.04.01 |
주사위 굴리기 (0) | 2021.03.27 |