Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- greedy
- 러닝일지
- 문제해결
- 프로그래머스
- BOJ
- 코딩테스트
- 알고리즘
- 백준
- math
- 운동기록
- 투포인터
- 이분탐색
- SQL
- BFS
- 건강
- Java
- dfs
- binary search
- 시뮬레이션
- priorityqueue
- DP
- 문자열
- SWEA
- 코테
- oracle
- COS PRO
- 문제풀이
- db
- 자바
- MySQL
Archives
- Today
- Total
슈콩
[BOJ] 백준 14891 톱니바퀴 본문
[문제]
https://www.acmicpc.net/problem/14891
[소스 코드]
import java.io.*;
import java.util.*;
import javax.print.attribute.standard.NumberUpSupported;
public class Main {
static int[][] gear;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
gear = new int[4][8];
for(int i=0;i<4;i++) {
String s = br.readLine();
for(int j=0;j<8;j++) {
gear[i][j] = s.charAt(j) - '0';
}
}
int k = Integer.parseInt(br.readLine());
while(k-->0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken()) - 1;
int d = Integer.parseInt(st.nextToken());
rotateWithEffect(n,d);
}
int result = 0;
for(int i=0;i<4;i++) {
if(gear[i][0]==1) result += 1<<i;
}
System.out.println(result);
}
private static void rotateWithEffect(int n,int d) {
int[] dir = new int[4];
dir[n] = d;
for(int i=n+1;i<4;i++) {
if(gear[i-1][2]!=gear[i][6]) {
dir[i] = dir[i-1]*-1;
}
}
for(int i=n-1;i>=0;i--) {
if(gear[i+1][6]!=gear[i][2]) {
dir[i] = dir[i+1]*-1;
}
}
for(int i=0;i<4;i++) {
if(dir[i]!=0) {
rotate(i,dir[i]);
}
}
}
private static void rotate(int n,int d) {
int temp = 0;
if(d==1) {
temp = gear[n][0];
for(int i=7;i>0;i--) {
gear[n][(i+1)%8] = gear[n][i];
}
gear[n][1] = temp;
}
else {
temp = gear[n][0];
for(int i=0;i<7;i++) {
gear[n][i] = gear[n][(i+1)%8];
}
gear[n][7] = temp;
}
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 15684 사다리 조작 (0) | 2025.09.23 |
|---|---|
| [BOJ] 백준 15683 감시 (0) | 2025.09.23 |
| [BOJ] 백준 14890 경사로 (0) | 2025.09.23 |
| [BOJ] 백준 14889 스타트와 링크 (0) | 2025.09.22 |
| [BOJ] 백준 2467 용액 (0) | 2025.09.22 |