백준 JAVA 14891번 톱니바퀴
문제 링크: https://www.acmicpc.net/problem/14891
14891번: 톱니바퀴
총 8개의 톱니를 가지고 있는 톱니바퀴 4개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴
www.acmicpc.net
시뮬레이션 문제로서 하나의 톱니바퀴를 돌렸을 때 옆에 있는 톱니바퀴들이 돌아간다면 상태 배열을 변경시키고 상태 배열에 따라서 시계방향, 반시계방향으로 돌려주면된다. 시작 지점에서의 시계, 반시계 방향으로 퍼져나가는 것을 생각해주면 어려운 문제는 아니다.
import java.util.*; import java.io.*; class info{ int num; int dir; public info(int num, int dir) { this.num = num; this.dir = dir; } } public class Main { static StringBuilder sb = new StringBuilder(); static String [ ] str; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static boolean[] status = new boolean[5]; static List<info> list = new ArrayList<>(); static int answer; public static void main(String args[]) throws IOException { str= new String[5]; for(int i=1; i<=4; i++){ str[i]=br.readLine(); } int count = Integer.parseInt(br.readLine()); for(int i=0; i<count; i++){ String[] s = br.readLine().split(" "); int num = Integer.parseInt(s[0]); int dir = Integer.parseInt(s[1]); list.add(new info(num,dir)); } for(int i=0; i<count; i++){ check(i); int recent = list.get(i).dir; int start = list.get(i).num; leftSequence(start,recent); recent = list.get(i).dir; if(recent==1){ recent=0; } else{ recent=1; } rightSequence(start,recent); Arrays.fill(status, false); } answer=sum(); System.out.println(answer); } public static void check(int index){ info info = list.get(index); status[info.num]=true; int start = info.num; for(int j=start; j>1; j--){ if(str[j].charAt(6)!=str[j-1].charAt(2)){ status[j-1]=true; continue; } break; } for(int j=start ; j<4; j++) { if (str[j].charAt(2) != str[j + 1].charAt(6)) { status[j + 1] = true; continue; } break; } } public static String right(String now){ String temp = now.substring(0,7); String a = String.valueOf(now.charAt(7)); return a.concat(temp); } public static String left(String now){ String temp = now.substring(1,8); String a= String.valueOf(now.charAt(0)); return temp.concat(a); } public static int sum(){ int result=0; if(str[1].charAt(0)=='1'){ result+=1; } if(str[2].charAt(0)=='1'){ result+=2; } if(str[3].charAt(0)=='1'){ result+=4; } if(str[4].charAt(0)=='1'){ result+=8; } return result; } public static void rightSequence(int start, int recent){ for(int j=start+1 ;j<=4; j++){ if(status[j]){ if(recent==1){ str[j]=right(str[j]); recent=0; } else{ str[j]=left(str[j]); recent=1; } } } } public static void leftSequence(int start, int recent){ for(int j=start; j>=1; j--){ if(status[j]){ if(recent==1){ str[j]=right(str[j]); recent=0; } else{ str[j]=left(str[j]); recent=1; } } } } }

'Alogorithm > simulation' 카테고리의 다른 글
백준 16235[자바] java 나무 재테크 (0) | 2022.04.11 |
---|---|
백준 15685 [자바] java 드래곤 커브 (0) | 2022.02.05 |
백준 17144 [자바] java 미세먼지 안녕! (0) | 2022.01.08 |
댓글
이 글 공유하기
다른 글
-
백준 16235[자바] java 나무 재테크
백준 16235[자바] java 나무 재테크
2022.04.11 -
백준 15685 [자바] java 드래곤 커브
백준 15685 [자바] java 드래곤 커브
2022.02.05 -
백준 17144 [자바] java 미세먼지 안녕!
백준 17144 [자바] java 미세먼지 안녕!
2022.01.08
댓글을 사용할 수 없습니다.